Text section for Redaction

66 views
Skip to first unread message

Chad Maggard

unread,
Sep 15, 2016, 3:47:54 PM9/15/16
to PDFTron PDFNet SDK
Hi..  I am looking for an updated example of how to retrieve the rectangle of the selected text, so I can redaction it.   I am looking to do this in C#  winforms. 

Every example I have found in this group is a few years old and not workable. 

Any guidance is greatly appreciated.. thanks 

Ryan

unread,
Sep 19, 2016, 1:48:22 PM9/19/16
to pdfne...@googlegroups.com
The following code will redact all text currently selected in the document.

You put this code into something like SetSmoothImages function in PDFViewForm.cs in the PDFViewTest sample project. Then you can highlight text, and then in the toolbar pick the Options->Smooth Images option to redact.

ArrayList rarr = new ArrayList();
Current_View.DocLockRead();
int itr = Current_View.GetSelectionBeginPage();
int end = Current_View.GetSelectionEndPage();
for (; itr <= end; ++itr)
{
   
PDFViewWPF.Selection selection = Current_View.GetSelection(itr);
   
double[] quads = selection.GetQuads();
   
int sz = quads.Length / 8;  //each quad has eight numbers (x0, y0), ... (x3, y3)
   
if (sz == 0) continue;
   
for(int i = 0; i < sz; ++i)
   
{
       
int offset = 8 * i;
       
// we just want an axis aligned bounding box.
       
double[] x_axis = { quads[offset], quads[offset + 2], quads[offset + 4], quads[offset + 6] };
       
double[] y_axis = { quads[offset + 1], quads[offset + 3], quads[offset + 5], quads[offset + 7] };
       
double x1 = x_axis.Min();
       
double y1 = y_axis.Min();
       
double x2 = x_axis.Max();
       
double y2 = y_axis.Max();
        rarr
.Add(new Redactor.Redaction(itr, new pdftron.PDF.Rect(x1, y1, x2, y2), false, ""));
   
}
}
Current_View.DocUnlockRead();
Redactor.Appearance app = new Redactor.Appearance();
app
.PositiveOverlayColor = System.Drawing.Color.Red;
app
.NegativeOverlayColor = System.Drawing.Color.WhiteSmoke;
Current_View.DocLock(true);
Redactor.Redact(Current_View.GetDoc(), rarr, app);
Current_View.DocUnlock();

Note, the two types of document locking that occurs. A write lock causes rendering to stop, so we delay until we are ready to redact.

Chad Maggard

unread,
Sep 20, 2016, 7:03:02 PM9/20/16
to PDFTron PDFNet SDK
Thank you your time and replying to me,  I really appreciate it.

Question.
Can I just set   "_pdfview.SetImageSmoothing(true);"  on form Load to fulfill the Smooth Imagine Requirement?


If so,  I added a button to run this code  "_pdfview.SetToolMode(PDFViewCtrl.ToolMode.e_highlight_create"  to highlight some text  

then I ran your code behind another button 

but No success, the code is not finding the highlighted text.


On Monday, September 19, 2016 at 12:48:22 PM UTC-5, Ryan wrote:
The following code will redact all text currently selected in the document.

You put this code into something like SetSmoothImages function in PDFViewForm.cs in the PDFViewTest sample project. Then you can highlight text, and then in the toolbar pick the Options->Smooth Images option to redact.

ArrayList rarr = new ArrayList();
_pdfview.DocLockRead();
int itr = _pdfview.GetSelectionBeginPage();
int end = _pdfview.GetSelectionEndPage();
for (; itr <= end;++itr )
{
    PDFViewCtrl.Selection selection = _pdfview.GetSelection(itr);
    double[] quads = selection.GetQuads();
    int sz = quads.Length / 8;  //each quad has eight numbers (x0, y0), ... (x3, y3)
    if (sz == 0) continue;
    // we just want an axis aligned bounding box.
    double[] x_axis = { quads[0], quads[2], quads[4], quads[6] };
    double[] y_axis = { quads[1], quads[3], quads[5], quads[7] };
    double x1 = x_axis.Min();
    double y1 = y_axis.Min();
    double x2 = x_axis.Max();
    double y2 = y_axis.Max();
    rarr.Add(new Redactor.Redaction(itr, new Rect(x1, y1, x2, y2), false, ""));
}
_pdfview.DocUnlockRead();
Redactor.Appearance app = new Redactor.Appearance();
app.PositiveOverlayColor = System.Drawing.Color.Red;
app.NegativeOverlayColor = System.Drawing.Color.WhiteSmoke;
_pdfview.DocLock(true);
Redactor.Redact(_pdfview.GetDoc(), rarr, app);
_pdfview.DocUnlock();
_pdfview.Update();

Chad Maggard

unread,
Sep 23, 2016, 4:50:06 PM9/23/16
to PDFTron PDFNet SDK
Ok.. I got it to work using the rectangle select tool,  but if I use that tool to select more than one line,  it only redacts the first line and keeps the other lines highlighted.

Ryan

unread,
Sep 26, 2016, 3:02:53 PM9/26/16
to PDFTron PDFNet SDK
Thanks for pointing this out. I corrected the original code, and it worked for me.
Reply all
Reply to author
Forward
0 new messages