Foxit PDF SDK for Linux Foxit PDF SDK for Mac Foxit PDF SDK for Windows

How to use Redaction with Foxit PDF SDK

What is Redaction?

Redaction is the process of removing data from a page for security purposes. With printed pages, redaction involves blacking-out or cutting-out areas of the printed page. With electronic documents that use formats such as PDF, redaction involves removing (not just obscuring) content within a region.

Redaction lets you permanently remove (redact) visible text and images from PDF documents to protect confidential information. Using redaction, you can distribute files with confidence by permanently removing sensitive data such as social security numbers, credit card information, and product launch dates. This ensures that no confidential information gets accidentally distributed.

How to use redaction in a PDF?

Redaction is a type of markup annotation. It identifies content that is intended to be removed from the document.

To do Redaction, you can use the following APIs:

  • First, call function addon::Redaction to create a redaction module. If the “Redaction” module is not defined in the license information which is used in function common::Library::Initialize, that means the user has no right to use redaction related functions and the constructor will throw exception foxit::e_ErrInvalidLicense.
  • Then, call function MarkRedactAnnot to create a redaction object and mark page contents (text object, image object, and path object) for redaction.
  • Finally call function Apply to apply redaction in the marked areas: the text or graphics under marked areas will be removed permanently.

How to redact the text “PDF” on the first page of a PDF?

Example:

(C++)

 #include "../../../include/common/fs_common.h" 
 #include "../../../include/pdf/fs_pdfdoc.h" 
 #include "../../../include/pdf/fs_pdfpage.h" 
 #include "../../../include/pdf/fs_search.h" 
 #include "../../../include/addon/fs_redaction.h" 
 #include "../../../include/common/fs_render.h" 

 using namespace foxit; 
 using namespace common; 
 using namespace addon; 
 using namespace pdf; 
 using namespace foxit::pdf::annots;
... 
 Redaction redaction(doc); 

 // Parse PDF page. 
 PDFPage page = doc.GetPage(0); 
 page.StartParse(foxit::pdf::PDFPage::e_ParsePageNormal, NULL, false); 

 //Find Text Object to redact 
 TextPage text_page(page); 
 TextSearch text_search(text_page); 
 text_search.SetPattern(L"PDF"); 
 RectFArray rect_array; 
 while(text_search.FindNext()) { 
 rect_array.Append(text_search.GetMatchRects()); 
 } 
 if(rect_array.GetSize() > 0) { 
 Redact redact = redaction.MarkRedactAnnot(page, rect_array); 
 redact.ResetAppearanceStream(); 
 doc.SaveAs(output_directory + L"AboutFoxit_redected_default.pdf"); 

 // set border color to Green 
 redact.SetBorderColor((long)0x00FF00); 

 // set fill color to Blue 
 redact.SetFillColor((long)0x0000FF); 

 // set rollover fill color to Red 
 redact.SetApplyFillColor((long)0xFF0000); 
 redact.ResetAppearanceStream(); 
 doc.SaveAs(output_directory + L"AboutFoxit_redected_setColor.pdf"); 

 redact.SetOpacity((float)0.5); 
 redact.ResetAppearanceStream(); 
 doc.SaveAs(output_directory + L"AboutFoxit_redected_setOpacity.pdf"); 

 if(redaction.Apply()) 
 cout << "Redact page(0) succeed." << endl; 
 else 
 cout << "Redact page(0) failed." << endl; 
 } 
 doc.SaveAs(output_directory + L"AboutFoxit_redected_apply.pdf"); 

C#

using foxit; 
using foxit.common; 
using foxit.common.fxcrt; 
using foxit.pdf; 
using foxit.addon; 
using foxit.pdf.annots; 
... 
using (Redaction redaction = new Redaction(doc)) 
{ 
 using (PDFPage page = doc.GetPage(0)) 
 { 

 // Parse PDF page. 
 page.StartParse((int)foxit.pdf.PDFPage.ParseFlags.e_ParsePageNormal, null, false); 
 TextPage text_page = new TextPage(page, (int)foxit.pdf.TextPage.TextParseFlags.e_ParseTextNormal); 
 TextSearch text_search = new TextSearch(text_page); 
 text_search.SetPattern("PDF"); 
 RectFArray rect_array = new RectFArray(); 
 while (text_search.FindNext()) 
 { 
 RectFArray itemArray = text_search.GetMatchRects(); 
 rect_array.InsertAt(rect_array.GetSize(), itemArray); 
 } 
 if (rect_array.GetSize() > 0) 
 { 
 Redact redact = redaction.MarkRedactAnnot(page, rect_array); 
 redact.ResetAppearanceStream(); 
 doc.SaveAs(output_path + "AboutFoxit_redected_default.pdf", (int)foxit.pdf.PDFDoc.SaveFlags.e_SaveFlagNormal); 

 // set border color to Green 
 redact.SetBorderColor(0x00FF00); 

 // set fill color to Blue 
 redact.SetFillColor(0x0000FF); 

 // set rollover fill color to Red 
 redact.SetApplyFillColor(0xFF0000); 
 redact.ResetAppearanceStream(); 
 doc.SaveAs(output_path + "AboutFoxit_redected_setColor.pdf", (int)foxit.pdf.PDFDoc.SaveFlags.e_SaveFlagNormal); 

 redact.SetOpacity((float)0.5); 
 redact.ResetAppearanceStream(); 
 doc.SaveAs(output_path + "AboutFoxit_redected_setOpacity.pdf", (int)foxit.pdf.PDFDoc.SaveFlags.e_SaveFlagNormal); 

 if (redaction.Apply()) 
 Console.WriteLine("Redact page(0) succeed."); 
 else 
 Console.WriteLine("Redact page(0) failed."); 
 redact.Dispose(); 
 } 
 } 
} 

