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

替换后的效果

相关推荐
不厌 -_-11 小时前
VSCode 中 Markdown 转 PDF
vscode·pdf
reasonsummer14 小时前
【办公类-130-01】20260628“中级会计实务母题”多个PDF合并一个PDF便于打印
pdf
鲲穹AI超级员工15 小时前
覆盖文档修改批注的电脑端 PDF 编辑工具功能与局限完整记录清单
pdf·文本编辑工具
脑子运行超载2 天前
Java动态生成PDF文件
java·pdf·状态模式·文件上传下载
uuai2 天前
vue导出pdf
前端·vue.js·pdf
Sour2 天前
产品说明书翻译成英文怎么保留排版?PDF、Word、外贸资料处理清单
pdf·word
winfredzhang2 天前
为长阅读而生:我做了一个「安静」的 PDF 阅读器
pdf·html
100个铜锣烧2 天前
文档处理管线:从PDF到向量——RAG系统的“第一道关口”
pdf
m0_547486663 天前
广东海洋大学《概率论与数理统计》期末试卷17-22 23-24学年+复习题PDF
pdf·概率论