Search in sources :

Example 1 with XMPBasicSchema

use of org.apache.xmpbox.schema.XMPBasicSchema in project mustangproject by ZUGFeRD.

the class ZUGFeRDExporterFromA1Factory method makePDFA3compliant.

private void makePDFA3compliant(PDDocument doc) throws IOException {
    String fullProducer = producer + " (via mustangproject.org " + Version.VERSION + ")";
    PDDocumentCatalog cat = doc.getDocumentCatalog();
    PDMetadata metadata = new PDMetadata(doc);
    cat.setMetadata(metadata);
    XMPMetadata xmp = XMPMetadata.createXMPMetadata();
    PDFAIdentificationSchema pdfaid = new PDFAIdentificationSchema(xmp);
    xmp.addSchema(pdfaid);
    DublinCoreSchema dc = xmp.createAndAddDublinCoreSchema();
    dc.addCreator(creator);
    XMPBasicSchema xsb = xmp.createAndAddXMPBasicSchema();
    xsb.setCreatorTool(creator);
    xsb.setCreateDate(GregorianCalendar.getInstance());
    // PDDocumentInformation pdi=doc.getDocumentInformation();
    PDDocumentInformation pdi = new PDDocumentInformation();
    pdi.setProducer(fullProducer);
    pdi.setAuthor(creator);
    doc.setDocumentInformation(pdi);
    AdobePDFSchema pdf = xmp.createAndAddAdobePDFSchema();
    pdf.setProducer(fullProducer);
    /*
        *
        * To be on the safe side, we use level B without Markinfo because we
        * can not guarantee that the user correctly tagged the templates for
        * the PDF.
        */
    try {
        //$NON-NLS-1$ //$NON-NLS-1$
        pdfaid.setConformance(conformanceLevel.getLetter());
    } catch (BadFieldValueException ex) {
        // however the enum enforces that the conformance level is valid.
        throw new Error(ex);
    }
    pdfaid.setPart(3);
    if (attachZugferdHeaders) {
        addZugferdXMP(xmp);
    /*
								 * this is the only line where we do something
								 * Zugferd-specific, i.e. add PDF metadata
								 * specifically for Zugferd, not generically for
								 * a embedded file
								 */
    }
    try {
        metadata.importXMPMetadata(serializeXmpMetadata(xmp));
    } catch (TransformerException e) {
        throw new ZUGFeRDExportException("Could not export XmpMetadata", e);
    }
}
Also used : BadFieldValueException(org.apache.xmpbox.type.BadFieldValueException) XMPBasicSchema(org.apache.xmpbox.schema.XMPBasicSchema) PDMetadata(org.apache.pdfbox.pdmodel.common.PDMetadata) PDDocumentCatalog(org.apache.pdfbox.pdmodel.PDDocumentCatalog) PDFAIdentificationSchema(org.apache.xmpbox.schema.PDFAIdentificationSchema) XMPMetadata(org.apache.xmpbox.XMPMetadata) DublinCoreSchema(org.apache.xmpbox.schema.DublinCoreSchema) AdobePDFSchema(org.apache.xmpbox.schema.AdobePDFSchema) PDDocumentInformation(org.apache.pdfbox.pdmodel.PDDocumentInformation) TransformerException(javax.xml.transform.TransformerException)

Example 2 with XMPBasicSchema

use of org.apache.xmpbox.schema.XMPBasicSchema in project pdfbox by apache.

the class SynchronizedMetaDataValidation method validateMetadataSynchronization.

/**
 * Check if document information entries and XMP information are synchronized
 *
 * @param document the PDF Document
 * @param metadata the XMP MetaData
 * @return List of validation errors
 * @throws ValidationException
 */
