Example usage for com.itextpdf.text.pdf PdfDictionary remove

List of usage examples for com.itextpdf.text.pdf PdfDictionary remove

Introduction

In this page you can find the example usage for com.itextpdf.text.pdf PdfDictionary remove.

Prototype

public void remove(final PdfName key) 

Source Link

Document

Removes a PdfObject and its key from the PdfDictionary.

Usage

From source file:mkl.testarea.itext5.pdfcleanup.StrictPdfCleanUpProcessor.java

License:Open Source License

private void cleanUpPage(int pageNum, List<PdfCleanUpLocation> cleanUpLocations)
        throws IOException, DocumentException {
    if (cleanUpLocations.size() == 0) {
        return;//from www .ja v a  2  s.c om
    }

    PdfReader pdfReader = pdfStamper.getReader();
    PdfDictionary page = pdfReader.getPageN(pageNum);
    PdfContentByte canvas = pdfStamper.getUnderContent(pageNum);
    byte[] pageContentInput = ContentByteUtils.getContentBytesForPage(pdfReader, pageNum);
    page.remove(PdfName.CONTENTS);

    canvas.saveState();

    PdfCleanUpRegionFilter filter = createFilter(cleanUpLocations);
    PdfCleanUpRenderListener pdfCleanUpRenderListener = new PdfCleanUpRenderListener(pdfStamper, filter);
    pdfCleanUpRenderListener.registerNewContext(pdfReader.getPageResources(page), canvas);

    PdfContentStreamProcessor contentProcessor = new PdfContentStreamProcessor(pdfCleanUpRenderListener);
    PdfCleanUpContentOperator.populateOperators(contentProcessor, pdfCleanUpRenderListener);
    contentProcessor.processContent(pageContentInput, page.getAsDict(PdfName.RESOURCES));
    pdfCleanUpRenderListener.popContext();

    canvas.restoreState();

    colorCleanedLocations(canvas, cleanUpLocations);

    if (redactAnnotIndirRefs != null) { // if it isn't null, then we are in "extract locations from redact annots" mode
        deleteRedactAnnots(pageNum);
    }
}

From source file:mkl.testarea.itext5.pdfcleanup.StrictPdfCleanUpProcessor.java

License:Open Source License

/**
 * Deletes redact annotations from the page and substitutes them with either OverlayText or RO object if it's needed.
 *///from   w w  w .  j a v a 2s.c o m
private void deleteRedactAnnots(int pageNum) throws IOException, DocumentException {
    Set<String> indirRefs = redactAnnotIndirRefs.get(pageNum);

    if (indirRefs == null || indirRefs.isEmpty()) {
        return;
    }

    PdfReader reader = pdfStamper.getReader();
    PdfContentByte canvas = pdfStamper.getOverContent(pageNum);
    PdfDictionary pageDict = reader.getPageN(pageNum);
    PdfArray annotsArray = pageDict.getAsArray(PdfName.ANNOTS);

    // j is for access annotRect (i can be decreased, so we need to store additional index,
    // indicating current position in ANNOTS array in case if we don't remove anything
    for (int i = 0, j = 0; i < annotsArray.size(); ++i, ++j) {
        PdfIndirectReference annotIndRef = annotsArray.getAsIndirectObject(i);
        PdfDictionary annotDict = annotsArray.getAsDict(i);

        if (indirRefs.contains(annotIndRef.toString()) || indirRefs.contains(getParentIndRefStr(annotDict))) {
            PdfStream formXObj = annotDict.getAsStream(PdfName.RO);
            PdfString overlayText = annotDict.getAsString(PdfName.OVERLAYTEXT);

            if (fillCleanedArea && formXObj != null) {
                PdfArray rectArray = annotDict.getAsArray(PdfName.RECT);
                Rectangle annotRect = new Rectangle(rectArray.getAsNumber(0).floatValue(),
                        rectArray.getAsNumber(1).floatValue(), rectArray.getAsNumber(2).floatValue(),
                        rectArray.getAsNumber(3).floatValue());

                insertFormXObj(canvas, pageDict, formXObj, clippingRects.get(j), annotRect);
            } else if (fillCleanedArea && overlayText != null && overlayText.toUnicodeString().length() > 0) {
                drawOverlayText(canvas, clippingRects.get(j), overlayText, annotDict.getAsString(PdfName.DA),
                        annotDict.getAsNumber(PdfName.Q), annotDict.getAsBoolean(PdfName.REPEAT));
            }

            annotsArray.remove(i--); // array size is changed, so we need to decrease i
        }
    }

    if (annotsArray.size() == 0) {
        pageDict.remove(PdfName.ANNOTS);
    }
}