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

替换后的效果

相关推荐
Nemo_XP1 天前
C# 打印PDF的常用方法
windows·pdf·c#
毕小宝1 天前
PDF文件转换之输出指定页到新的 PDF 文件
python·pdf
qq_393828221 天前
万兴PDF手机版
windows·学习·pdf·软件需求·安全架构
IDRSolutions_CN2 天前
如何用命令行将 PDF 表格转换为 HTML 表格
java·经验分享·pdf·软件工程·团队开发
开开心心就好3 天前
能按需拆分 PDF 为多个文档的工具
javascript·python·智能手机·django·pdf·word·excel
路漫漫其远,吾求索3 天前
并发解析hea,转为pdf格式
pdf·ssh
課代表3 天前
PDF 表单按钮动态边框效果
pdf·透明·按钮·复选框·pdf 表单·透明按钮·mouse up
java_强哥3 天前
SpringBoot+tabula+pdfbox解析pdf中的段落和表格数据
spring boot·后端·pdf
天下琴川3 天前
GitHub开源|AI顶会论文中文翻译PDF合集(gpt-translated-pdf-zh)
人工智能·gpt·pdf
huazeci3 天前
PHP生成pdf方法
开发语言·pdf·php