Example usage for com.itextpdf.text.pdf PdfString toUnicodeString

List of usage examples for com.itextpdf.text.pdf PdfString toUnicodeString

Introduction

In this page you can find the example usage for com.itextpdf.text.pdf PdfString toUnicodeString.

Prototype

public String toUnicodeString() 

Source Link

Document

Returns the Unicode String value of this PdfString-object.

Usage

From source file:commentextractor.CommentExtractorApp.java

License:GNU General Public License

static String extractComments(String filename, int first, int last) {
    StringBuffer output = null;/*from   w w  w.  j a v a 2s.  com*/
    try {
        PdfReader reader = new PdfReader(filename);

        if (last >= reader.getNumberOfPages() || (last == -1)) {
            last = reader.getNumberOfPages();
        }

        output = new StringBuffer(1024);

        for (int i = first; i <= last; i++) {

            PdfDictionary page = reader.getPageN(i);
            PdfArray annotsArray = null;

            if (page.getAsArray(PdfName.ANNOTS) == null) {
                continue;
            }

            annotsArray = page.getAsArray(PdfName.ANNOTS);
            for (ListIterator<PdfObject> iter = annotsArray.listIterator(); iter.hasNext();) {
                PdfDictionary annot = (PdfDictionary) PdfReader.getPdfObject(iter.next());
                PdfString content = (PdfString) PdfReader.getPdfObject(annot.get(PdfName.CONTENTS));
                if (content != null) {
                    output.append("----------\n");
                    output.append("Page " + i);
                    output.append("\n");
                    output.append(content.toUnicodeString().replaceAll("\r", "\r\n"));
                    output.append("\n");
                }
            }
        }
    } catch (Exception e) {
        Logger.getLogger(CommentExtractorApp.class.getName()).log(Level.SEVERE, null, e);
    }
    return new String(output);
}

From source file:de.offis.health.icardea.cied.pdf.extractor.PDFiText5Extractor.java

License:GNU General Public License

/**
 * This method will populate the text bookmark list.
 * //  w ww  .  j  av a2  s .c o m
 * @param rootOutlinesPdfDictionary The node element for the bookmark item.
 * @param indentionString The base indention string to be used.
 */
@SuppressWarnings("unchecked")
private void populateBookmarkTextList(PdfDictionary rootOutlinesPdfDictionary, String indentionString) {
    PdfDictionary outlineItemPdfDictionary = (PdfDictionary) PdfReader
            .getPdfObjectRelease(rootOutlinesPdfDictionary.get(PdfName.FIRST));
    while (outlineItemPdfDictionary != null) {
        PdfString bookmarkTitle = (PdfString) PdfReader
                .getPdfObjectRelease(outlineItemPdfDictionary.get(PdfName.TITLE));
        bookmarkTextList.add(indentionString + bookmarkTitle.toUnicodeString());
        logger.trace(indentionString + bookmarkTitle.toUnicodeString());

        /*
         * Recursive call to fill List
         */
        populateBookmarkTextList(outlineItemPdfDictionary, indentionString + bookmarkIndentionString());

        /*
         * Get next outline item
         */
        outlineItemPdfDictionary = (PdfDictionary) PdfReader
                .getPdfObjectRelease(outlineItemPdfDictionary.get(PdfName.NEXT));
    } // end while
}

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  ww w . ja va  2s. 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);
    }
}

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

License:Open Source License

