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

替换后的效果

相关推荐
2601_963906783 小时前
可自由切割与合并的 PDF 尺寸调整与打印工具
pdf
2601_965912915 小时前
PDF转Word与拆分组合操作实测:从格式保真到批量处理的方案对比
pdf·word
海兰7 小时前
【应用】ClawPDF 完全指南:安装、部署与使用
人工智能·pdf·openclaw
XLYcmy8 小时前
PDF论文处理器 - 功能总结
数据库·python·pdf·csv·pymupdf·dify·文本分割
阿伟玩不懂9 小时前
AI帮你读PDF——自建文档解析与智能问答工具
人工智能·pdf
霸道流氓气质1 天前
基于本地 PaddleOCR 的 PDF 文字识别完整技术文档
开发语言·r语言·pdf
2601_965913001 天前
多份PDF批量转Word合并,三次踩坑后的方案对比与性能评估
pdf·word
桐桐桐1 天前
PDF 翻译工具横评与自建批量方案:从在线工具到 Python 自动化
python·pdf·自动化
百事牛科技1 天前
办公文档防护:如何限制 PDF 转换成其他格式文件
windows·pdf
SunnyDays10111 天前
Java 给 PDF 添加页码:实现“第 X 页,共 Y 页”
java·pdf·页码