简体   繁体   中英

Edit PDF text using C#

How can I find and then hide (or delete) specific text phrase?

For example, I have created a PDF file containing all sorts of data such as images, tables, text etc.

Now, I want to find a specific phrase like "Hello World" wherever it is mentioned in the file and somehow hide it, or -better even- delete it from the PDF.

And finally get the PDF after deleting this phrase.

I have tried iTextSharp and Spire , but couldn't find anything that worked.

Try the following code snippets to hide the specifc text phrase on PDF using Spire.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");
        }
    }
}

Result在此处输入图片说明

The following snippetfrom here let you find and black-out the text in pdf document:

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();

Pay attention to the license. It is AGPL, if you don't buy license.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM