Search in sources :

Example 6 with PDFAIdentificationSchema

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

the class ZUGFeRDExporter method makeDocPDFA3compliant.

private PDDocumentCatalog makeDocPDFA3compliant(String producer, String creator, boolean attachZugferdHeaders) throws IOException, TransformerException {
    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) {
        // supplied, 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
								 */
    }
    XmpSerializer serializer = new XmpSerializer();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    serializer.serialize(xmp, baos, false);
    metadata.importXMPMetadata(baos.toByteArray());
    return cat;
}
Also used : BadFieldValueException(org.apache.xmpbox.type.BadFieldValueException) XmpSerializer(org.apache.xmpbox.xml.XmpSerializer) XMPBasicSchema(org.apache.xmpbox.schema.XMPBasicSchema) PDMetadata(org.apache.pdfbox.pdmodel.common.PDMetadata) ByteArrayOutputStream(java.io.ByteArrayOutputStream) 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)

Example 7 with PDFAIdentificationSchema

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

the class CreatePDFA method main.

public static void main(String[] args) throws IOException, TransformerException {
    if (args.length != 3) {
        System.err.println("usage: " + CreatePDFA.class.getName() + " <output-file> <Message> <ttf-file>");
        System.exit(1);
    }
    String file = args[0];
    String message = args[1];
    String fontfile = args[2];
    try (PDDocument doc = new PDDocument()) {
        PDPage page = new PDPage();
        doc.addPage(page);
        // load the font as this needs to be embedded
        PDFont font = PDType0Font.load(doc, new File(fontfile));
        // 
        if (!font.isEmbedded()) {
            throw new IllegalStateException("PDF/A compliance requires that all fonts used for" + " text rendering in rendering modes other than rendering mode 3 are embedded.");
        }
        // create a page with the message
        try (PDPageContentStream contents = new PDPageContentStream(doc, page)) {
            contents.beginText();
            contents.setFont(font, 12);
            contents.newLineAtOffset(100, 700);
            contents.showText(message);
            contents.endText();
        }
        // add XMP metadata
        XMPMetadata xmp = XMPMetadata.createXMPMetadata();
        try {
            DublinCoreSchema dc = xmp.createAndAddDublinCoreSchema();
            dc.setTitle(file);
            PDFAIdentificationSchema id = xmp.createAndAddPFAIdentificationSchema();
            id.setPart(1);
            id.setConformance("B");
            XmpSerializer serializer = new XmpSerializer();
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            serializer.serialize(xmp, baos, true);
            PDMetadata metadata = new PDMetadata(doc);
            metadata.importXMPMetadata(baos.toByteArray());
            doc.getDocumentCatalog().setMetadata(metadata);
        } catch (BadFieldValueException e) {
            // won't happen here, as the provided value is valid
            throw new IllegalArgumentException(e);
        }
        // sRGB output intent
        InputStream colorProfile = CreatePDFA.class.getResourceAsStream("/org/apache/pdfbox/resources/pdfa/sRGB.icc");
        PDOutputIntent intent = new PDOutputIntent(doc, colorProfile);
        intent.setInfo("sRGB IEC61966-2.1");
        intent.setOutputCondition("sRGB IEC61966-2.1");
        intent.setOutputConditionIdentifier("sRGB IEC61966-2.1");
        intent.setRegistryName("http://www.color.org");
        doc.getDocumentCatalog().addOutputIntent(intent);
        doc.save(file);
    }
}
Also used : PDFont(org.apache.pdfbox.pdmodel.font.PDFont) BadFieldValueException(org.apache.xmpbox.type.BadFieldValueException) PDPage(org.apache.pdfbox.pdmodel.PDPage) XmpSerializer(org.apache.xmpbox.xml.XmpSerializer) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PDMetadata(org.apache.pdfbox.pdmodel.common.PDMetadata) PDOutputIntent(org.apache.pdfbox.pdmodel.graphics.color.PDOutputIntent) PDFAIdentificationSchema(org.apache.xmpbox.schema.PDFAIdentificationSchema) XMPMetadata(org.apache.xmpbox.XMPMetadata) PDDocument(org.apache.pdfbox.pdmodel.PDDocument) PDPageContentStream(org.apache.pdfbox.pdmodel.PDPageContentStream) DublinCoreSchema(org.apache.xmpbox.schema.DublinCoreSchema) File(java.io.File)

