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

替换后的效果

相关推荐
E_ICEBLUE2 小时前
高效压缩 PDF 文件大小(3 大实用的 Python 库)
python·pdf
技术钱10 小时前
vue3 封装图片上传预览组件支持docx、excel、pdf、图片、txt格式
vue.js·pdf·excel
xingxing_F11 小时前
PDF Protector for mac PDF文档加密解密工具
macos·pdf
985小水博一枚呀13 小时前
【AI大模型学习路线】第三阶段之RAG与LangChain——第十九章(实战基于Advanced RAG的PDF问答)系统部署与测试?
人工智能·学习·langchain·pdf
励志成为美貌才华为一体的女子15 小时前
本地用docling实现pdf转markdown操作笔记
笔记·pdf
星空的资源小屋20 小时前
RoboIntern,一款自动化办公小助手
运维·人工智能·pdf·自动化·电脑·excel
mucheni1 天前
迅为RK3568开发板OpenHarmony系统南向驱动开发手册-pdf配置 rk3568_uart_config.hcs
驱动开发·pdf
ONLYOFFICE1 天前
ONLYOFFICE 桌面编辑器9.1版本已发布:PDF密文功能和全新注释、工作表公式优化及文件恢复便捷化等
pdf·编辑器·onlyoffice 模板
Aevget1 天前
DevExpress WinForms v25.1亮点 - PDF Viewer(查看器)等全新升级
pdf·c#·界面控件·winform·devexpress·ui开发
企鹅侠客2 天前
基于python写的PDF表格提取到excel文档
python·pdf·excel·pdf文档表格转excel