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

替换后的效果

相关推荐
枫叶丹41 小时前
【HarmonyOS 6.0】ArkWeb PDF预览回调功能详解:让PDF加载状态可控可感
开发语言·华为·pdf·harmonyos
优化控制仿真模型1 小时前
【26年6月最新】英语六级2015-2025年12月历年真题及答案PDF
经验分享·pdf
开开心心_Every4 小时前
动图制作工具,拆分转视频动态照离线免费
运维·前端·人工智能·edge·pdf·散列表·启发式算法
枫叶丹45 小时前
【HarmonyOS 6.0】ArkWeb PDF浏览能力增强:指定PDF文档背景色功能详解
开发语言·华为·pdf·harmonyos
开开心心_Every1 天前
安卓图片压缩工具,无损缩放尺寸免费好用
人工智能·pdf·计算机外设·ocr·语音识别·团队开发·规格说明书
无心水1 天前
14、企业级表格|AWS Textract 扫描件表格自动结构化
架构·pdf·云计算·aws·pdf解析·pdf抽取·aws textract
无心水1 天前
13、云端OCR终极指南|百度/阿里/腾讯API高精度文字提取实战
百度·架构·pdf·ocr·dubbo·pdf解析·pdf抽取
优化控制仿真模型2 天前
2026年初中英语大纲词汇表1600词
经验分享·pdf
优化控制仿真模型2 天前
方正小标宋简体、仿宋GB2312和楷体_GB2312办公字体安装包下载安装教程
经验分享·pdf
王莎莎-MinerU2 天前
MinerU 生态全景:Skills、RAG、MCP、Cursor Rules
人工智能·深度学习·计算机视觉·chatgpt·pdf·软件工程