Example 8 with PDFAIdentificationSchema

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

the class PDFAIdentificationValidation method validatePDFAIdentifer.

/**
 * Check if PDFAIdentification is valid
 *
 * @param metadata the XMP MetaData.
 * @return the list of validation errors.
 * @throws ValidationException
 */
public List<ValidationError> validatePDFAIdentifer(XMPMetadata metadata) throws ValidationException {
    List<ValidationError> ve = new ArrayList<>();
    PDFAIdentificationSchema id = metadata.getPDFIdentificationSchema();
    if (id == null) {
        ve.add(new ValidationError(ERROR_METADATA_PDFA_ID_MISSING, "PDF/A identification schema " + PDFAIdentificationSchema.class.getAnnotation(StructuredType.class).namespace() + " is missing"));
        return ve;
    }
    // According to the PDF/A specification, the prefix must be pdfaid for this schema.
    StructuredType stBasic = XMPBasicSchema.class.getAnnotation(StructuredType.class);
    StructuredType stPdfaIdent = PDFAIdentificationSchema.class.getAnnotation(StructuredType.class);
    if (!id.getPrefix().equals(stPdfaIdent.preferedPrefix())) {
        if (metadata.getSchema(stPdfaIdent.preferedPrefix(), stBasic.namespace()) == null) {
            ve.add(unexpectedPrefixFoundError(id.getPrefix(), stPdfaIdent.preferedPrefix(), PDFAIdentificationSchema.class.getName()));
        } else {
            id = (PDFAIdentificationSchema) metadata.getSchema(stPdfaIdent.preferedPrefix(), stPdfaIdent.namespace());
        }
    }
    checkConformanceLevel(ve, id.getConformance());
    checkPartNumber(ve, id.getPart() == null ? -1 : id.getPart());
    return ve;
}
Also used : PDFAIdentificationSchema(org.apache.xmpbox.schema.PDFAIdentificationSchema) ArrayList(java.util.ArrayList) ValidationError(org.apache.pdfbox.preflight.ValidationResult.ValidationError) StructuredType(org.apache.xmpbox.type.StructuredType)

Example 9 with PDFAIdentificationSchema

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

the class PDFMergerExample method createXMPMetadata.

private PDMetadata createXMPMetadata(COSStream cosStream, String title, String creator, String subject) throws BadFieldValueException, TransformerException, IOException {
    LOG.info("Setting XMP metadata (title, author, subject) for merged PDF");
    XMPMetadata xmpMetadata = XMPMetadata.createXMPMetadata();
    // PDF/A-1b properties
    PDFAIdentificationSchema pdfaSchema = xmpMetadata.createAndAddPFAIdentificationSchema();
    pdfaSchema.setPart(1);
    pdfaSchema.setConformance("B");
    // Dublin Core properties
    DublinCoreSchema dublinCoreSchema = xmpMetadata.createAndAddDublinCoreSchema();
    dublinCoreSchema.setTitle(title);
    dublinCoreSchema.addCreator(creator);
    dublinCoreSchema.setDescription(subject);
    // XMP Basic properties
    XMPBasicSchema basicSchema = xmpMetadata.createAndAddXMPBasicSchema();
    Calendar creationDate = Calendar.getInstance();
    basicSchema.setCreateDate(creationDate);
    basicSchema.setModifyDate(creationDate);
    basicSchema.setMetadataDate(creationDate);
    basicSchema.setCreatorTool(creator);
    // Create and return XMP data structure in XML format
    try (ByteArrayOutputStream xmpOutputStream = new ByteArrayOutputStream();
        OutputStream cosXMPStream = cosStream.createOutputStream()) {
        new XmpSerializer().serialize(xmpMetadata, xmpOutputStream, true);
        cosXMPStream.write(xmpOutputStream.toByteArray());
        return new PDMetadata(cosStream);
    }
}
Also used : PDFAIdentificationSchema(org.apache.xmpbox.schema.PDFAIdentificationSchema) XmpSerializer(org.apache.xmpbox.xml.XmpSerializer) XMPMetadata(org.apache.xmpbox.XMPMetadata) Calendar(java.util.Calendar) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) XMPBasicSchema(org.apache.xmpbox.schema.XMPBasicSchema) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PDMetadata(org.apache.pdfbox.pdmodel.common.PDMetadata) DublinCoreSchema(org.apache.xmpbox.schema.DublinCoreSchema)

