Search in sources :

Example 6 with XmpParsingException

use of org.apache.xmpbox.xml.XmpParsingException in project mustangproject by ZUGFeRD.

the class MustangReaderWriterTest method checkPdfA3B.

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);
    }
}
Also used : XmpParsingException(org.apache.xmpbox.xml.XmpParsingException) PDFAIdentificationSchema(org.apache.xmpbox.schema.PDFAIdentificationSchema) XMPMetadata(org.apache.xmpbox.XMPMetadata) InputStream(java.io.InputStream) DomXmpParser(org.apache.xmpbox.xml.DomXmpParser) PDDocument(org.apache.pdfbox.pdmodel.PDDocument) PDMetadata(org.apache.pdfbox.pdmodel.common.PDMetadata)

Example 7 with XmpParsingException

use of org.apache.xmpbox.xml.XmpParsingException in project pdfbox by apache.

the class ExtractMetadata method main.

/**
 * This is the main method.
 *
 * @param args The command line arguments.
 *
 * @throws IOException If there is an error parsing the document.
 * @throws XmpParsingException
 */
public static void main(String[] args) throws IOException, XmpParsingException {
    if (args.length != 1) {
        usage();
        System.exit(1);
    } else {
        try (PDDocument document = PDDocument.load(new File(args[0]))) {
            PDDocumentCatalog catalog = document.getDocumentCatalog();
            PDMetadata meta = catalog.getMetadata();
            if (meta != null) {
                DomXmpParser xmpParser = new DomXmpParser();
                try {
                    XMPMetadata metadata = xmpParser.parse(meta.createInputStream());
                    DublinCoreSchema dc = metadata.getDublinCoreSchema();
                    if (dc != null) {
                        display("Title:", dc.getTitle());
                        display("Description:", dc.getDescription());
                        listString("Creators: ", dc.getCreators());
                        listCalendar("Dates:", dc.getDates());
                        listString("Subjects:", dc.getSubjects());
                    }
                    AdobePDFSchema pdf = metadata.getAdobePDFSchema();
                    if (pdf != null) {
                        display("Keywords:", pdf.getKeywords());
                        display("PDF Version:", pdf.getPDFVersion());
                        display("PDF Producer:", pdf.getProducer());
                    }
                    XMPBasicSchema basic = metadata.getXMPBasicSchema();
                    if (basic != null) {
                        display("Create Date:", basic.getCreateDate());
                        display("Modify Date:", basic.getModifyDate());
                        display("Creator Tool:", basic.getCreatorTool());
                    }
                } catch (XmpParsingException e) {
                    System.err.println("An error ouccred when parsing the meta data: " + e.getMessage());
                }
            } else {
                // The pdf doesn't contain any metadata, try to use the
                // document information instead
                PDDocumentInformation information = document.getDocumentInformation();
                if (information != null) {
                    showDocumentInformation(information);
                }
            }
        }
    }
}
Also used : XmpParsingException(org.apache.xmpbox.xml.XmpParsingException) XMPMetadata(org.apache.xmpbox.XMPMetadata) DomXmpParser(org.apache.xmpbox.xml.DomXmpParser) PDDocument(org.apache.pdfbox.pdmodel.PDDocument) XMPBasicSchema(org.apache.xmpbox.schema.XMPBasicSchema) PDMetadata(org.apache.pdfbox.pdmodel.common.PDMetadata) DublinCoreSchema(org.apache.xmpbox.schema.DublinCoreSchema) AdobePDFSchema(org.apache.xmpbox.schema.AdobePDFSchema) File(java.io.File) PDDocumentInformation(org.apache.pdfbox.pdmodel.PDDocumentInformation) PDDocumentCatalog(org.apache.pdfbox.pdmodel.PDDocumentCatalog)

Example 8 with XmpParsingException

use of org.apache.xmpbox.xml.XmpParsingException in project pdfbox by apache.

the class DeserializationTest method testWithNoXPacketStart.

