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

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

Introduction

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

Prototype

public String toUnicodeString() 

Source Link

Document

Returns the Unicode String value of this PdfString-object.

Usage

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

License:LGPL

/**
 * This method will populate the text bookmark list.
 * /*www . ja  v a  2s .  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:it.flavianopetrocchi.jpdfbookmarks.itextbookmarksconverter.iTextBookmarksConverter.java

License:Open Source License

private Bookmark bookmarkFromDictionary(PdfDictionary outline) {
    if (outline == null) {
        return null;
    }//from  w ww  . j  a  v a  2  s.  c  o  m
    Bookmark bookmark = new Bookmark();
    PdfString title = (PdfString) PdfReader.getPdfObjectRelease(outline.get(PdfName.TITLE));
    bookmark.setTitle(title.toUnicodeString());
    PdfArray color = (PdfArray) PdfReader.getPdfObjectRelease(outline.get(PdfName.C));
    if (color != null && color.size() == 3) {
        ByteBuffer out = new ByteBuffer();
        out.append(color.getAsNumber(0).floatValue()).append(' ');
        out.append(color.getAsNumber(1).floatValue()).append(' ');
        out.append(color.getAsNumber(2).floatValue());
        bookmark.setColor(new Color(color.getAsNumber(0).floatValue(), color.getAsNumber(1).floatValue(),
                color.getAsNumber(2).floatValue()));
    }
    PdfNumber style = (PdfNumber) PdfReader.getPdfObjectRelease(outline.get(PdfName.F));
    if (style != null) {
        int f = style.intValue();
        if ((f & 1) != 0) {
            bookmark.setItalic(true);
        }
        if ((f & 2) != 0) {
            bookmark.setBold(true);
        }
    }
    PdfNumber count = (PdfNumber) PdfReader.getPdfObjectRelease(outline.get(PdfName.COUNT));
    if (count != null && count.intValue() < 0) {
        bookmark.setOpened(false);
    } else {
        bookmark.setOpened(true);
    }
    try {
        PdfObject dest = PdfReader.getPdfObjectRelease(outline.get(PdfName.DEST));
        if (dest != null) {
            mapGotoBookmark(bookmark, dest);
        } else {
            PdfDictionary action = (PdfDictionary) PdfReader.getPdfObjectRelease(outline.get(PdfName.A));
            if (action != null) {
                setActionsRecursive(bookmark, action);
            } else {
                bookmark.setType(BookmarkType.Unknown);
            }
        }
    } catch (Exception e) {
        //empty on purpose
    }
    return bookmark;
}

From source file:net.sf.jasperreports.engine.export.PdfXmpCreator.java

License:Open Source License

private String extractInfo(PdfName key) {
    PdfString value = (PdfString) info.get(key);
    return value == null ? null : value.toUnicodeString();
}

From source file:net.sqs2.omr.master.sqm.PDFAttachmentExtractor.java

License:Apache License

/**
 * Unpacks a file attachment./*from  w ww  .  ja va  2 s. c om*/
 * 
 * @param reader
 *            The object that reads the PDF document
 * @param filespec
 *            The dictionary containing the file specifications
 * @param outPath
 *            The path where the attachment has to be written
 * @throws IOException
 */
private static byte[] unpackFile(PdfReader reader, PdfDictionary filespec, String suffix) throws IOException {
    if (filespec == null) {
        return null;
    }
    PdfName type = (PdfName) PdfReader.getPdfObject(filespec.get(PdfName.TYPE));
    if (!PdfName.F.equals(type) && !PdfName.FILESPEC.equals(type)) {
        return null;
    }
    PdfDictionary ef = (PdfDictionary) PdfReader.getPdfObject(filespec.get(PdfName.EF));
    if (ef == null) {
        return null;
    }
    PdfString fn = (PdfString) PdfReader.getPdfObject(filespec.get(PdfName.F));
    if (fn == null) {
        return null;
    }
    File fLast = new File(fn.toUnicodeString());
    String filename = fLast.getName();

    if (!filename.endsWith(suffix)) {
        return null;
    }

    PRStream prs = (PRStream) PdfReader.getPdfObject(ef.get(PdfName.F));
    if (prs == null) {
        return null;
    }
    return PdfReader.getStreamBytes(prs);
}

From source file:org.kuali.coeus.propdev.impl.budget.subaward.PropDevPropDevBudgetSubAwardServiceImpl.java

License:Open Source License

/**
 * Unpacks a file attachment.//from   w  w w .  ja  v a  2s. c  o m
 * @param reader The object that reads the PDF document
 * @param filespec The dictonary containing the file specifications
 * @throws IOException
 */

protected static Object[] unpackFile(PdfReader reader, PdfDictionary filespec) throws IOException {
    Object arr[] = new Object[2]; //use to store name and file bytes
    if (filespec == null) {
        return null;
    }

    PdfName type = (PdfName) PdfReader.getPdfObject(filespec.get(PdfName.TYPE));
    if (!PdfName.F.equals(type) && !PdfName.FILESPEC.equals(type)) {
        return null;
    }

    PdfDictionary ef = (PdfDictionary) PdfReader.getPdfObject(filespec.get(PdfName.EF));
    if (ef == null) {
        return null;
    }

    PdfString fn = (PdfString) PdfReader.getPdfObject(filespec.get(PdfName.F));
    if (fn == null) {
        return null;
    }

    File fLast = new File(fn.toUnicodeString());
    PRStream prs = (PRStream) PdfReader.getPdfObject(ef.get(PdfName.F));
    if (prs == null) {
        return null;
    }

    byte attachmentByte[] = PdfReader.getStreamBytes(prs);
    arr[0] = fLast.getName();
    arr[1] = attachmentByte;

    return arr;

}

From source file:org.kuali.coeus.propdev.impl.s2s.S2sUserAttachedFormServiceImpl.java

License:Open Source License

/**
 * Unpacks a file attachment./* w  w w.  j ava2s  . c om*/
 * @param filespec
 *            The dictonary containing the file specifications
 * @throws IOException
 */
private Object[] unpackFile(PdfDictionary filespec) throws IOException {

    if (filespec == null)
        return null;

    PdfName type = (PdfName) PdfReader.getPdfObject(filespec.get(PdfName.TYPE));

    if (!PdfName.F.equals(type) && !PdfName.FILESPEC.equals(type))
        return null;

    PdfDictionary ef = (PdfDictionary) PdfReader.getPdfObject(filespec.get(PdfName.EF));
    if (ef == null)
        return null;

    PdfString fn = (PdfString) PdfReader.getPdfObject(filespec.get(PdfName.F));
    if (fn == null)
        return null;

    File fLast = new File(fn.toUnicodeString());

    PRStream prs = (PRStream) PdfReader.getPdfObject(ef.get(PdfName.F));
    if (prs == null)
        return null;

    byte attachmentByte[] = PdfReader.getStreamBytes(prs);
    Object[] fileInfo = new Object[2];
    fileInfo[0] = fLast.getName();
    fileInfo[1] = attachmentByte;
    return fileInfo;
}

From source file:org.kuali.kra.s2s.service.impl.S2SUserAttachedFormServiceImpl.java

License:Educational Community License

/**
 * Unpacks a file attachment.//  w  ww .ja v  a2 s .co m
 *
 * @param reader
 *            The object that reads the PDF document
 * @param filespec
 *            The dictonary containing the file specifications
 * @throws IOException
 */
private Object[] unpackFile(PdfReader reader, PdfDictionary filespec) throws IOException {
    S2sUserAttachedFormAtt userAttachedS2SFormAttachmentBean = new S2sUserAttachedFormAtt();

    if (filespec == null)
        return null;

    PdfName type = (PdfName) PdfReader.getPdfObject(filespec.get(PdfName.TYPE));

    if (!PdfName.F.equals(type) && !PdfName.FILESPEC.equals(type))
        return null;

    PdfDictionary ef = (PdfDictionary) PdfReader.getPdfObject(filespec.get(PdfName.EF));
    if (ef == null)
        return null;

    PdfString fn = (PdfString) PdfReader.getPdfObject(filespec.get(PdfName.F));
    if (fn == null)
        return null;

    File fLast = new File(fn.toUnicodeString());

    PRStream prs = (PRStream) PdfReader.getPdfObject(ef.get(PdfName.F));
    if (prs == null)
        return null;

    byte attachmentByte[] = PdfReader.getStreamBytes(prs);
    Object[] fileInfo = new Object[2];
    fileInfo[0] = fLast.getName();
    fileInfo[1] = attachmentByte;
    return fileInfo;
}

From source file:org.pdfsam.console.business.pdf.handlers.UnpackCmdExecutor.java

License:Open Source License

/**
 * Unpack/*from  w w w. j  a  v  a  2  s .c o m*/
 * @param filespec the dictionary
 * @param outPath output directory
 * @param overwrite if true overwrite if already exists
 * @return number of unpacked files
 * @throws IOException
 */
private int unpackFile(PdfDictionary filespec, File outPath, boolean overwrite) throws IOException {
    int retVal = 0;
    if (filespec != null) {
        PdfName type = (PdfName) PdfReader.getPdfObject(filespec.get(PdfName.TYPE));
        if (PdfName.F.equals(type) || PdfName.FILESPEC.equals(type)) {
            PdfDictionary ef = (PdfDictionary) PdfReader.getPdfObject(filespec.get(PdfName.EF));
            PdfString fn = (PdfString) PdfReader.getPdfObject(filespec.get(PdfName.F));
            if (fn != null && ef != null) {
                LOG.debug("Unpacking file " + fn + " to " + outPath);
                File fLast = new File(fn.toUnicodeString());
                File fullPath = new File(outPath, fLast.getName());
                if (fullPath.exists()) {
                    //check if overwrite is allowed
                    if (overwrite) {
                        if (!fullPath.delete()) {
                            LOG.warn("Unable to overwrite " + fullPath.getAbsolutePath()
                                    + ", unable to unpack.");
                        }
                    } else {
                        LOG.warn("Cannot overwrite " + fullPath.getAbsolutePath()
                                + " (overwrite is false), unable to unpack.");
                    }
                } else {
                    PRStream prs = (PRStream) PdfReader.getPdfObject(ef.get(PdfName.F));
                    if (prs != null) {
                        byte b[] = PdfReader.getStreamBytes(prs);
                        FileOutputStream fout = new FileOutputStream(fullPath);
                        fout.write(b);
                        fout.close();
                        retVal = 1;
                    }
                }
            }
        }
    }
    return retVal;
}

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

License:Apache License

private void unpack(Set<PdfDictionary> dictionaries) throws TaskIOException {
    for (PdfDictionary dictionary : dictionaries) {
        PdfName type = (PdfName) PdfReader.getPdfObject(dictionary.get(PdfName.TYPE));
        if (PdfName.F.equals(type) || PdfName.FILESPEC.equals(type)) {
            PdfDictionary ef = (PdfDictionary) PdfReader.getPdfObject(dictionary.get(PdfName.EF));
            PdfString fn = (PdfString) PdfReader.getPdfObject(dictionary.get(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()));
                }/*  ww  w.jav  a 2  s.  c o m*/
            }
        }
    }
}