Example usage for com.itextpdf.text.pdf PdfArray getAsDict

List of usage examples for com.itextpdf.text.pdf PdfArray getAsDict

Introduction

In this page you can find the example usage for com.itextpdf.text.pdf PdfArray getAsDict.

Prototype

public PdfDictionary getAsDict(final int idx) 

Source Link

Document

Returns a PdfObject as a PdfDictionary, resolving indirect references.

Usage

From source file:com.poet.ar.remover.AnnotationRemover.java

/**
 * remove annotation that matches keywords
 *
 * @param page/*from   w  w  w .j a v a2  s.c o  m*/
 * @return count of removed annotations
 */
private static int doRemoveAnnotation(PdfDictionary page) {

    // all annotations in page i
    PdfArray annoArray = page.getAsArray(PdfName.ANNOTS);
    List<Integer> willRemovedIx = new ArrayList<Integer>();

    if (annoArray != null) {

        PdfDictionary annotation = null;
        PdfDictionary a = null;
        PdfString uri = null;
        for (int i = 0; i < annoArray.size(); i++) {

            annotation = annoArray.getAsDict(i);

            if (annotation == null) {
                continue;
            }

            a = annotation.getAsDict(PdfName.A);

            if (a == null) {
                continue;
            }

            uri = a.getAsString(PdfName.URI);

            if (uri == null) {
                continue;
            }

            String uriStr = uri.toString().trim();

            if (keywords.contains(uriStr)) {
                willRemovedIx.add(i);
            }

        }

        int i = 0;
        for (Integer ix : willRemovedIx) {
            annoArray.remove(ix - i++);
        }

    }

    return willRemovedIx.size();
}

From source file:de.rub.dez6a3.jpdfsigner.control.JPodPDFViewer.java

License:Open Source License

@Override
public ArrayList getAttachments() throws IOException {
    ArrayList files = new ArrayList();
    PdfReader reader = new PdfReader(conf.getPDFFile());
    PdfDictionary root = reader.getCatalog();
    PdfDictionary documentnames = root.getAsDict(PdfName.NAMES);
    PdfDictionary embeddedfiles = documentnames.getAsDict(PdfName.EMBEDDEDFILES);
    PdfArray filespecs = embeddedfiles.getAsArray(PdfName.NAMES);
    PdfDictionary filespec;/*from   w  ww. j ava 2 s .co  m*/
    PdfDictionary refs;
    for (int i = 0; i < filespecs.size();) {
        filespecs.getAsName(i++);
        filespec = filespecs.getAsDict(i++);
        refs = filespec.getAsDict(PdfName.EF);
        Iterator it = refs.getKeys().iterator();
        while (it.hasNext()) {
            PdfName key = (PdfName) it.next();
            if (key.toString().equals("/F")) {

                String filename = "-";
                String desc = "-";
                int size = -1;
                String moddate = "-";
                String compsize = "-";
                PdfObject pdfobj = null;

                try {
                    filename = filespec.getAsString(key).toString();
                } catch (Exception e) {
                    log.warn("Cannot load attachment-name - " + e.getMessage());
                }
                try {
                    desc = filespec.getAsString(PdfName.DESC).toString();
                } catch (Exception e) {
                    log.warn("Cannot load attachment-description - " + e.getMessage());
                }
                byte[] attBytes = null;
                try {
                    PRStream stream = (PRStream) PdfReader.getPdfObject(refs.getAsIndirectObject(key));
                    attBytes = PdfReader.getStreamBytes(stream);
                    size = attBytes.length;
                } catch (Exception e) {
                    log.warn("Cannot load attachment-size - " + e.getMessage());
                }
                try {
                    pdfobj = PdfReader.getPdfObject(refs.getAsIndirectObject(key));
                } catch (Exception e) {
                    log.warn("Cannot load attachment-pdfobject - " + e.getMessage());
                }

                Hashtable fileData = new Hashtable();
                fileData.put(ATTACHMENT_FILENAME_STRING, filename); //filename
                fileData.put(ATTACHMENT_DESCRIPTION_STRING, desc); //Description
                fileData.put(ATTACHMENT_SIZE_INT, size); //size
                fileData.put(ATTACHMENT_BYTES_ARR, attBytes); //bytes
                files.add(fileData);
            }
        }
    }
    return files;
}

From source file:io.konik.carriage.itext.ITextInvoiceExtractor.java

License:Open Source License

private static PdfDictionary getValidFileSpec(PdfArray af) {
    if (af.isEmpty() || af.getAsDict(0) == null) {
        throw new InvoiceExtractionError("Pdf does not contain a FileSpec Entry");
    }/*w w  w .  j a  v  a 2  s.co  m*/
    return af.getAsDict(0);
}

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

License:Open Source License

/**
 * Extracts locations from the redact annotations contained in the document and applied to the given page.
 *//*from w  w w . j a v a  2  s  .  c  o m*/
private List<PdfCleanUpLocation> extractLocationsFromRedactAnnots(int page, PdfDictionary pageDict) {
    List<PdfCleanUpLocation> locations = new ArrayList<PdfCleanUpLocation>();

    if (pageDict.contains(PdfName.ANNOTS)) {
        PdfArray annotsArray = pageDict.getAsArray(PdfName.ANNOTS);

        for (int i = 0; i < annotsArray.size(); ++i) {
            PdfIndirectReference annotIndirRef = annotsArray.getAsIndirectObject(i);
            PdfDictionary annotDict = annotsArray.getAsDict(i);
            PdfName annotSubtype = annotDict.getAsName(PdfName.SUBTYPE);

            if (annotSubtype.equals(PdfName.REDACT)) {
                saveRedactAnnotIndirRef(page, annotIndirRef.toString());
                locations.addAll(extractLocationsFromRedactAnnot(page, i, annotDict));
            }
        }
    }

    return locations;
}

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 ww  .j a  v a2s. co 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);
    }
}