@Test
public void testWithNoXPacketStart() throws Exception {
    InputStream fis = DomXmpParser.class.getResourceAsStream("/invalidxmp/noxpacket.xml");
    DomXmpParser xdb = new DomXmpParser();
    try {
        xdb.parse(fis);
        Assert.fail("Should fail during parse");
    } catch (XmpParsingException e) {
        Assert.assertEquals(ErrorType.XpacketBadStart, e.getErrorType());
    }
}
Also used : XmpParsingException(org.apache.xmpbox.xml.XmpParsingException) InputStream(java.io.InputStream) DomXmpParser(org.apache.xmpbox.xml.DomXmpParser) Test(org.junit.Test)

Example 9 with XmpParsingException

use of org.apache.xmpbox.xml.XmpParsingException in project pdfbox by apache.

the class DeserializationTest method testWithTwoRDFElement.

@Test
public void testWithTwoRDFElement() throws Exception {
    InputStream fis = DomXmpParser.class.getResourceAsStream("/invalidxmp/tworoot.xml");
    DomXmpParser xdb = new DomXmpParser();
    try {
        xdb.parse(fis);
        Assert.fail("Should fail during parse");
    } catch (XmpParsingException e) {
        Assert.assertEquals(ErrorType.Format, e.getErrorType());
    }
}
Also used : XmpParsingException(org.apache.xmpbox.xml.XmpParsingException) InputStream(java.io.InputStream) DomXmpParser(org.apache.xmpbox.xml.DomXmpParser) Test(org.junit.Test)

Example 10 with XmpParsingException

use of org.apache.xmpbox.xml.XmpParsingException in project pdfbox by apache.

the class DeserializationTest method testUndefinedSchema.

@Test
public void testUndefinedSchema() throws Exception {
    InputStream fis = DomXmpParser.class.getResourceAsStream("/invalidxmp/undefinedschema.xml");
    DomXmpParser xdb = new DomXmpParser();
    try {
        xdb.parse(fis);
        Assert.fail("Should fail during parse");
    } catch (XmpParsingException e) {
        Assert.assertEquals(ErrorType.NoSchema, e.getErrorType());
    }
}
Also used : XmpParsingException(org.apache.xmpbox.xml.XmpParsingException) InputStream(java.io.InputStream) DomXmpParser(org.apache.xmpbox.xml.DomXmpParser) Test(org.junit.Test)

Aggregations

DomXmpParser (org.apache.xmpbox.xml.DomXmpParser)13 XmpParsingException (org.apache.xmpbox.xml.XmpParsingException)13 InputStream (java.io.InputStream)11 Test (org.junit.Test)9 XMPMetadata (org.apache.xmpbox.XMPMetadata)4 PDDocument (org.apache.pdfbox.pdmodel.PDDocument)3 PDMetadata (org.apache.pdfbox.pdmodel.common.PDMetadata)3 ValidationError (org.apache.pdfbox.preflight.ValidationResult.ValidationError)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 PDDocumentCatalog (org.apache.pdfbox.pdmodel.PDDocumentCatalog)1 PDDocumentInformation (org.apache.pdfbox.pdmodel.PDDocumentInformation)1 ValidationException (org.apache.pdfbox.preflight.exception.ValidationException)1 FontMetaDataValidation (org.apache.pdfbox.preflight.font.util.FontMetaDataValidation)1 PDFAIdentificationValidation (org.apache.pdfbox.preflight.metadata.PDFAIdentificationValidation)1 RDFAboutAttributeConcordanceValidation (org.apache.pdfbox.preflight.metadata.RDFAboutAttributeConcordanceValidation)1 DifferentRDFAboutException (org.apache.pdfbox.preflight.metadata.RDFAboutAttributeConcordanceValidation.DifferentRDFAboutException)1 SynchronizedMetaDataValidation (org.apache.pdfbox.preflight.metadata.SynchronizedMetaDataValidation)1