public List<ValidationError> validateMetadataSynchronization(PDDocument document, XMPMetadata metadata) throws ValidationException {
    List<ValidationError> ve = new ArrayList<>();
    if (document == null) {
        throw new ValidationException("Document provided is null");
    } else {
        PDDocumentInformation dico = document.getDocumentInformation();
        if (metadata == null) {
            throw new ValidationException("Metadata provided are null");
        } else {
            DublinCoreSchema dc = metadata.getDublinCoreSchema();
            // TITLE
            analyzeTitleProperty(dico, dc, ve);
            // AUTHOR
            analyzeAuthorProperty(dico, dc, ve);
            // SUBJECT
            analyzeSubjectProperty(dico, dc, ve);
            AdobePDFSchema pdf = metadata.getAdobePDFSchema();
            // KEYWORDS
            analyzeKeywordsProperty(dico, pdf, ve);
            // PRODUCER
            analyzeProducerProperty(dico, pdf, ve);
            XMPBasicSchema xmp = metadata.getXMPBasicSchema();
            // CREATOR TOOL
            analyzeCreatorToolProperty(dico, xmp, ve);
            // CREATION DATE
            analyzeCreationDateProperty(dico, xmp, ve);
            // MODIFY DATE
            analyzeModifyDateProperty(dico, xmp, ve);
        }
    }
    return ve;
}
Also used : ValidationException(org.apache.pdfbox.preflight.exception.ValidationException) ArrayList(java.util.ArrayList) XMPBasicSchema(org.apache.xmpbox.schema.XMPBasicSchema) ValidationError(org.apache.pdfbox.preflight.ValidationResult.ValidationError) DublinCoreSchema(org.apache.xmpbox.schema.DublinCoreSchema) AdobePDFSchema(org.apache.xmpbox.schema.AdobePDFSchema) PDDocumentInformation(org.apache.pdfbox.pdmodel.PDDocumentInformation)

Example 3 with XMPBasicSchema

use of org.apache.xmpbox.schema.XMPBasicSchema in project pdfbox by apache.

the class TestSynchronizedMetadataValidation method testAllInfoSynhcronized.

/**
 * Check reaction when metadata are well-formed
 *
 * @throws Exception
 */
@Test
public void testAllInfoSynhcronized() throws Exception {
    title = "TITLE";
    author = "AUTHOR(S)";
    subject = "SUBJECTS";
    keywords = "KEYWORD(S)";
    creator = "CREATOR";
    producer = "PRODUCER";
    creationDate = Calendar.getInstance();
    modifyDate = Calendar.getInstance();
    // building temporary XMP metadata
    DublinCoreSchema dc = metadata.createAndAddDublinCoreSchema();
    XMPBasicSchema xmp = metadata.createAndAddXMPBasicSchema();
    AdobePDFSchema pdf = metadata.createAndAddAdobePDFSchema();
    // Writing info in XMP and Document Information dictionary
    // TITLE
    dico.setTitle(title);
    dc.setTitle("x-default", title);
    // AUTHOR
    dico.setAuthor(author);
    dc.addCreator(author);
    // SUBJECT
    dico.setSubject(subject);
    dc.addDescription("x-default", subject);
    // KEYWORDS
    dico.setKeywords(keywords);
    pdf.setKeywords(keywords);
    // CREATOR
    dico.setCreator(creator);
    xmp.setCreatorTool(creator);
    // PRODUCER
    dico.setProducer(producer);
    pdf.setProducer(producer);
    // CREATION DATE
    dico.setCreationDate(creationDate);
    xmp.setCreateDate(creationDate);
    // MODIFY DATE
    dico.setModificationDate(modifyDate);
    xmp.setModifyDate(modifyDate);
    // Launching synchronization test
    try {
        ve = sync.validateMetadataSynchronization(doc, metadata);
        // Checking all is synchronized
        Assert.assertEquals(0, ve.size());
    } catch (ValidationException e) {
        throw new Exception(e.getMessage());
    }
}
Also used : ValidationException(org.apache.pdfbox.preflight.exception.ValidationException) XMPBasicSchema(org.apache.xmpbox.schema.XMPBasicSchema) DublinCoreSchema(org.apache.xmpbox.schema.DublinCoreSchema) AdobePDFSchema(org.apache.xmpbox.schema.AdobePDFSchema) IOException(java.io.IOException) ValidationException(org.apache.pdfbox.preflight.exception.ValidationException) Test(org.junit.Test)

Example 4 with XMPBasicSchema

use of org.apache.xmpbox.schema.XMPBasicSchema in project pdfbox by apache.

the class TestSynchronizedMetadataValidation method testBadSizeOfArrays.

/**
 * in XMP, Subject and Author must be embedded in a single entry text array This function check the detection of
 * multiple entries for these properties
 *
 * @throws Exception
 */
@Test
public void testBadSizeOfArrays() throws Exception {
    // building temporary XMP metadata
    DublinCoreSchema dc = metadata.createAndAddDublinCoreSchema();
    AdobePDFSchema pdf = metadata.createAndAddAdobePDFSchema();
    XMPBasicSchema xmp = metadata.createAndAddXMPBasicSchema();
    // Writing info in XMP and Document Information dictionary
    // TITLE
    dico.setTitle("dicoTitle");
    dc.setTitle("x-default", "XMPTitle");
    // AUTHOR
    dico.setAuthor("dicoAuthor");
    dc.addCreator("XMPAuthor");
    dc.addCreator("2ndCreator");
    // SUBJECT
    dico.setSubject("dicoSubj");
    dc.addSubject("XMPSubj");
    dc.addSubject("2ndSubj");
    // KEYWORDS
    dico.setKeywords("DicoKeywords");
    pdf.setKeywords("XMPkeywords");
    // CREATOR
    dico.setCreator("DicoCreator");
    xmp.setCreatorTool("XMPCreator");
    // PRODUCER
    dico.setProducer("DicoProducer");
    pdf.setProducer("XMPProducer");
    // CREATION DATE
    dico.setCreationDate(Calendar.getInstance());
    GregorianCalendar XMPCreate = new GregorianCalendar(2008, 11, 05);
    xmp.setCreateDate(XMPCreate);
    // MODIFY DATE
    dico.setModificationDate(Calendar.getInstance());
    GregorianCalendar XMPModify = new GregorianCalendar(2009, 10, 15);
    xmp.setModifyDate(XMPModify);
    // Launching synchronization test
    try {
        ve = sync.validateMetadataSynchronization(doc, metadata);
        // Test unsychronized value
        Assert.assertEquals(8, ve.size());
    } catch (ValidationException e) {
        throw new Exception(e.getMessage());
    }
}
Also used : ValidationException(org.apache.pdfbox.preflight.exception.ValidationException) XMPBasicSchema(org.apache.xmpbox.schema.XMPBasicSchema) GregorianCalendar(java.util.GregorianCalendar) DublinCoreSchema(org.apache.xmpbox.schema.DublinCoreSchema) AdobePDFSchema(org.apache.xmpbox.schema.AdobePDFSchema) IOException(java.io.IOException) ValidationException(org.apache.pdfbox.preflight.exception.ValidationException) Test(org.junit.Test)

Example 5 with XMPBasicSchema

use of org.apache.xmpbox.schema.XMPBasicSchema in project pdfbox by apache.

the class TestSynchronizedMetadataValidation method testdoublePrefixSchemas.

/**
 * Check reaction when metadata are well-formed
 *
 * @throws Exception
 */
@Test
public void testdoublePrefixSchemas() throws Exception {
    title = "TITLE";
    author = "AUTHOR(S)";
    subject = "SUBJECTS";
    keywords = "KEYWORD(S)";
    creator = "CREATOR";
    producer = "PRODUCER";
    creationDate = Calendar.getInstance();
    modifyDate = Calendar.getInstance();
    // building temporary XMP metadata
    DublinCoreSchema dc = metadata.createAndAddDublinCoreSchema();
    DublinCoreSchema dc2 = new DublinCoreSchema(metadata, "dctest");
    metadata.addSchema(dc2);
    XMPBasicSchema xmp = metadata.createAndAddXMPBasicSchema();
    XMPBasicSchema xmp2 = new XMPBasicSchema(metadata, "xmptest");
    metadata.addSchema(xmp2);
    AdobePDFSchema pdf = metadata.createAndAddAdobePDFSchema();
    AdobePDFSchema pdf2 = new AdobePDFSchema(metadata, "pdftest");
    metadata.addSchema(pdf2);
    // write some temp info in 'false' schemas
    dc2.setCoverage("tmpcover");
    xmp2.setCreatorTool("tmpcreator");
    pdf2.setKeywords("tmpkeys");
    // Writing info in XMP and Document Information dictionary
    // TITLE
    dico.setTitle(title);
    dc.setTitle("x-default", title);
    // AUTHOR
    dico.setAuthor(author);
    dc.addCreator(author);
    // SUBJECT
    dico.setSubject(subject);
    dc.addDescription("x-default", subject);
    // KEYWORDS
    dico.setKeywords(keywords);
    pdf.setKeywords(keywords);
    // CREATOR
    dico.setCreator(creator);
    xmp.setCreatorTool(creator);
    // PRODUCER
    dico.setProducer(producer);
    pdf.setProducer(producer);
    // CREATION DATE
    dico.setCreationDate(creationDate);
    xmp.setCreateDate(creationDate);
    // MODIFY DATE
    dico.setModificationDate(modifyDate);
    xmp.setModifyDate(modifyDate);
    // Launching synchronization test
    try {
        ve = sync.validateMetadataSynchronization(doc, metadata);
        Assert.assertTrue(ve.isEmpty());
    } catch (ValidationException e) {
        throw new Exception(e.getMessage());
    }
}
Also used : ValidationException(org.apache.pdfbox.preflight.exception.ValidationException) XMPBasicSchema(org.apache.xmpbox.schema.XMPBasicSchema) DublinCoreSchema(org.apache.xmpbox.schema.DublinCoreSchema) AdobePDFSchema(org.apache.xmpbox.schema.AdobePDFSchema) IOException(java.io.IOException) ValidationException(org.apache.pdfbox.preflight.exception.ValidationException) Test(org.junit.Test)

Aggregations

XMPBasicSchema (org.apache.xmpbox.schema.XMPBasicSchema)15 DublinCoreSchema (org.apache.xmpbox.schema.DublinCoreSchema)13 AdobePDFSchema (org.apache.xmpbox.schema.AdobePDFSchema)11 Test (org.junit.Test)7 ValidationException (org.apache.pdfbox.preflight.exception.ValidationException)6 XMPMetadata (org.apache.xmpbox.XMPMetadata)6 IOException (java.io.IOException)5 PDDocumentInformation (org.apache.pdfbox.pdmodel.PDDocumentInformation)5 PDMetadata (org.apache.pdfbox.pdmodel.common.PDMetadata)5 GregorianCalendar (java.util.GregorianCalendar)4 PDDocumentCatalog (org.apache.pdfbox.pdmodel.PDDocumentCatalog)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 ValidationError (org.apache.pdfbox.preflight.ValidationResult.ValidationError)3 PDFAIdentificationSchema (org.apache.xmpbox.schema.PDFAIdentificationSchema)3 BadFieldValueException (org.apache.xmpbox.type.BadFieldValueException)3 DomXmpParser (org.apache.xmpbox.xml.DomXmpParser)3 XmpSerializer (org.apache.xmpbox.xml.XmpSerializer)3 File (java.io.File)2 Calendar (java.util.Calendar)2 PDDocument (org.apache.pdfbox.pdmodel.PDDocument)2