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

替换后的效果

相关推荐
2501_9307077816 小时前
使用C#代码为 PDF 文档添加印章
pdf
daanpdf16 小时前
C1C2驾照考试科目一题库和答案大全免费版下载2026
pdf
开开心心就好17 小时前
吾爱大佬原创的文件时间修改工具
安全·智能手机·pdf·电脑·智能音箱·智能手表·1024程序员节
开开心心_Every18 小时前
安卓免费证件照制作软件,无广告弹窗
linux·运维·服务器·安全·elasticsearch·zookeeper·pdf
优化控制仿真模型1 天前
【2026】新大纲普通话考试真题题库50套(PDF电子版)
经验分享·pdf
开开心心_Every2 天前
免费简洁的安卓黄历日历,软件推荐
运维·服务器·随机森林·pdf·电脑·excel·最小二乘法
梵克之泪3 天前
批量拆分PDF只取PDF的首页,批量按文件页数拆分PDF,按卷内目录页码表计算批量拆分分割PDF
pdf·pdf拆分
aisifang003 天前
PDF转Word神器:Gemini3.1Pro一键搞定文档处理
人工智能·pdf·word
盘古工具3 天前
【分享】打开PDF文件的几种方式
windows·pdf
优化控制仿真模型4 天前
2026年初中英语考纲词汇表(1600词)PDF电子版
经验分享·pdf