Search in sources :

Example 16 with XMPBasicSchema

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

the class DeserializationTest method testWihtAttributesAsProperties.

@Test
void testWihtAttributesAsProperties() throws Exception {
    InputStream fis = DomXmpParser.class.getResourceAsStream("/validxmp/attr_as_props.xml");
    DomXmpParser xdb = new DomXmpParser();
    XMPMetadata meta = xdb.parse(fis);
    AdobePDFSchema pdf = meta.getAdobePDFSchema();
    assertEquals("GPL Ghostscript 8.64", pdf.getProducer());
    DublinCoreSchema dc = meta.getDublinCoreSchema();
    assertEquals("application/pdf", dc.getFormat());
    XMPBasicSchema basic = meta.getXMPBasicSchema();
    assertNotNull(basic.getCreateDate());
}
Also used : XMPMetadata(org.apache.xmpbox.XMPMetadata) InputStream(java.io.InputStream) DomXmpParser(org.apache.xmpbox.xml.DomXmpParser) XMPBasicSchema(org.apache.xmpbox.schema.XMPBasicSchema) AdobePDFSchema(org.apache.xmpbox.schema.AdobePDFSchema) DublinCoreSchema(org.apache.xmpbox.schema.DublinCoreSchema) Test(org.junit.jupiter.api.Test)

Example 17 with XMPBasicSchema

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

the class MetadataValidationProcess method checkThumbnails.

// check thumbnails. See Bavaria Testsuite file PDFA_Conference_2009_nc.pdf for an example.
private void checkThumbnails(PreflightContext ctx, XMPMetadata metadata) {
    XMPBasicSchema xmp = metadata.getXMPBasicSchema();
    if (xmp == null) {
        return;
    }
    List<ThumbnailType> tbProp;
    try {
        tbProp = xmp.getThumbnailsProperty();
    } catch (BadFieldValueException e) {
        // should not happen here because it would have happened in XmpParser already
        addValidationError(ctx, new ValidationError(PreflightConstants.ERROR_METADATA_FORMAT, e.getMessage(), e));
        return;
    }
    if (tbProp == null) {
        return;
    }
    tbProp.forEach(tb -> checkThumbnail(tb, ctx));
}
Also used : BadFieldValueException(org.apache.xmpbox.type.BadFieldValueException) XMPBasicSchema(org.apache.xmpbox.schema.XMPBasicSchema) ValidationError(org.apache.pdfbox.preflight.ValidationResult.ValidationError) ThumbnailType(org.apache.xmpbox.type.ThumbnailType)

Example 18 with XMPBasicSchema

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

the class PdfDocument method addMetadata.

public void addMetadata(String title, String subject, String author) throws IOException, TransformerException {
    PDDocumentCatalog catalog = document.getDocumentCatalog();
    PDDocumentInformation info = document.getDocumentInformation();
    Calendar date = Calendar.getInstance();
    info.setAuthor(author);
    info.setCreator(author);
    info.setCreationDate(date);
    info.setModificationDate(date);
    info.setTitle(title);
    info.setSubject(subject);
    XMPMetadata metadata = XMPMetadata.createXMPMetadata();
    AdobePDFSchema pdfSchema = metadata.createAndAddAdobePDFSchema();
    pdfSchema.setProducer("OpenOLAT");
    XMPBasicSchema basicSchema = metadata.createAndAddXMPBasicSchema();
    basicSchema.setModifyDate(date);
    basicSchema.setCreateDate(date);
    basicSchema.setCreatorTool("OpenOLAT");
    basicSchema.setMetadataDate(date);
    DublinCoreSchema dcSchema = metadata.createAndAddDublinCoreSchema();
    dcSchema.setTitle(title);
    dcSchema.addCreator(author);
    dcSchema.setDescription(subject);
    PDMetadata metadataStream = new PDMetadata(document);
    // Create and return XMP data structure in XML format
    try (ByteArrayOutputStream xmpOutputStream = new ByteArrayOutputStream()) {
        new XmpSerializer().serialize(metadata, xmpOutputStream, true);
        metadataStream.importXMPMetadata(xmpOutputStream.toByteArray());
    } catch (IOException e) {
        log.error("", e);
    }
    catalog.setMetadata(metadataStream);
}
Also used : XmpSerializer(org.apache.xmpbox.xml.XmpSerializer) XMPMetadata(org.apache.xmpbox.XMPMetadata) Calendar(java.util.Calendar) XMPBasicSchema(org.apache.xmpbox.schema.XMPBasicSchema) PDMetadata(org.apache.pdfbox.pdmodel.common.PDMetadata) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) AdobePDFSchema(org.apache.xmpbox.schema.AdobePDFSchema) DublinCoreSchema(org.apache.xmpbox.schema.DublinCoreSchema) PDDocumentInformation(org.apache.pdfbox.pdmodel.PDDocumentInformation) PDDocumentCatalog(org.apache.pdfbox.pdmodel.PDDocumentCatalog)

