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 天前
右键菜单管理工具,添加程序自定义名称位置
linux·运维·服务器·ci/cd·docker·pdf·1024程序员节
Mars空港1 天前
PDF 转换其他格式
pdf
YJlio1 天前
PsPing 学习笔记(14.6):直方图视图——可视化延迟分布与抖动
开发语言·笔记·python·学习·eclipse·pdf·github
paopao_wu1 天前
LangChainV1.0[03]-语义检索:PDF放入向量数据库
人工智能·python·langchain·pdf
我爱娃哈哈2 天前
SpringBoot集成:5分钟实现HTML转PDF功能
spring boot·pdf·html
月巴月巴白勺合鸟月半3 天前
几种 HTML 转 PDF的方式
pdf·c#
夏日白云4 天前
《PDF解析工程实录》第 12 章|别让模型贴着墙走:为什么加一圈空白,效果反而更好?
图像处理·机器学习·pdf·llm·大语言模型·rag·文档解析
December3104 天前
新手入门指南:PDF页面方向怎么旋转?
pdf·pdf页面旋转·旋转pdf页面方向·pdf旋转
Summer不秃4 天前
使用 SnapDOM + jsPDF 生成高质量 PDF (含多页分页, 附源码)
前端·javascript·vue.js·pdf·node.js
bosins4 天前
基于Python实现PDF文件个人隐私信息检查
开发语言·python·pdf