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

替换后的效果

相关推荐
驯龙高手_追风14 小时前
Adobe Acrobat PDF阅读器设置默认滚动翻页
adobe·pdf·adobe acrobat reader·adobe reader
优化控制仿真模型19 小时前
【26年社工】初级社会工作者历年真题及答案PDF电子版(2010-2025年)
经验分享·pdf
ComPDFKit20 小时前
PDF发票生成怎么做?从零到服务化落地的完整指南
pdf·pdf生成·文件生成·发票生成
weixin_441003641 天前
【2026年最新】初级社会工作者(社工)考试历年真题及答案解析电子版pdf(2010-2025年)
pdf
优化控制仿真模型1 天前
【26年7月】日语N1、N2、N3、N4、N5历年真题及答案PDF电子版(2010-2025年12月)
经验分享·pdf
jianwuhuang821 天前
智谱清言怎么导出pdf
人工智能·chatgpt·pdf·豆包·deepseek·ai导出鸭
daanpdf1 天前
专科英语A级和B级考试历年真题试卷及答案PDF电子版
pdf
南风微微吹2 天前
2015-2025年英语四级历年真题及答案解析电子版PDF(含听力音频)
pdf·英语四级
Metaphor6922 天前
使用 Python 将 Excel 转换为 PDF
python·pdf·excel
daanpdf2 天前
考研英语一历年真题及答案解析PDF(2010-2026)百度网盘
考研·pdf