Skip to content
Guides

Redact Text and Data in PDF

This samples shows how to mark up cipher text, a separate licensable add-on that provides the option to remove (not just overwrite or blur) content within PDF areas.

C#
using ComPDFKit.Import;
using ComPDFKit.PDFAnnotation;
using ComPDFKit.PDFDocument;
using ComPDFKit.PDFPage;
using System; 
using System.IO; 
using System.Windows;

namespace PDFRedactTest
{
    internal class PDFRedactTest
    {
        static private string outputPath = Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory()))) + "\\Output\\Redact";
        static void Main(string[] args)
        {
            #region Perparation work
            Console.WriteLine("Running redact test sample…\r\n");
            SDKLicenseHelper.LicenseVerify();

            CPDFDocument document = CPDFDocument.InitWithFilePath("CommonFivePage.pdf");

            string str = document.PageAtIndex(0).GetTextPage().GetSelectText(new Point(300, 240), new Point(400, 300), new Point(0, 0));
            Console.WriteLine("The text need to be redact is: {0}", str);

            if (!Directory.Exists(outputPath))
            {
                Directory.CreateDirectory(outputPath);
            }
            #endregion

            #region Redact
            if (Redact(document))
            {
                Console.WriteLine("Redact done.");
            }
            else
            {
                Console.WriteLine("Redact failed.");
            }

            Console.WriteLine("--------------------");
            #endregion

            Console.WriteLine("Done!");
            Console.WriteLine("--------------------");

            Console.ReadLine();
        }

        /// <summary>
        /// Redact an area in PDF
        /// </summary>
        /// <param name="document">Regular document with some text</param>
        /// <returns></returns>
        static private bool Redact(CPDFDocument document)
        {
            CPDFPage page = document.PageAtIndex(0);
            CPDFRedactAnnotation redact = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_REDACT) as CPDFRedactAnnotation;
            //Set radact rect: cover the title
            redact.SetRect(new CRect(300, 240, 400, 300));
            //Set overlay text: REDACTED
            redact.SetOverlayText("REDACTED");

            //Properties of cover text
            CTextAttribute textAttribute = new CTextAttribute();
            textAttribute.FontName = "Helvetica";
            textAttribute.FontSize = 12;
            byte[] fontColor = { 255, 0, 0 };
            textAttribute.FontColor = fontColor;
            redact.SetTextDa(textAttribute);
            redact.SetTextAlignment(C_TEXT_ALIGNMENT.ALIGNMENT_LEFT);

            //Properties of cover square
            byte[] fillColor = { 255, 0, 0 };
            redact.SetFillColor(fillColor);
            byte[] outlineColor = { 0, 255, 0 };
            redact.SetOutlineColor(outlineColor);

            redact.UpdateAp();
            document.ApplyRedaction();
            // Save to pointed path so you can observe the effect.
            string path = outputPath + "\\RedactTest.pdf";
            if (!document.WriteToFilePath(path))
            {
                return false;
            }
            Console.WriteLine("Browse the changed file in " + path);
            CPDFDocument newDocument = CPDFDocument.InitWithFilePath(path);

            //Validation: try to get the text of the covered area
            string str = newDocument.PageAtIndex(0).GetTextPage().GetSelectText(new Point(60, 200), new Point(560, 250), new Point(0, 0));
            Console.WriteLine("Text in the redacted area is: {0}", str); 
            return true;
        }
    }
}