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

替换后的效果

相关推荐
易鹤鹤.3 小时前
pdf标注高亮
pdf
2501_930707783 小时前
使用C#代码在 PDF 文档中创建表格
pdf
2601_961845157 小时前
行测电子版pdf教材|真题|解析
pdf·新媒体运营·github·个人开发·内容运营·规格说明书·极限编程
Metaphor6928 小时前
使用 Python 将 PDF 转换为 PDF/A
python·pdf
元启数宇8 小时前
PDF转CAD 3种方法实测:手动描线/AI矢量化/在线工具谁更快更准
人工智能·pdf
2603_9541383918 小时前
PDF 转 Word 工具深度评测:从参数解析到实战避坑
pdf·word
沉默王二1 天前
LlamaIndex 开源 LiteParse,零云依赖搞定扫描件PDF
pdf·开源
啦啦啦~~~3301 天前
【办公软件】开源的PDF合并分割工具!支持PDF拆分、合并、交替混合、页面旋转、提取页面等
阿里云·pdf·电脑·开源软件
tsfy20031 天前
Python批量调整Excel格式,并排版导出PDF
python·pdf·excel