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

替换后的效果

相关推荐
我要学好英语9 小时前
知网下载的.caj文件怎么转成.pdf文件
pdf
墨痕诉清风14 小时前
文件上传漏洞(PDF文件)
安全·web安全·pdf
E_ICEBLUE14 小时前
【2026 最新教程】Java 自动化提取 PDF 表格:从文本到 Excel/CSV 的全场景实现
java·pdf·自动化
程序员哈基耄16 小时前
高效便捷的本地PDF处理工具——PDF工具箱全面解析
pdf
m5655bj17 小时前
使用 C# 将 PPT 转换为 PDF
pdf·c#·powerpoint
爱码猿2 天前
Springboot结合thymeleaf模板生成pdf文件
spring boot·后端·pdf
南风微微吹2 天前
2025年12月英语四级真题及答案解析完整版(第一、二、三套全PDF)
pdf·英语四级真题
我叫汪枫2 天前
Python 实战:把 PDF 表格完整转换成 Excel(小白教程)
python·pdf·excel
海上Bruce2 天前
长图PDF转多页PDF文件
pdf
南风微微吹2 天前
2025年12月英语六级真题及答案解析完整版(第一、二、三套全PDF)
pdf·英语六级