Net9为PDF文字替换,使用Spire.PDF版本10.12.4.1360

如下图,把第一行几个字换掉

Dll版本

替换代码

cs 复制代码
private void ReplaceTextInPage(object sender, RoutedEventArgs e)
{
    string title = "选择文件进行字符串替换...";
    string? pdfFile = FindFile(title);
    if (pdfFile is null)
        return;
    PdfDocument pdfDoc = new PdfDocument(pdfFile);

    // 创建PdfTextReplaceOptions 对象
    PdfTextReplaceOptions textReplaceOptions = new PdfTextReplaceOptions();

    // 指定文本替换的选项
    textReplaceOptions.ReplaceType = PdfTextReplaceOptions.ReplaceActionType.WholeWord;

    //遍历PDF文档所有页面
    for (int i = 0; i < pdfDoc.Pages.Count; i++)
    {
        PdfPageBase page = pdfDoc.Pages[i];

        // 根据页面创建PdfTextReplacer 对象
        PdfTextReplacer textReplacer = new PdfTextReplacer(page);
        textReplacer.Options = textReplaceOptions;

        // 将所有目标文本的出现替换为新文本
        textReplacer.ReplaceAllText("第一行", "行行行");
    }
    string name = Path.GetFileNameWithoutExtension(pdfFile);
    pdfDoc.SaveToFile($"{desktop}\\{name}_替换文本.pdf", FileFormat.PDF);
    System.Windows.MessageBox.Show("替换文本完成...");
}

替换后的效果

相关推荐
非凡ghost12 小时前
Xournal++(PDF文档注释工具) 中文绿色版
学习·pdf·生活·软件需求
MonkeyKing_sunyuhua13 小时前
使用ARQ做PDF OCR和 图片OCR的任务的方案
pdf·ocr
2501_9307077814 小时前
如何使用C#代码在 PDF 中添加或删除附件
pdf
m5655bj14 小时前
如何使用 Python 调整 PDF 页面顺序?
python·pdf
YuanYWRS15 小时前
办公基础:实现PDF中表单不改变格式的情况下转成excel
pdf·excel
E_ICEBLUE1 天前
PDF vs PDF/A:区别、场景与常用转换方法(2025 全面解读)
python·pdf
TextIn智能文档云平台1 天前
从散乱资料到智能知识库:基于TextIn与Coze的RAG实战
人工智能·pdf·知识库·rag·coze·文档解析
m0_564914922 天前
图片怎么快速转为PDF?PDF怎么快速转为图片?
pdf
2501_930707782 天前
如何使用C#代码将多张图片整合为一个PDF文档
开发语言·pdf·c#
拓端研究室2 天前
赢战2025电商新趋势下的增长策略报告:平台格局、跨境趋势、大促消费|附600+份报告PDF、数据、可视化模板汇总下载
大数据·pdf