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

替换后的效果

相关推荐
随手工具-Excel, pdf, SQL17 小时前
多个 PDF 怎么合并成一个?免费在线工具,还能混入图片和 Word
pdf·word·效率工具·在线工具·pdf合并·合并pdf
2601_9659128317 小时前
PDF加水印工具实测:2026年国内免费方案对比
pdf
AmyLin_200120 小时前
PDF 色彩保真工程实践【3】上手实践:工程结构、依赖集成与一键构建运行
c++·visualstudio·pdf·zlib·vcpkg·libtiff·cmyk
2601_9659128320 小时前
PDF转Word技术选型:2026年文档解析引擎性能对比与集成评估
pdf·word
鸿翼Macrowing2 天前
**从一份 PDF 到一次 API 调用:企业 AI 能力的“最后一公里“**
人工智能·pdf·agent·数据治理·skill
工具派2 天前
发PDF给外部前先做脱敏:我用在线工具批量涂黑了身份证号和姓名(实操记录)
pdf
南风微微吹2 天前
【英一】考研英语一历年真题及答案解析PDF(1980-2026年)
考研·pdf
界面开发小八哥2 天前
界面控件DevExpress Office & PDF File v26.1新版亮点 - 全新的条码生成API
pdf·.net·devexpress·ui开发·文档控件·.net 10
'pi%'2 天前
LangGraph 多智能体实战:搭建电力政策跟踪 Agent,实现网页爬虫、OCR 与 PDF 文档自动化解析
爬虫·pdf·ocr
DevOpenClub3 天前
用网页快照、静态 HTML 和 PDF 转换接口构建网页归档 Agent
前端·人工智能·pdf·html