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("替换文本完成...");
}

替换后的效果

相关推荐
Lucky_Turtle15 小时前
【论文写作】PDF图片不清晰,打印生成PDF空白大,PDF字体嵌入
开发语言·pdf·c#
工具派15 小时前
写技术文档时,我把Markdown编辑和PDF导出都搬到了浏览器
pdf
杰佛史彦明116 小时前
PDF 表格转 CSV:Python 实现方法与代码解析
开发语言·python·pdf
复园电子1 天前
解析PDF二进制结构:数字签名块(Signature Block)篡改异变分析
pdf
AI导出鸭PC端1 天前
豆包智能体停运在即,历史对话如何一键导出为 Word 或 PDF?
人工智能·pdf·word·豆包·ai导出鸭
不厌 -_-2 天前
VSCode 中 Markdown 转 PDF
vscode·pdf
reasonsummer2 天前
【办公类-130-01】20260628“中级会计实务母题”多个PDF合并一个PDF便于打印
pdf
鲲穹AI超级员工2 天前
覆盖文档修改批注的电脑端 PDF 编辑工具功能与局限完整记录清单
pdf·文本编辑工具
脑子运行超载3 天前
Java动态生成PDF文件
java·pdf·状态模式·文件上传下载