Tutorials

PDF Security: Using Redaction to Protect Your Documents

By ComPDFKit | Fri. 17 May. 2024
RedactionC#

With the continuous development of technology, the paperless office is more and more widely known, and people replace paper documents with digital documents. PDF is widely accepted as the preferred format for digital documents in many cases. However, as the leak of information and data becomes more common, many users are beginning to worry about the security of their files. So today we will talk about a great way to protect PDF documents —  redaction.

 

 

Introduce Redaction

 

If you need to share your document that contains plenty of your private information like your ID card number or your home address, you can remove that content by redacting it.

 

Redaction is the process of permanently removing any PDF content from your PDF files, such as text, data, images, etc. Once applying redaction, the selected content will be replaced by a black area (or another custom color) and cannot be accessed. Thus, redaction is one of the safest ways you can secure your highly sensitive information. For more details, you can click here

 

 

How to Redact

 

ComPDFKit provides various methods to redact the content you want, giving users a great experience. Here we take C# as an example.

 

Select an Area to Redact

 

A basic way is to select and mark an area in your PDF file for redaction. This is how to create a redaction annotation that covers the specified region on the first page of a document:

 

CPDFDocument document = CPDFDocument.InitWithFilePath("filePath");
CPDFPage page = document.PageAtIndex(0);
CPDFRedactAnnotation redact = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_REDACT) as CPDFRedactAnnotation;
redact.SetRect(new CRect(0, 50, 50, 0));
redact.SetOverlayText("REDACTED");

CTextAttribute textDa = new CTextAttribute();
textDa.FontName = "Helvetica";
textDa.FontSize = 12;
byte[] fontColor = { 255, 0, 0 };
textDa.FontColor = fontColor;
redact.SetTextDa(textDa);

redact.SetTextAlignment(C_TEXT_ALIGNMENT.ALIGNMENT_LEFT);
byte[] fillColor = { 255, 0, 0 };
redact.SetFillColor(fillColor);
byte[] outlineColor = { 0, 255, 0 };
redact.SetOutlineColor(outlineColor);

redact.UpdateAp();

 

Then you can apply the redactions.

document.ApplyRedaction();

 

Search for a Specific Text to Redact 

 

You can search for a specific text and identify all of its instances for redaction in the PDF document. For example, let’s search for and redact the text “ComPDFKit”.

 

CPDFDocument document = CPDFDocument.InitWithFilePath("***");
for (int i = 0; i < document.PageCount; i++)
{
    CPDFPage page = document.PageAtIndex(i);
    if (page == null)
        continue;

    CPDFTextPage textPage = page.GetTextPage();
    CPDFTextSearcher searcher = new CPDFTextSearcher();
    int findIndex = 0;

    if (searcher.FindStart(textPage, "ComPDFKit", C_Search_Options.Search_Case_Sensitive, findIndex))
    {
        CRect textRect = new CRect();
        string textContent = "";
        while (searcher.FindNext(page, textPage, ref textRect, ref textContent, ref findIndex))
        {
            CPDFRedactAnnotation redact = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_REDACT) as CPDFRedactAnnotation;
            redact.SetRect(textRect);
            redact.SetOverlayText("REDACTED");

            CTextAttribute textDa = new CTextAttribute();
            textDa.FontName = "Helvetica";
            textDa.FontSize = 12;
            byte[] fontColor = { 255, 0, 0 };
            textDa.FontColor = fontColor;
            redact.SetTextDa(textDa);

            redact.SetTextAlignment(C_TEXT_ALIGNMENT.ALIGNMENT_LEFT);
            byte[] fillColor = { 255, 0, 0 };
            redact.SetFillColor(fillColor);
            byte[] outlineColor = { 0, 255, 0 };
            redact.SetOutlineColor(outlineColor);

            redact.UpdateAp();
        }
    }
    searcher.FindClose();
}
document.Release();

 

Redact the Full Page

 

You can directly redact the full page. The following method shows how to redact page 5.

CPDFDocument document = CPDFDocument.InitWithFilePath("***");
CPDFPage page = document.PageAtIndex(4);
if (page != null)
    page.ApplyRedaction();

document.Release();

 

Redact the Whole Document

 

Redaction of the entire document is also supported by ComPDFKit. 

CPDFDocument document = CPDFDocument.InitWithFilePath("***");
document.ApplyRedaction();
document.Release();

 

 

Related Operations

 

Apart from redaction, ComPDFKit offers other ways to secure your files as well.

         - Password protected.

         - Watermark.

         - Headers & Footers.

         - Bates numbers.

         - Background.

If you want to know more about PDF security, please visit our documentation.

 

 

Conclusion

 

Security of PDF is an important issue that can’t be ignored. Hope this blog can give you some new ideas to protect your document. Feel free to contact us if you have any feedback.