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

替换后的效果

相关推荐
其实秋天的枫1 天前
【2026年最新】驾考科目一考试题库2309道电子版pdf
经验分享·pdf
墨染天姬1 天前
【AI】如何基于cursor创建MCP索引pdf
人工智能·pdf
qq_429499571 天前
分享免费的PDF 翻译 原格式
pdf
来自外太空的鱼-张小张1 天前
jeecg预览pdf、jeecg无法预览pdf、jeecg自带预览pdf
pdf·状态模式
SEO-狼术1 天前
All-About-PDF provides DRM
pdf
珍朱(珠)奶茶1 天前
Spring Boot3整合FreeMark、itextpdf 5/7 实现pdf文件导出及注意问题
java·spring boot·后端·pdf·itextpdf
优化控制仿真模型2 天前
26年英语六级高频核心词汇1500个+历年真题PDF电子版
经验分享·pdf
xinixini2 天前
小初高全学科课程标准PDF
pdf
SQVIoMPLe2 天前
python-langchain框架(3-7-提取pdf中的图片 )
python·langchain·pdf
Highcharts.js2 天前
Highcharts客户端导出使用文档说明|图表导出模块讲解
前端·javascript·pdf·highcharts·图表导出