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

替换后的效果

相关推荐
开开心心就好5 小时前
定时结束进程的小工具,到点还能关机
linux·运维·服务器·智能手机·pdf·电脑·word
充值内卷8 小时前
从 WAML 到 PDF 阅读器:PentPDF 开发过程
pdf
Matt Feng9 小时前
PDF批量转图片:DPI、命名规则和长图怎么设
pdf·wps
Matt Feng11 小时前
PDF水印分4类:哪些能删,哪些删了会伤正文
pdf·wps
凤城老人13 小时前
开源少儿编程教材《代码岛》:41 讲 Python 入门,PDF/EPUB 免费下载
python·pdf·pygame
福兮说17 小时前
PDF 文件提取页面后发现文件体积反而变大?分析底层对象流与冗余结构
pdf
Matt Feng1 天前
PDF加水印和密码:6项权限设置与完整步骤
pdf·word·wps
泡海椒1 天前
Java PDF 报表开发:一页简历的两种版式风格对比与折行避坑
java·开发语言·pdf
奔跑的呱呱牛1 天前
两张发票印在一张 A4 上:宇境自研的聆境 PDF,完全免费
pdf
捷焱GEO2 天前
搜索还能下载旧 PDF,怎样沿一次请求找出该改的响应
http·pdf