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

替换后的效果

相关推荐
TextIn智能文档云平台4 小时前
PDF格式转化,哪款软件的准确率更高?
人工智能·pdf
帧栈5 小时前
开发避坑指南(73):itext7 pdf表单字体加粗解决方案
java·pdf
裤裤兔7 小时前
如何搜索多个PDF文件中的关键字?
pdf
TextIn智能文档云平台9 小时前
PDF提取文字全是乱码怎么解决?
pdf
伟贤AI之路11 小时前
原创分享:Markdown 转 Word 工具,一键导出Word/PDF文档
pdf·word·markdown·markdown转
yesyesido1 天前
PDF全能管家:3合1智能处理,一键解锁高效文档管理新体验
科技·考研·安全·pdf·生活·交互·改行学it
南风微微吹1 天前
2015-2025年英语四级历年真题及答案解析PDF电子版(可下载)
pdf·英语四级
拓端研究室1 天前
专题:2026年中国消费零售行业新图景报告:消费者行为、品牌增长与AI应用|附540+份报告PDF、数据、可视化模板汇总下载
人工智能·pdf·零售
YongCheng_Liang1 天前
Python实现PDF 转 JPG 批量转换工具
运维·python·pdf
weixin_462446231 天前
【原创实现】Python 实现 PDF 批量压缩工具(支持逐个确认 / 批量处理 / 多级压缩)
开发语言·python·pdf