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

替换后的效果

相关推荐
MaiTube&Maipdf5 分钟前
如何设置PDF文件的到期日期
pdf
聪明的墨菲特i6 分钟前
Python 办公技巧:PDF 自动化处理
python·pdf·自动化
inxunoffice2 小时前
批量压缩与优化 PDF 文档,减少 PDF 文件的大小
pdf
码农老起6 小时前
与Aspose.pdf类似的jar库分享
java·pdf·jar
Elastic 中国社区官方博客14 小时前
Elasticsearch:使用 Azure AI 文档智能解析 PDF 文本和表格数据
大数据·人工智能·elasticsearch·搜索引擎·pdf·全文检索·azure
inxunoffice15 小时前
批量删除 PDF 中的所有图片、所有二维码图片以及指定的某张图片
pdf
aiweker17 小时前
Python PDF解析利器:pdfplumber | AI应用开发
python·pdf
inxunoffice19 小时前
批量提取 PDF 文档中指定页为新的 PDF 文档
pdf
inxunoffice19 小时前
批量在多个 PDF 的指定位置插入页,如插入封面、插入尾页
前端·pdf
hu55667981 天前
Epub转PDF软件Calibre电子书管理软件
pdf