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

替换后的效果

相关推荐
T^T尚15 分钟前
h5实现pdf预览
vue.js·pdf
优化控制仿真模型4 小时前
【26年考研408】考研计算机408统考历年真题及答案解析PDF电子版(2009-2026年)
经验分享·pdf
小脑斧1231 天前
Adobe PDF 编辑器 破截一键激火
adobe·pdf
qq_283720051 天前
Python3 模块精讲|PyPDF2 万字实战:PDF 读写、拆分、合并、加水印一站式搞定
pdf·#办公自动化·#pypdf2·#pdf 处理·#python 实战
William.csj1 天前
科研——PPT导出高清PDF的方法
pdf·ppt
开开心心_Every1 天前
跨平台高速下载工具,支持浏览器功能强大
运维·服务器·随机森林·pdf·电脑·逻辑回归·excel
daanpdf1 天前
永乐大典pdf电子版(现存最全版本)
pdf
好运的阿财1 天前
OpenClaw工具拆解之 image+pdf
人工智能·python·程序人生·pdf·ai编程·openclaw·openclaw工具
开开心心就好2 天前
避免借电脑尴尬的故障模拟工具
科技·游戏·visualstudio·edge·pdf·电脑·powerpoint
南风微微吹3 天前
全国事业单位联考A、B、C、D、E类历年真题及答案解析PDF电子版(2015-2025年下)
pdf·考公