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

替换后的效果

相关推荐
tanis_20776 小时前
扫描版中文 PDF 怎么提取文字:用 MinerU 做 OCR + 结构化一体处理
人工智能·pdf·ocr
tanis_20778 小时前
MinerU2.5-Pro 中文 PDF 识别准确率全解:OmniDocBench v1.6 权威基准数据
人工智能·python·pdf
小林敲代码778812 小时前
基于 PDFBox 的 PDF 水印管理:使用 OCG 层实现精准添加与一键去除
pdf
tanis_207714 小时前
学术论文 PDF 的版面自动还原:MinerU 对多栏排版、浮动图表与脚注区域的识别实战
人工智能·pdf·ocr
tanis_207715 小时前
从 PDF 中精准提取表格、图片与公式:MinerU 结构化元素抽取的 3 种方案
pdf
Eric.Lee202116 小时前
python实现多个pdf合并
开发语言·python·pdf·pdf合并
寻道模式18 小时前
【开发心得】给私有部署OpenClaw添加PDF阅读技能
开发语言·python·pdf
优化控制仿真模型18 小时前
【2026六级】英语六级历年真题及答案PDF电子版(2015-2025年12月)
经验分享·pdf
南风微微吹18 小时前
初级银行从业资格考试历年真题及答案解析电子版pdf(2017-2025年)
pdf