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 小时前
【FDE系列】阶段3:Day 63:文档解析 — 把真实 PDF 手册变成可用文本
人工智能·pdf·fde
码林拾遗WJ1 天前
学术 PDF 翻译工作流:从乱码到双语对照的实战记录
pdf
web打印社区1 天前
政务办事大厅:叫号小票、办理回执网页怎么静默出纸
前端·javascript·vue.js·pdf·html·政务
是枚小菜鸡儿吖1 天前
TraceBack:基于TextIn xParse,统一解析PDF/Word/Excel/扫描件/截图,自动交叉核验维修报告真伪,带原文出处,让造假无处遁形
pdf·word·excel
开开心心_Every1 天前
文件夹批量创建工具支持同级和多层级
运维·服务器·游戏·jupyter·智能手机·pdf·postman
一念春风2 天前
PDF浏览器(阅读、编辑、新建)
pdf
qq_369173632 天前
PDF 怎么分享成在线链接?在 AI 助手里一句话发布
pdf·agent·效率工具·ai 工具
开开心心就好2 天前
视频模糊怎么修复?免费工具支持批量处理
java·前端·人工智能·智能手机·pdf·excel
asdzx672 天前
Python 实现从 URL 在线下载 PDF 文档教程
python·pdf
大连好光景3 天前
RAG系统中,如何处理PDF文档中的表格?
pdf·ai-native