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

替换后的效果

相关推荐
正在走向自律2 小时前
实战心得:利用PaddleOCR彻底解决大模型无法解析图片型PDF的问题
开发语言·pdf·视觉检测·paddleocr·视觉模型·离线ocr识别
AI原来如此2 小时前
零基础教程:50页PDF一键浓缩成1页摘要
人工智能·ai·pdf·大模型
186******205312 小时前
PDF 转 Word 高效办公实战指南
pdf·word
随手工具-Excel, pdf, SQL3 小时前
PDF删除页面怎么操作?免费在线删除PDF指定页面,无需安装软件
pdf
SunnyDays101114 小时前
Java 实现 PDF 页面删除、排序、旋转和裁剪
java·pdf
zyplayer-doc1 天前
个人 AI 知识库怎么搭建:用 zyplayer-doc 把笔记、PDF 和图片资料变成可问答的第二大脑
大数据·开发语言·人工智能·笔记·pdf·ocr
日日行不惧千万里1 天前
Java AI 完整学习笔记
pdf
听风吹等浪起2 天前
002:多图合并导出PDF【网页版】
计算机视觉·pdf
蝶恋舞者2 天前
NMC-G2网络管理卡 资料官网下载流程
pdf
点我头像干啥2 天前
002:一键把多张图片合成一个PDF
计算机视觉·pdf