Example 10 with PDFAIdentificationSchema

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

the class MustangReaderWriterEdgeTest method testEdgeExportIsPDFA3.

/**
 * This test is based on #testEdgeExport but test only that the PDFIdentificationSchema is present and identifies
 * the pdf as PDF/A-3 conformant.
 */
public void testEdgeExportIsPDFA3() throws Exception {
    final String SOURCE_PDF = "/MustangGnuaccountingBeispielRE-20170509_505PDFA3.pdf";
    final String TARGET_PDF = "./target/testout-MustangGnuaccountingBeispielRE-20170509_505newEdge.pdf";
    // the writing part
    setupPdfUnderTest(SOURCE_PDF, TARGET_PDF);
    // now check the contents
    PDDocument doc = PDDocument.load(new File(TARGET_PDF));
    PDMetadata meta = doc.getDocumentCatalog().getMetadata();
    assertNotNull("The pdf must contain XMPMetadata", meta);
    byte[] xmpBytes = meta.toByteArray();
    assertNotNull("The xmp metadata stream must not be null", xmpBytes);
    DomXmpParser xmpParser = new DomXmpParser();
    XMPMetadata xmp = xmpParser.parse(xmpBytes);
    PDFAIdentificationSchema pdfai = xmp.getPDFIdentificationSchema();
    assertNotNull("The pdf must contain a PDFIdentificationSchema", pdfai);
    assertTrue("The PDF/A conformance must be A, B or U", Arrays.asList("A", "B", "U", "a", "b", "u").contains(pdfai.getConformance()));
    assertEquals("The PDF/A must be 3", (Integer) 3, pdfai.getPart());
}
Also used : PDFAIdentificationSchema(org.apache.xmpbox.schema.PDFAIdentificationSchema) XMPMetadata(org.apache.xmpbox.XMPMetadata) DomXmpParser(org.apache.xmpbox.xml.DomXmpParser) PDDocument(org.apache.pdfbox.pdmodel.PDDocument) PDMetadata(org.apache.pdfbox.pdmodel.common.PDMetadata) File(java.io.File)

Aggregations

PDFAIdentificationSchema (org.apache.xmpbox.schema.PDFAIdentificationSchema)10 PDMetadata (org.apache.pdfbox.pdmodel.common.PDMetadata)6 XMPMetadata (org.apache.xmpbox.XMPMetadata)6 BadFieldValueException (org.apache.xmpbox.type.BadFieldValueException)5 DublinCoreSchema (org.apache.xmpbox.schema.DublinCoreSchema)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 PDDocument (org.apache.pdfbox.pdmodel.PDDocument)3 XMPBasicSchema (org.apache.xmpbox.schema.XMPBasicSchema)3 XmpSerializer (org.apache.xmpbox.xml.XmpSerializer)3 File (java.io.File)2 InputStream (java.io.InputStream)2 PDDocumentCatalog (org.apache.pdfbox.pdmodel.PDDocumentCatalog)2 PDDocumentInformation (org.apache.pdfbox.pdmodel.PDDocumentInformation)2 AdobePDFSchema (org.apache.xmpbox.schema.AdobePDFSchema)2 DomXmpParser (org.apache.xmpbox.xml.DomXmpParser)2 OutputStream (java.io.OutputStream)1 ArrayList (java.util.ArrayList)1 Calendar (java.util.Calendar)1 TransformerException (javax.xml.transform.TransformerException)1 PDPage (org.apache.pdfbox.pdmodel.PDPage)1