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

替换后的效果

相关推荐
其实秋天的枫7 小时前
2026年新高考英语大纲词汇表3500个电子版PDF(含正序版、乱序版和默写版)
经验分享·pdf
lijfrank7 小时前
MacOS 下 VS Code + LaTeX + Skim 双向同步配置
vscode·macos·pdf·latex·mactex
程序员的记录8 小时前
AI 实战 - 文档处理(pdf/work/md/txt...)
pdf
Muyuan199810 小时前
22.让 RAG Agent 更像真实产品:聊天页面优化、PDF 上传、知识库重建与检索片段展示
python·django·pdf·fastapi
打小就很皮...13 小时前
html2canvas + jsPDF 生成 PDF 的踩坑与解决方案总结
前端·pdf
优化控制仿真模型14 小时前
27考研数学一、二、三历年真题及答案解析PDF电子版(1987-2026年)
经验分享·pdf
huluang14 小时前
解决 Adobe Acrobat 裁剪 PDF 后内容仍存留的问题
pdf
其实秋天的枫14 小时前
27考研数学一、二、三历年真题及答案解析PDF电子版(1987-2026年)
经验分享·pdf
其实秋天的枫15 小时前
【2026专四】英语专业四级TEM4历年真题及答案电子版PDF(2009-2025年)
经验分享·pdf
SunnyDays101115 小时前
如何使用 Python 将 PDF 转换为 TIFF 或将 TIFF 转换为 PDF
人工智能·python·pdf