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

替换后的效果

相关推荐
2501_907136823 小时前
批量改发票pdf名称为金额+发票号码+销售方,方便金额核对
pdf
li星野4 小时前
从 PDF 到 FAISS 向量索引:构建本地 RAG 数据预处理流水线
pdf·faiss
selfboot05 小时前
已知 PDF 密码,如何免费去掉密码保护并保存无密码副本
pdf
Pearson5 小时前
特大pdf文件在线预览技术方案
javascript·nginx·pdf
zyplayer-doc6 小时前
知识库官方CLI工具已发布并开源,以及重写思维导图编辑器,提供更完整的编辑能力,zyplayer-doc 2.6.6 发布啦!
人工智能·安全·pdf·编辑器·创业创新
庖丁AI6 小时前
PDF转Markdown工具怎么选?AI知识库和RAG场景要注意什么
人工智能·pdf·格式转换
2601_961875241 天前
高考真题电子版|2025高考全科真题分类PDF
金融·pdf·云计算·azure·七牛云存储·交友·高考
质造者1 天前
Python 本地 RAG 实战 | Ollama+ChromaDB 实现 PDF 离线智能问答
开发语言·python·pdf·大模型·rag
王莎莎-MinerU1 天前
从 OCR 到 Context Engineering:用 MinerU 搭一个可复现文档解析评测
人工智能·深度学习·机器学习·pdf·ocr·个人开发
m0_547486661 天前
华南农业大学《数据结构》期末试卷及答案2011-2019 2020-2023年PDF
大数据·数据结构·pdf·华南农业大学