private void drawOverlayText(PdfContentByte canvas, List<Rectangle> textRectangles, PdfString overlayText,
        PdfString otDA, PdfNumber otQ, PdfBoolean otRepeat) throws DocumentException, IOException {
    ColumnText ct = new ColumnText(canvas);
    ct.setLeading(0, 1.2F);//from ww  w .  j  a v  a  2s . c  om
    ct.setUseAscender(true);

    String otStr = overlayText.toUnicodeString();

    canvas.saveState();
    Map<String, List> parsedDA = parseDAParam(otDA);

    Font font = null;

    if (parsedDA.containsKey(STROKE_COLOR)) {
        List strokeColorArgs = parsedDA.get(STROKE_COLOR);
        setStrokeColor(canvas, strokeColorArgs);
    }

    if (parsedDA.containsKey(FILL_COLOR)) {
        List fillColorArgs = parsedDA.get(FILL_COLOR);
        setFillColor(canvas, fillColorArgs);
    }

    if (parsedDA.containsKey("Tf")) {
        List tfArgs = parsedDA.get("Tf");
        font = retrieveFontFromAcroForm((PdfName) tfArgs.get(0), (PdfNumber) tfArgs.get(1));
    }

    for (Rectangle textRect : textRectangles) {
        ct.setSimpleColumn(textRect);

        if (otQ != null) {
            ct.setAlignment(otQ.intValue());
        }

        Phrase otPhrase;

        if (font != null) {
            otPhrase = new Phrase(otStr, font);
        } else {
            otPhrase = new Phrase(otStr);
        }

        float y = ct.getYLine();

        if (otRepeat != null && otRepeat.booleanValue()) {
            int status = ct.go(true);

            while (!ColumnText.hasMoreText(status)) {
                otPhrase.add(otStr);
                ct.setText(otPhrase);
                ct.setYLine(y);
                status = ct.go(true);
            }
        }

        ct.setText(otPhrase);
        ct.setYLine(y);
        ct.go();
    }

    canvas.restoreState();
}

From source file:org.roda.common.certification.PDFSignatureUtils.java

private static StringBuilder getExtractionInformation(AcroFields fields, ArrayList<?> names,
        Path outputContents, String filename) throws IOException {

    StringBuilder sb = new StringBuilder();

    for (int i = 0; i < names.size(); i++) {
        String name = (String) names.get(i);
        Item item = fields.getFieldItem(name);

        PdfDictionary widget = item.getWidget(0);
        PdfDictionary infoDictionary = widget.getAsDict(PdfName.V);
        sb.append("<signature>\n");

        try {//from   w w w .  j  a v a 2 s  .c  o m
            PdfPKCS7 pk = fields.verifySignature(name);
            sb = addElementToExtractionResult(sb, "name", name);
            sb = addElementToExtractionResult(sb, "sign-name", pk.getSignName());
            sb = addElementToExtractionResult(sb, "version", Integer.toString(pk.getVersion()));
            sb = addElementToExtractionResult(sb, "reason", pk.getReason());
            sb = addElementToExtractionResult(sb, "location", pk.getLocation());

            SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");

            if (pk.getTimeStampDate() != null) {
                String timestamp = formatter.format(pk.getTimeStampDate().getTime());
                sb = addElementToExtractionResult(sb, "timestamp-time", timestamp);
            }

            if (pk.getSignDate() != null) {
                String sign = formatter.format(pk.getSignDate().getTime());
                sb = addElementToExtractionResult(sb, "sign-time", sign);
            }

            sb = addElementToExtractionResult(sb, "digest-algorithm", pk.getDigestAlgorithm());
            sb = addElementToExtractionResult(sb, "hash-algorithm", pk.getHashAlgorithm());
            sb = addElementToExtractionResult(sb, "covers-whole-document",
                    Boolean.toString(fields.signatureCoversWholeDocument(name)));
            sb = addElementToExtractionResult(sb, "ft", widget.get(PdfName.FT).toString());

            if (infoDictionary.contains(PdfName.CONTACTINFO))
                sb = addElementToExtractionResult(sb, "contact-info",
                        infoDictionary.getAsString(PdfName.CONTACTINFO).toString());

            if (infoDictionary.contains(PdfName.FILTER))
                sb = addElementToExtractionResult(sb, "filter", infoDictionary.get(PdfName.FILTER).toString());

            if (infoDictionary.contains(PdfName.SUBFILTER))
                sb = addElementToExtractionResult(sb, "subfilter",
                        infoDictionary.get(PdfName.SUBFILTER).toString());

            if (infoDictionary.contains(PdfName.LOCK))
                sb = addElementToExtractionResult(sb, "lock", "true");

            if (infoDictionary.contains(PdfName.CONTENTS)) {
                PdfString elementName = infoDictionary.getAsString(PdfName.CONTENTS);
                Files.write(outputContents, elementName.toUnicodeString().getBytes());
                sb = addElementToExtractionResult(sb, "contents", filename + ".pkcs7");
            }

        } catch (NoSuchFieldError e) {
            LOGGER.warn("DS information extraction did not execute properly");
        }

        sb.append("</signature>");
    }

    return sb;
}

