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

替换后的效果

相关推荐
爱喝水的鱼丶18 小时前
SAP-ABAP:SAP 简单报表输出开发系列(共6篇) 第五篇:SAP 报表多格式输出:Excel/PDF 批量导出功能实现
学习·性能优化·pdf·excel·sap·abap
cosinmz1 天前
PDF 发票合并经验分享:月初高效整理发票的实用方法
经验分享·小程序·pdf·pdf转换·pdf发票合并·发票合并打印
一个博客1 天前
pdf-viewer 实现预览pdf文件
开发语言·javascript·pdf
代码小库1 天前
免费在线简历工具「面试帮」——18 款模板 + PDF 导出
面试·职场和发展·pdf
庖丁AI1 天前
文档比对工具怎么选?Word、PDF、扫描件差异检测思路
pdf·word·扫描件·文档比对
asdzx671 天前
使用 Python 快速提取 PDF 中的表格
python·pdf
南风微微吹2 天前
2026英语六级作文模版万能句子PDF电子版
pdf·英语六级
又是被bug折磨的一天2 天前
对多个pdf合同文件批量命名
pdf
南风微微吹2 天前
2026年英语四级作文模版万能句子PDF电子版
pdf·英语四级
这是个假程序员2 天前
PDF分色、智能PDF黑彩识别工具
pdf