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

替换后的效果

相关推荐
听风吹等浪起12 小时前
004:多个PDF合并成一个PDF【网页版】
pdf·状态模式
SamChan9014 小时前
用PostgreSQL+pgvector构建PDF翻译记忆库:向量相似度检索+增量更新实战
数据库·python·ai·postgresql·pdf·wpf
2601_963906781 天前
中文线装古籍电子书排版一键生成工具,PDF导出
pdf·软件需求
百事牛科技1 天前
只可读不可改!PDF禁止修改实操方法
windows·pdf
fruge2 天前
不用反复折腾 Word 排版:本地部署 Magic Resume,在线编辑并导出 PDF
pdf
gsls2008082 天前
Penpot 导出 PDF 报错 —— 问题排查与解决记录
pdf·原型·penpot
SamChan902 天前
PDF 翻译服务的全链路可观测性设计:OpenTelemetry + Jaeger + Loki 实战方案
后端·python·pdf·机器翻译
Source.Liu2 天前
【A11】基于 PDF 渲染引擎的统一文档界面架构设计
rust·pdf
zhonyu鱼4 天前
Saber:专为触控笔设计的开源手写笔记与 PDF 批注工具
笔记·pdf·开源
winfredzhang4 天前
用 Python + wxPython 造一个照片工具箱:PDF / 加密ZIP / MP4 / 归档,以及我在这过程中踩到的 4 个坑
python·pdf·zip·mp4·移动