Example 19 with XMPBasicSchema

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

the class PdfboxSigner method setDocumentInfo.

private PDDocument setDocumentInfo(PDDocument document, DocumentInfo docInfo) throws TransformerException, IOException {
    PDDocument outDocument = null;
    if (docInfo != null) {
        PDDocumentInformation info = document.getDocumentInformation();
        // Change document metadata
        if (docInfo.getAuthor() != null)
            info.setAuthor(docInfo.getAuthor());
        if (docInfo.getTitle() != null)
            info.setTitle(docInfo.getTitle());
        if (docInfo.getSubject() != null)
            info.setSubject(docInfo.getSubject());
        if (docInfo.getKeywords() != null)
            info.setKeywords(docInfo.getKeywords());
        document.setDocumentInformation(info);
        XMPMetadata metadata = XMPMetadata.createXMPMetadata();
        AdobePDFSchema pdfSchema = metadata.createAndAddAdobePDFSchema();
        if (info.getKeywords() != null)
            pdfSchema.setKeywords(info.getKeywords());
        if (info.getProducer() != null)
            pdfSchema.setProducer(info.getProducer());
        XMPBasicSchema basicSchema = metadata.createAndAddXMPBasicSchema();
        if (info.getModificationDate() != null)
            basicSchema.setModifyDate(info.getModificationDate());
        if (info.getCreationDate() != null)
            basicSchema.setCreateDate(info.getCreationDate());
        if (info.getCreator() != null)
            basicSchema.setCreatorTool(info.getCreator());
        if (info.getCreationDate() != null)
            basicSchema.setMetadataDate(info.getCreationDate());
        DublinCoreSchema dcSchema = metadata.createAndAddDublinCoreSchema();
        if (info.getTitle() != null)
            dcSchema.setTitle(info.getTitle());
        if (info.getAuthor() != null)
            dcSchema.addCreator(info.getAuthor());
        if (info.getSubject() != null)
            dcSchema.setDescription(info.getSubject());
        PDMetadata metadataStream = new PDMetadata(document);
        PDDocumentCatalog catalog = document.getDocumentCatalog();
        catalog.setMetadata(metadataStream);
        XmpSerializer serializer = new XmpSerializer();
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        serializer.serialize(metadata, out, false);
        metadataStream.importXMPMetadata(out.toByteArray());
        // Save all document & reload is needed.
        File tmpFile = File.createTempFile("metadata", ".pdf");
        try (FileOutputStream fos = new FileOutputStream(tmpFile);
            FileInputStream fis = new FileInputStream(tmpFile)) {
            document.save(fos);
            outDocument = PDDocument.load(fis);
            document.close();
        } finally {
            tmpFile.delete();
        }
    }
    return outDocument;
}
Also used : 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) FileInputStream(java.io.FileInputStream) XMPMetadata(org.apache.xmpbox.XMPMetadata) PDDocument(org.apache.pdfbox.pdmodel.PDDocument) FileOutputStream(java.io.FileOutputStream) AdobePDFSchema(org.apache.xmpbox.schema.AdobePDFSchema) DublinCoreSchema(org.apache.xmpbox.schema.DublinCoreSchema) PDDocumentInformation(org.apache.pdfbox.pdmodel.PDDocumentInformation) File(java.io.File)

Aggregations

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