簡體   English   中英

使用 C# 編輯 PDF 文本

[英]Edit PDF text using C#

如何找到然后隱藏(或刪除)特定的文本短語?

例如,我創建了一個 PDF 文件,其中包含各種數據,例如圖像、表格、文本等。

現在,我想在文件中提到的任何地方找到一個像“Hello World”這樣的特定短語,並以某種方式隱藏它,或者 - 更好的是 - 從 PDF 中刪除它。

刪除這句話后終於得到了PDF。

我試過iTextSharpSpire ,但找不到任何iTextSharp東西。

嘗試使用以下代碼片段使用 Spire.PDF 隱藏 PDF 上的特定文本短語。

using Spire.Pdf;
using Spire.Pdf.General.Find;
using System.Drawing;

namespace HideText
{
    class Program
    {
        static void Main(string[] args)
        {
            //load PDF file
            PdfDocument doc = new PdfDocument();
            doc.LoadFromFile(@"C:\Users\Administrator\Desktop\Example.pdf");

            //find all results where "Hello World" appears
            PdfTextFind[] finds = null;
            foreach (PdfPageBase page in doc.Pages)
            {
                finds = page.FindText("Hello World").Finds;               
            }

            //cover the specific result with white background color
            finds[0].ApplyRecoverString("", Color.White, false);

            //save to file
            doc.SaveToFile("output.pdf");
        }
    }
}

結果在此處輸入圖片說明

此處的以下代碼段可讓您找到並塗黑 pdf 文檔中的文本:

PdfDocument pdf = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST));
ICleanupStrategy cleanupStrategy = new RegexBasedCleanupStrategy(new Regex(@"Alice", RegexOptions.IgnoreCase)).SetRedactionColor(ColorConstants.PINK);
PdfAutoSweep autoSweep = new PdfAutoSweep(cleanupStrategy);
autoSweep.CleanUp(pdf);
pdf.Close();

注意許可證。 如果您不購買許可證,則它是 AGPL。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM