Example usage for org.apache.pdfbox.pdmodel PDDocument load

List of usage examples for org.apache.pdfbox.pdmodel PDDocument load

Introduction

In this page you can find the example usage for org.apache.pdfbox.pdmodel PDDocument load.

Prototype

public static PDDocument load(byte[] input) throws IOException 

Source Link

Document

Parses a PDF.

Usage

From source file:org.modeshape.sequencer.pdf.PdfBasicMetadata.java

License:Apache License

public boolean check() throws Exception {
    try (PDDocument document = PDDocument.load(in)) {
        PDDocumentCatalog catalog = document.getDocumentCatalog();
        PDPageable pageable = new PDPageable(document);
        PageFormat firstPage = pageable.getPageFormat(0);

        encrypted = document.isEncrypted();
        pageCount = document.getNumberOfPages();
        orientation = ORIENTATION_STRINGS[firstPage.getOrientation()];
        version = String.valueOf(document.getDocument().getVersion());
        String catalogVersion = catalog.getVersion();
        if (catalogVersion != null && !catalogVersion.isEmpty()) {
            // According to specs version saved here should be determining instead
            // the version in header. It is barely used, though.
            version = catalogVersion;/*www  .  j a va 2s  .  c  om*/
        }

        if (!encrypted) {
            PDDocumentInformation metadata = document.getDocumentInformation();
            author = metadata.getAuthor();
            creationDate = metadata.getCreationDate();
            creator = metadata.getCreator();
            keywords = metadata.getKeywords();
            modificationDate = metadata.getModificationDate();
            producer = metadata.getProducer();
            subject = metadata.getSubject();
            title = metadata.getTitle();
        }

        // extract all attached files from all pages
        int pageNumber = 0;
        for (Object page : catalog.getAllPages()) {
            pageNumber += 1;
            PdfPageMetadata pageMetadata = new PdfPageMetadata();
            pageMetadata.setPageNumber(pageNumber);
            for (PDAnnotation annotation : ((PDPage) page).getAnnotations()) {
                if (annotation instanceof PDAnnotationFileAttachment) {
                    PdfAttachmentMetadata attachmentMetadata = new PdfAttachmentMetadata();

                    PDAnnotationFileAttachment fann = (PDAnnotationFileAttachment) annotation;
                    PDComplexFileSpecification fileSpec = (PDComplexFileSpecification) fann.getFile();
                    PDEmbeddedFile embeddedFile = fileSpec.getEmbeddedFile();

                    attachmentMetadata.setSubject(fann.getSubject());
                    attachmentMetadata.setName(fileSpec.getFilename());
                    attachmentMetadata.setCreationDate(embeddedFile.getCreationDate());
                    attachmentMetadata.setModificationDate(embeddedFile.getModDate());
                    attachmentMetadata.setMimeType(embeddedFile.getSubtype());
                    attachmentMetadata.setData(embeddedFile.getByteArray());

                    pageMetadata.addAttachment(attachmentMetadata);
                }
            }
            pages.add(pageMetadata);
        }
        return true;
    }
}

From source file:org.modeshape.sequencer.pdf.PdfXmpMetadata.java

License:Apache License

public boolean check() throws Exception {
    try (PDDocument document = PDDocument.load(in)) {
        Boolean encrypted = document.isEncrypted();

        if (encrypted) {
            return false;
        }/*  w  w w . j ava 2 s  . com*/

        PDDocumentCatalog catalog = document.getDocumentCatalog();
        PDMetadata metadata = catalog.getMetadata();
        if (metadata == null) {
            return false;
        }

        DomXmpParser xmpParser = new DomXmpParser();
        try (InputStream is = metadata.createInputStream()) {
            XMPMetadata xmp = xmpParser.parse(is);
            XMPBasicSchema basicSchema = xmp.getXMPBasicSchema();
            if (basicSchema != null) {
                baseURL = basicSchema.getBaseURL();
                createDate = basicSchema.getCreateDate();
                creatorTool = basicSchema.getCreatorTool();
                if (basicSchema.getIdentifiers() != null) {
                    identifier.addAll(basicSchema.getIdentifiers());
                }
                metadataDate = basicSchema.getMetadataDate();
                modifyDate = basicSchema.getModifyDate();
                nickname = basicSchema.getNickname();
                rating = basicSchema.getRating();
                label = basicSchema.getLabel();
                return true;
            }
            return false;
        }

    }
}

From source file:org.mustangproject.ZUGFeRD.MustangReaderWriterTest.java

License:Open Source License

private void checkPdfA3B(File tempFile) throws IOException, InvalidPasswordException {
    try (PDDocument doc = PDDocument.load(tempFile)) {
        PDMetadata metadata = doc.getDocumentCatalog().getMetadata();
        InputStream exportXMPMetadata = metadata.exportXMPMetadata();
        byte[] xmpBytes = new byte[exportXMPMetadata.available()];
        exportXMPMetadata.read(xmpBytes);
        final XMPMetadata xmp = new DomXmpParser().parse(xmpBytes);
        PDFAIdentificationSchema pdfaid = xmp.getPDFIdentificationSchema();
        assertEquals(pdfaid.getPart().intValue(), 3);
        assertEquals(pdfaid.getConformance(), "U");
    } catch (XmpParsingException e) {
        throw new IllegalStateException("Failed to read PDF", e);
    }/*from   w ww  . j a va 2s  .co m*/
}

From source file:org.mustangproject.ZUGFeRD.ZUGFeRDExporter.java

License:Open Source License

/**
 * @deprecated Use the factory method {@link #createFromPDFA3(String)} instead
 *//* w  w w . j  av  a  2 s .co  m*/
@Deprecated
public void loadPDFA3(String filename) throws IOException {
    doc = PDDocument.load(new File(filename));
}

From source file:org.mustangproject.ZUGFeRD.ZUGFeRDExporter.java

License:Open Source License

public static ZUGFeRDExporter createFromPDFA3(String filename) throws IOException {
    return new ZUGFeRDExporter(PDDocument.load(new File(filename)));
}

From source file:org.mustangproject.ZUGFeRD.ZUGFeRDExporter.java

License:Open Source License

/**
 * @deprecated Use the factory method {@link #createFromPDFA3(InputStream)}
 * instead/* w  w w .  ja  v a  2 s.  com*/
 */
@Deprecated
public void loadPDFA3(InputStream file) throws IOException {
    doc = PDDocument.load(file);
}

From source file:org.mustangproject.ZUGFeRD.ZUGFeRDExporter.java

License:Open Source License

public static ZUGFeRDExporter createFromPDFA3(InputStream pdfSource) throws IOException {
    return new ZUGFeRDExporter(PDDocument.load(pdfSource));
}

From source file:org.mustangproject.ZUGFeRD.ZUGFeRDExporterFromA3Factory.java

License:Open Source License

/**
 * Makes A PDF/A3a-compliant document from a PDF-A1 compliant document (on the
 * metadata level, this will not e.g. convert graphics to JPG-2000)
 *
 * @param pdfFilename filename of an PDF/A1 compliant document
 *///w w  w.  ja  v a 2  s  .com
public ZUGFeRDExporter load(String pdfFilename) throws IOException {

    ensurePDFIsValidPDFA(new FileDataSource(pdfFilename));
    ZUGFeRDExporter zugFeRDExporter;
    PDDocument doc = PDDocument.load(new File(pdfFilename));
    zugFeRDExporter = new ZUGFeRDExporter(doc);
    zugFeRDExporter.setZUGFeRDVersion(ZFVersion);
    zugFeRDExporter.setZUGFeRDConformanceLevel(zugferdConformanceLevel);
    zugFeRDExporter.setCreator(creator);
    // Use creator as default for compatibility
    zugFeRDExporter.setCreatorTool(creatorTool != null ? creatorTool : creator);
    zugFeRDExporter.setProducer(producer);
    zugFeRDExporter.setAttachZUGFeRDHeaders(attachZUGFeRDHeaders);
    zugFeRDExporter.setPDFA3(ensurePDFisUpgraded);

    return zugFeRDExporter;

}

From source file:org.mustangproject.ZUGFeRD.ZUGFeRDExporterFromA3Factory.java

License:Open Source License

/**
 * Makes A PDF/A3a-compliant document from a PDF-A1 compliant document (on the
 * metadata level, this will not e.g. convert graphics to JPG-2000)
 *
 * @param pdfBinary binary of a PDF/A1 compliant document
 *//* ww  w  .j  a  v  a2  s.  co  m*/
public ZUGFeRDExporter load(byte[] pdfBinary) throws IOException {
    ensurePDFIsValidPDFA(new ByteArrayDataSource(new ByteArrayInputStream(pdfBinary)));
    ZUGFeRDExporter zugFeRDExporter;
    PDDocument doc = PDDocument.load(pdfBinary);
    zugFeRDExporter = new ZUGFeRDExporter(doc);
    zugFeRDExporter.setZUGFeRDVersion(ZFVersion);
    zugFeRDExporter.setZUGFeRDConformanceLevel(zugferdConformanceLevel);
    zugFeRDExporter.setCreator(creator);
    zugFeRDExporter.setProducer(producer);
    zugFeRDExporter.setAttachZUGFeRDHeaders(attachZUGFeRDHeaders);
    zugFeRDExporter.setPDFA3(ensurePDFisUpgraded);

    return zugFeRDExporter;
}

From source file:org.mustangproject.ZUGFeRD.ZUGFeRDImporter.java

License:Open Source License

/**
 * Extracts a ZUGFeRD invoice from a PDF document represented by an input
 * stream. Errors are reported via exception handling.
 *
 * @param pdfStream a inputstream of a pdf file
 *///w w w. j  ava 2 s.c  o m
private void extractLowLevel(InputStream pdfStream) throws IOException {
    try (PDDocument doc = PDDocument.load(pdfStream)) {
        // PDDocumentInformation info = doc.getDocumentInformation();
        PDDocumentNameDictionary names = new PDDocumentNameDictionary(doc.getDocumentCatalog());
        //start
        InputStream XMP = doc.getDocumentCatalog().getMetadata().exportXMPMetadata();

        xmpString = convertStreamToString(XMP);

        PDEmbeddedFilesNameTreeNode etn = names.getEmbeddedFiles();
        if (etn == null) {
            return;
        }

        Map<String, PDComplexFileSpecification> efMap = etn.getNames();
        // String filePath = "/tmp/";

        if (efMap != null) {
            extractFiles(efMap); // see
            // https://memorynotfound.com/apache-pdfbox-extract-embedded-file-pdf-document/
        } else {

            List<PDNameTreeNode<PDComplexFileSpecification>> kids = etn.getKids();
            for (PDNameTreeNode<PDComplexFileSpecification> node : kids) {
                Map<String, PDComplexFileSpecification> namesL = node.getNames();
                extractFiles(namesL);
            }
        }
    }
}