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

替换后的效果

相关推荐
qetfw12 小时前
初中数学教材PDF合集|人教北师大华师大等10个版本
pdf
绘梨衣54720 小时前
求助帖:pdf复杂表格解析
爬虫·python·pdf
大飞记Python1 天前
Linux命令速查手册(测试开发4年实战总结,附PDF)
linux·网络·pdf
2501_930707782 天前
如何在不损失质量的情况下将 PDF 转换为 SVG
pdf
eybk2 天前
写一个可以编制pdf文件的python程序
开发语言·python·pdf
YFJ_mily2 天前
**Python 实战:写一个论文 PDF 投稿自检工具|附 IPAT 2026 智能光子学会议征稿信息
人工智能·python·pdf·量子计算·论文投稿·智能光子学
qq_422152572 天前
PDF内容复用的几种实用方法:转PPT、转图片、转长图
pdf·php·powerpoint
Lucky_Turtle3 天前
【论文写作】PDF图片不清晰,打印生成PDF空白大,PDF字体嵌入
开发语言·pdf·c#
工具派3 天前
写技术文档时,我把Markdown编辑和PDF导出都搬到了浏览器
pdf
杰佛史彦明13 天前
PDF 表格转 CSV:Python 实现方法与代码解析
开发语言·python·pdf