Java

import com.foxit.sdk.PDFException; 
import com.foxit.sdk.common.Library; 
import com.foxit.sdk.common.fxcrt.RectFArray; 
import com.foxit.sdk.pdf.PDFDoc; 
import com.foxit.sdk.pdf.PDFPage; 
import com.foxit.sdk.pdf.TextPage; 
import com.foxit.sdk.pdf.TextSearch; 
import com.foxit.sdk.addon.Redaction; 
import com.foxit.sdk.pdf.annots.Redact; 
... 
Redaction redaction = new Redaction(doc); 
PDFPage page = doc.getPage(0); 

// Parse PDF page. 
page.startParse(PDFPage.e_ParsePageNormal, null, false); 
TextPage text_page = new TextPage(page, TextPage.e_ParseTextNormal); 
TextSearch text_search = new TextSearch(text_page); 
text_search.setPattern("PDF"); 
RectFArray matched_rect_array = new RectFArray(); 
while (text_search.findNext()) { 
 RectFArray temp_rect_array = text_search.getMatchRects(); 
 for (int z=0; z<temp_rect_array.getSize(); z++) 
 matched_rect_array.add(temp_rect_array.getAt(z)); 
} 
if (matched_rect_array.getSize()>0) { 
 Redact redact = redaction.markRedactAnnot(page, matched_rect_array); 
 redact.resetAppearanceStream(); 
 doc.saveAs(output_path + "AboutFoxit_redected_default.pdf", PDFDoc.e_SaveFlagNormal); 

 // set border color to Green 
 redact.setBorderColor((long)0x00FF00); 

 // set fill color to Blue 
 redact.setFillColor((long)0x0000FF); 

 // set rollover fill color to Red 
 redact.setApplyFillColor((long)0xFF0000); 
 redact.resetAppearanceStream(); 
 doc.saveAs(output_path + "AboutFoxit_redected_setColor.pdf", PDFDoc.e_SaveFlagNormal); 
 
 redact.setOpacity((float)0.5); 
 redact.resetAppearanceStream(); 
 doc.saveAs(output_path+"AboutFoxit_redected_setOpacity.pdf", PDFDoc.e_SaveFlagNormal); 

 redaction.apply(); 
} 

OC

#include "FSPDFObjC.h" 
...
FSRedaction *redaction = [[FSRedaction alloc] initWithDocument:doc];

// Parse PDF page.
FSPDFPage *page = [doc getPage:0];
[page startParse:FSPDFPageParsePageNormal pause:nil is_reparse:false];
FSTextPage *text_page = [[FSTextPage alloc] initWithPage:page flags:FSTextPageParseTextNormal];
FSTextSearch *text_search = [[FSTextSearch alloc] initWithText_page:text_page];
[text_search setPattern:@"PDF"];
FSRectFArray *rect_array = [[FSRectFArray alloc] init];
while ([text_search findNext]) {
FSRectFArray *matchrects = [text_search getMatchRects];
for (int z = 0; z < [matchrects getSize]; z++) {
FSRectF *temp_rect = [matchrects getAt:z];
[rect_array add:temp_rect];
}
}
if ([rect_array getSize] > 0) {
FSRedact *redact = [redaction markRedactAnnot:page rects:rect_array];
[redact resetAppearanceStream];
[doc saveAs:[output_directory stringByAppendingString:@"AboutFoxit_redected_default.pdf"] save_flags:FSPDFDocSaveFlagNormal];

// set border color to Green
[redact setBorderColor:0x00FF00];

// set fill color to Blue
[redact setFillColor:0x0000FF];

// set rollover fill color to Red
[redact setApplyFillColor:0xFF0000];
[redact resetAppearanceStream];
[doc saveAs:[output_directory stringByAppendingString:@"AboutFoxit_redected_setColor.pdf"] save_flags:FSPDFDocSaveFlagNormal];
[redact setOpacity:0.5];
[redact resetAppearanceStream];
[doc saveAs:[output_directory stringByAppendingString:@"AboutFoxit_redected_setOpacity.pdf"] save_flags:FSPDFDocSaveFlagNormal];
if ([redaction apply]) {
NSLog(@"Redact page (0) succeed.");
} else {
NSLog(@"Redact page (0) failed.");
}
}

Updated on February 18, 2019

Was this article helpful?
Thanks for your feedback. If you have a comment on how to improve the article, you can write it here: