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

替换后的效果

相关推荐
喝凉白开都长肉的大胖子7 小时前
比较 main.tex 的两个不同版本(例如旧版和新版),并生成一个带有修改标记(如删除线、高亮、修订注释)的 PDF 或文本输出。
pdf·latex
weixin_441003647 小时前
2025教资面试真题电子版|科目试讲+结构化真题解析|完整PDF
面试·职场和发展·pdf
小兜全糖(xdqt)7 小时前
python ppt转pdf以及图片提取
python·pdf·powerpoint
番石榴AI7 小时前
视频转ppt/pdf V2.0版(新增转为可编辑PPT功能)
人工智能·pdf·powerpoint
_深巷的猫10 小时前
python爬虫自动库DrissionPage保存网页快照mhtml/pdf/全局截图/打印机另存pdf
pdf
『 时光荏苒 』15 小时前
网页变成PDF下载到本地
前端·javascript·pdf·网页下载成
chenchihwen15 小时前
AI代码开发宝库系列:PDF文档解析MinerU
人工智能·python·pdf·dashscope
Source.Liu1 天前
【PDF-rs】pdf子项目的lib.rs文件
pdf
学会用脚编程1 天前
word转Pdf,在window正常,放在linux服务器上就转出来中文是空白
pdf·word
机器学习算法与Python实战1 天前
最好的大模型训练、微调教程.pdf
pdf