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

替换后的效果

相关推荐
ZeroNews内网穿透10 小时前
公网访问开源 Stirling-PDF 工具,提升办公效率
linux·运维·服务器·网络·pdf·ssh
千天夜10 小时前
Python合并多个PDF文件:完整指南与实践
windows·python·算法·pdf
Redundantº11 小时前
Uniapp 适配安卓与 iOS 的 PDF、DOC 文件上传
android·ios·pdf·uni-app·webview
旺旺的碎冰冰~11 小时前
Adobe如何给PDF添加页码
adobe·pdf
YANshangqian13 小时前
PDF工具箱 PDF24
pdf
优选资源分享13 小时前
PDF 补丁丁 v1.1.3.4663绿色版:PDF编辑软件
pdf·实用工具
南风微微吹1 天前
英语四级作文历年真题及范文模版汇总PDF电子版(2015-2025年6月)
pdf·英语四级
TextIn智能文档云平台1 天前
如何快速提取PDF文档内容
pdf
小马爱打代码1 天前
Spring AI:DeepSeek 整合 RAG 增强检索: 实现与 PDF 对话
人工智能·spring·pdf
南风微微吹1 天前
英语六级作文历年真题及范文模版汇总PDF电子版(2015-2025年6月)
pdf·英语六级