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

替换后的效果

相关推荐
2601_965912839 小时前
PDF添加页码功能横向评测:7款免费方案实测数据对比
pdf
AmyLin_200110 小时前
PDF 色彩保真工程实践【5】核心实现(下):拼装 Image XObject、页面资源与内容流
c++·pdf·sdk·颜色空间·印刷·icc·cmyk
Jazz_z10 小时前
如何用Python将彩色PDF一键转为黑白(灰度)
java·python·pdf
SamChan9014 小时前
PDF拆分+翻译+合并:一条流水线处理大文档的Python方案
java·python·ai·pdf·自动化
KH.15 小时前
AI翻译PDF保留排版原理:Gemini vs ChatGPT引擎实测对比
人工智能·gpt·ai·chatgpt·pdf·dall·e mini
2601_9659128316 小时前
PDF转Excel工具横向评测:6款免费方案实测数据对比
pdf·excel·notepad++
wtsolutions1 天前
PDF-to-Image-PDF的实际应用场景 - 从企业到个人的全面解决方案
pdf
2601_965912911 天前
PDF转Word工具深度横评:2026年国内免费方案技术对比
pdf·word
2501_930707781 天前
使用C#代码将PDF转化为线性PDF文件
pdf
诸葛大钢铁1 天前
PDF无法复制文字怎么办?3种方法解除PDF复制限制并提取文字
pdf·ocr·pdf教程·pdf解除限制·pdf无法复制·pdf文字识别