How do I redact all the annotations from WebViewer server-side?

Q: I am trying to redact all the annotations created by WebViewer, but haven’t been applied yet. I want to apply or burn-in redactions server-side.

A:

`

PDFNet.Initialize();
string output_path = “…/…/TestFiles/Output/”;

PDFDoc doc = new PDFDoc(…/…/TestFiles/ToBeRedacted.pdf");
doc.InitSecurityHandler();

ArrayList rarr = new ArrayList();

for (PageIterator itr = doc.GetPageIterator(); itr.HasNext(); itr.Next())
{
Page page = itr.Current();
int num_annots = page.GetNumAnnots();
for (int i = 0; i < num_annots; ++i)
{
Annot annot = page.GetAnnot(i);
if (!annot.IsValid()) continue;

if (annot.GetType().ToString().Equals(“e_Redact”))
{
rarr.Add(new Redactor.Redaction(annot.GetPage().GetIndex(), annot.GetRect(), false, “Top Secret”));
}
}
}

try
{
Redactor.Appearance app = new Redactor.Appearance();
app.RedactionOverlay = true;
app.Border = false;
app.ShowRedactedContentRegions = true;

Redactor.Redact(doc, rarr, app, false, true);
doc.Save(output_path + “redacted.pdf”, SDFDoc.SaveOptions.e_linearized);
}
catch (PDFNetException e)
{
Console.WriteLine(e.Message);
}

`

Here is a code snippet for applying redactions server-side from PDF coming from WebViewer in Java.

//---------------------------------------------------------------------------------------
// Copyright (c) 2001-2020 by PDFTron Systems Inc. All Rights Reserved.
// Consult legal.txt regarding legal and license information.
//---------------------------------------------------------------------------------------

import java.lang.*;
import java.awt.*;
import java.util.ArrayList;

import com.pdftron.pdf.*;
import com.pdftron.sdf.SDFDoc;

public class PDFRedactTest {

    public static void redact(PDFDoc doc, String output, Redactor.Redaction[] vec, Redactor.Appearance app) {
        try {
            Redactor.redact(doc, vec, app, false, true);
            doc.save(output, SDFDoc.SaveMode.REMOVE_UNUSED, null);
            doc.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Redactor.Redaction[] createRedactionArray(PDFDoc doc) {
        try {
            ArrayList<Redactor.Redaction> redactArrayList = new ArrayList<Redactor.Redaction>();

            int page_num = 1;

            for (PageIterator itr = doc.getPageIterator(); itr.hasNext();) {
                System.out.println("Page " + (page_num++) + ": ");
                Page page = itr.next();
                int num_annots = page.getNumAnnots();
                for (int i = 0; i < num_annots; ++i) {
                    Annot annot = page.getAnnot(i);
                    if (annot.isValid() == false)
                        continue;
                    switch (annot.getType()) {
                    case Annot.e_Redact: {
                        double[] rectOfAnnot = annot.getRect().get();
                        Rect newRect = new Rect(rectOfAnnot[0], rectOfAnnot[1], rectOfAnnot[2], rectOfAnnot[3]);
                        redactArrayList.add(new Redactor.Redaction(page_num-1, newRect, false, "Hehe"));
                    }
                        break;
                    default:
                        break;
                    }
                }
            }

            Redactor.Redaction[] redactArray = new Redactor.Redaction[redactArrayList.size()];
            redactArray = redactArrayList.toArray(redactArray);

            return redactArray;
        } catch (Exception e) {
            e.printStackTrace();
            Redactor.Redaction[] redactArray = new Redactor.Redaction[0];
            return redactArray;
        }
    }

    public static void main(String[] args) {
        // Relative paths to folders containing test files.
        String input_path = "../../TestFiles/";
        String output_path = "../../TestFiles/Output/";

        PDFNet.initialize();

        try {
            PDFDoc doc = new PDFDoc((input_path + "docToRedact.pdf"));
            doc.initSecurityHandler();
            Redactor.Redaction[] vec = createRedactionArray(doc);
            Redactor.Appearance app = new Redactor.Appearance();
            app.redactionOverlay = true;
            app.border = false;
            app.showRedactedContentRegions = true;

            redact(doc, output_path + "redacted.pdf", vec, app);

            System.out.println("Done...");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}