From source file:org.roda.core.plugins.plugins.characterization.PDFSignatureUtils.java

private static StringBuilder getExtractionInformation(AcroFields fields, ArrayList<?> names,
        Path outputContents, String filename) throws IOException {
    StringBuilder sb = new StringBuilder();

    for (int i = 0; i < names.size(); i++) {
        String name = (String) names.get(i);
        Item item = fields.getFieldItem(name);

        PdfDictionary widget = item.getWidget(0);
        PdfDictionary infoDictionary = widget.getAsDict(PdfName.V);
        sb.append("<signature>\n");

        try {//from   ww  w  .j  ava2s  . c  om
            PdfPKCS7 pk = fields.verifySignature(name);
            sb = addElementToExtractionResult(sb, "name", name);
            sb = addElementToExtractionResult(sb, "sign-name", pk.getSignName());
            sb = addElementToExtractionResult(sb, "version", Integer.toString(pk.getVersion()));
            sb = addElementToExtractionResult(sb, "reason", pk.getReason());
            sb = addElementToExtractionResult(sb, "location", pk.getLocation());

            SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");

            if (pk.getTimeStampDate() != null) {
                String timestamp = formatter.format(pk.getTimeStampDate().getTime());
                sb = addElementToExtractionResult(sb, "timestamp-time", timestamp);
            }

            if (pk.getSignDate() != null) {
                String sign = formatter.format(pk.getSignDate().getTime());
                sb = addElementToExtractionResult(sb, "sign-time", sign);
            }

            sb = addElementToExtractionResult(sb, "digest-algorithm", pk.getDigestAlgorithm());
            sb = addElementToExtractionResult(sb, "hash-algorithm", pk.getHashAlgorithm());
            sb = addElementToExtractionResult(sb, "covers-whole-document",
                    Boolean.toString(fields.signatureCoversWholeDocument(name)));
            sb = addElementToExtractionResult(sb, "ft", widget.get(PdfName.FT).toString());

            if (infoDictionary.contains(PdfName.CONTACTINFO))
                sb = addElementToExtractionResult(sb, "contact-info",
                        infoDictionary.getAsString(PdfName.CONTACTINFO).toString());

            if (infoDictionary.contains(PdfName.FILTER))
                sb = addElementToExtractionResult(sb, "filter", infoDictionary.get(PdfName.FILTER).toString());

            if (infoDictionary.contains(PdfName.SUBFILTER))
                sb = addElementToExtractionResult(sb, "subfilter",
                        infoDictionary.get(PdfName.SUBFILTER).toString());

            if (infoDictionary.contains(PdfName.LOCK))
                sb = addElementToExtractionResult(sb, "lock", "true");

            if (infoDictionary.contains(PdfName.CONTENTS)) {
                PdfString elementName = infoDictionary.getAsString(PdfName.CONTENTS);
                Files.write(outputContents, elementName.toUnicodeString().getBytes());
                sb = addElementToExtractionResult(sb, "contents", filename + ".pkcs7");
            }

        } catch (NoSuchFieldError e) {
            LOGGER.warn("DS information extraction did not execute properly");
        }

        sb.append("</signature>");
    }

    return sb;
}

From source file:org.sejda.impl.itext5.component.PdfUnpacker.java

License:Open Source License

private void unpack(Set<PdfDictionary> dictionaries) throws TaskIOException {
    for (PdfDictionary dictionary : dictionaries) {
        PdfName type = dictionary.getAsName(PdfName.TYPE);
        if (PdfName.F.equals(type) || PdfName.FILESPEC.equals(type)) {
            PdfDictionary ef = dictionary.getAsDict(PdfName.EF);
            PdfString fn = dictionary.getAsString(PdfName.F);
            if (fn != null && ef != null) {
                PRStream prs = (PRStream) PdfReader.getPdfObject(ef.get(PdfName.F));
                if (prs != null) {
                    File tmpFile = copyToTemporaryFile(prs);
                    outputWriter.addOutput(file(tmpFile).name(fn.toUnicodeString()));
                }//from   w  w w.  jav  a 2s .c o m
            }
        }
    }
}