Example usage for org.apache.pdfbox.pdmodel PDDocumentCatalog setMetadata

List of usage examples for org.apache.pdfbox.pdmodel PDDocumentCatalog setMetadata

Introduction

In this page you can find the example usage for org.apache.pdfbox.pdmodel PDDocumentCatalog setMetadata.

Prototype

public void setMetadata(PDMetadata meta) 

Source Link

Document

Sets the metadata for this object.

Usage

From source file:io.konik.carriage.pdfbox.PDFBoxInvoiceAppender.java

License:Open Source License

private void setMetadata(PDDocument doc, AppendParameter appendParameter)
        throws IOException, TransformerException, BadFieldValueException, XmpSerializationException {
    Calendar now = Calendar.getInstance();
    PDDocumentCatalog catalog = doc.getDocumentCatalog();

    PDMetadata metadata = new PDMetadata(doc);
    catalog.setMetadata(metadata);

    XMPMetadata xmp = XMPMetadata.createXMPMetadata();
    PDFAIdentificationSchema pdfaid = new PDFAIdentificationSchema(xmp);
    pdfaid.setPart(Integer.valueOf(3));
    pdfaid.setConformance("B");
    xmp.addSchema(pdfaid);//from   w  ww. ja  v a  2s  .  co m

    DublinCoreSchema dublicCore = new DublinCoreSchema(xmp);
    xmp.addSchema(dublicCore);

    XMPBasicSchema basicSchema = new XMPBasicSchema(xmp);
    basicSchema.setCreatorTool(PRODUCER);
    basicSchema.setCreateDate(now);
    xmp.addSchema(basicSchema);

    PDDocumentInformation pdi = doc.getDocumentInformation();
    pdi.setModificationDate(now);
    pdi.setProducer(PRODUCER);
    pdi.setAuthor(getAuthor());
    doc.setDocumentInformation(pdi);

    AdobePDFSchema pdf = new AdobePDFSchema(xmp);
    pdf.setProducer(PRODUCER);
    xmp.addSchema(pdf);

    PDMarkInfo markinfo = new PDMarkInfo();
    markinfo.setMarked(true);
    doc.getDocumentCatalog().setMarkInfo(markinfo);

    xmp.addSchema(zfDefaultXmp.getPDFExtensionSchema());
    XMPSchemaZugferd1p0 zf = new XMPSchemaZugferd1p0(xmp);
    zf.setConformanceLevel(appendParameter.zugferdConformanceLevel());
    zf.setVersion(appendParameter.zugferdVersion());
    xmp.addSchema(zf);

    new XmpSerializer().serialize(xmp, metadata.createOutputStream(), true);
}

From source file:net.sf.jabref.logic.xmp.XMPUtil.java

License:Open Source License

/**
 * Try to write the given BibTexEntries as DublinCore XMP Schemas
 *
 * Existing DublinCore schemas in the document are removed
 *
 * @param document/*from  w  ww .  j  a  v  a2 s  .c  o m*/
 *            The pdf document to write to.
 * @param entries
 *            The BibTeX entries that are written as schemas
 * @param database
 *            maybenull An optional database which the given BibTeX entries
 *            belong to, which will be used to resolve strings. If the
 *            database is null the strings will not be resolved.
 * @throws IOException
 * @throws TransformerException
 */
private static void writeDublinCore(PDDocument document, Collection<BibEntry> entries, BibDatabase database)
        throws IOException, TransformerException {

    Collection<BibEntry> resolvedEntries;
    if (database == null) {
        resolvedEntries = entries;
    } else {
        resolvedEntries = database.resolveForStrings(entries, false);
    }

    PDDocumentCatalog catalog = document.getDocumentCatalog();
    PDMetadata metaRaw = catalog.getMetadata();

    XMPMetadata meta;
    if (metaRaw == null) {
        meta = new XMPMetadata();
    } else {
        meta = new XMPMetadata(XMLUtil.parse(metaRaw.createInputStream()));
    }

    // Remove all current Dublin-Core schemas
    List<XMPSchema> schemas = meta.getSchemasByNamespaceURI(XMPSchemaDublinCore.NAMESPACE);
    for (XMPSchema schema : schemas) {
        schema.getElement().getParentNode().removeChild(schema.getElement());
    }

    for (BibEntry entry : resolvedEntries) {
        XMPSchemaDublinCore dcSchema = new XMPSchemaDublinCore(meta);
        XMPUtil.writeToDCSchema(dcSchema, entry, null);
        meta.addSchema(dcSchema);
    }

    // Save to stream and then input that stream to the PDF
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    meta.save(os);
    ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
    PDMetadata metadataStream = new PDMetadata(document, is, false);
    catalog.setMetadata(metadataStream);
}

From source file:net.sf.jabref.logic.xmp.XMPUtil.java

License:Open Source License

/**
 * Try to write the given BibTexEntry in the XMP-stream of the given
 * PDF-file./*from w w w. j ava 2  s  .com*/
 *
 * Throws an IOException if the file cannot be read or written, so the user
 * can remove a lock or cancel the operation.
 *
 * The method will overwrite existing BibTeX-XMP-data, but keep other
 * existing metadata.
 *
 * @param file
 *            The file to write the entries to.
 * @param bibtexEntries
 *            The entries to write to the file. *
 * @param database
 *            maybenull An optional database which the given bibtex entries
 *            belong to, which will be used to resolve strings. If the
 *            database is null the strings will not be resolved.
 * @param writePDFInfo
 *            Write information also in PDF document properties
 * @throws TransformerException
 *             If the entry was malformed or unsupported.
 * @throws IOException
 *             If the file could not be written to or could not be found.
 */
public static void writeXMP(File file, Collection<BibEntry> bibtexEntries, BibDatabase database,
        boolean writePDFInfo) throws IOException, TransformerException {

    Collection<BibEntry> resolvedEntries;
    if (database == null) {
        resolvedEntries = bibtexEntries;
    } else {
        resolvedEntries = database.resolveForStrings(bibtexEntries, false);
    }

    try (PDDocument document = PDDocument.load(file.getAbsoluteFile())) {
        if (document.isEncrypted()) {
            throw new EncryptionNotSupportedException("Error: Cannot add metadata to encrypted document.");
        }

        if (writePDFInfo && (resolvedEntries.size() == 1)) {
            XMPUtil.writeDocumentInformation(document, resolvedEntries.iterator().next(), null);
            XMPUtil.writeDublinCore(document, resolvedEntries, null);
        }

        PDDocumentCatalog catalog = document.getDocumentCatalog();
        PDMetadata metaRaw = catalog.getMetadata();

        XMPMetadata meta;
        if (metaRaw == null) {
            meta = new XMPMetadata();
        } else {
            meta = new XMPMetadata(XMLUtil.parse(metaRaw.createInputStream()));
        }
        meta.addXMLNSMapping(XMPSchemaBibtex.NAMESPACE, XMPSchemaBibtex.class);

        // Remove all current Bibtex-schemas
        List<XMPSchema> schemas = meta.getSchemasByNamespaceURI(XMPSchemaBibtex.NAMESPACE);
        for (XMPSchema schema : schemas) {
            XMPSchemaBibtex bib = (XMPSchemaBibtex) schema;
            bib.getElement().getParentNode().removeChild(bib.getElement());
        }

        for (BibEntry e : resolvedEntries) {
            XMPSchemaBibtex bibtex = new XMPSchemaBibtex(meta);
            meta.addSchema(bibtex);
            bibtex.setBibtexEntry(e, null);
        }

        // Save to stream and then input that stream to the PDF
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        meta.save(os);
        ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
        PDMetadata metadataStream = new PDMetadata(document, is, false);
        catalog.setMetadata(metadataStream);

        // Save
        try {
            document.save(file.getAbsolutePath());
        } catch (COSVisitorException e) {
            throw new TransformerException("Could not write XMP-metadata: " + e.getLocalizedMessage());
        }

    }
}

From source file:net.sf.jabref.util.XMPUtil.java

License:Open Source License

/**
 * Try to write the given BibTexEntries as DublinCore XMP Schemas
 * //  w ww. j  a v  a  2  s. c  o m
 * Existing DublinCore schemas in the document are removed
 * 
 * @param document
 *            The pdf document to write to.
 * @param entries
 *            The Bibtex entries that are written as schemas
 * @param database
 *            maybenull An optional database which the given bibtex entries
 *            belong to, which will be used to resolve strings. If the
 *            database is null the strings will not be resolved.
 * @throws IOException
 * @throws TransformerException
 */
@SuppressWarnings("unchecked")
private static void writeDublinCore(PDDocument document, Collection<BibtexEntry> entries,
        BibtexDatabase database) throws IOException, TransformerException {

    if (database != null) {
        entries = database.resolveForStrings(entries, false);
    }

    PDDocumentCatalog catalog = document.getDocumentCatalog();
    PDMetadata metaRaw = catalog.getMetadata();

    XMPMetadata meta;
    if (metaRaw != null) {
        meta = new XMPMetadata(XMLUtil.parse(metaRaw.createInputStream()));
    } else {
        meta = new XMPMetadata();
    }

    // Remove all current Dublin-Core schemas
    List<XMPSchema> schemas = meta.getSchemasByNamespaceURI(XMPSchemaDublinCore.NAMESPACE);
    for (XMPSchema schema : schemas) {
        schema.getElement().getParentNode().removeChild(schema.getElement());
    }

    for (BibtexEntry entry : entries) {
        XMPSchemaDublinCore dcSchema = new XMPSchemaDublinCore(meta);
        XMPUtil.writeToDCSchema(dcSchema, entry, null);
        meta.addSchema(dcSchema);
    }

    // Save to stream and then input that stream to the PDF
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    meta.save(os);
    ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
    PDMetadata metadataStream = new PDMetadata(document, is, false);
    catalog.setMetadata(metadataStream);
}

From source file:net.sf.jabref.util.XMPUtil.java

License:Open Source License

/**
 * Try to write the given BibTexEntry in the XMP-stream of the given
 * PDF-file.//from  w w  w. j a va 2s.  c o  m
 * 
 * Throws an IOException if the file cannot be read or written, so the user
 * can remove a lock or cancel the operation.
 * 
 * The method will overwrite existing BibTeX-XMP-data, but keep other
 * existing metadata.
 * 
 * @param file
 *            The file to write the entries to.
 * @param bibtexEntries
 *            The entries to write to the file. *
 * @param database
 *            maybenull An optional database which the given bibtex entries
 *            belong to, which will be used to resolve strings. If the
 *            database is null the strings will not be resolved.
 * @param writePDFInfo
 *            Write information also in PDF document properties
 * @throws TransformerException
 *             If the entry was malformed or unsupported.
 * @throws IOException
 *             If the file could not be written to or could not be found.
 */
@SuppressWarnings("unchecked")
public static void writeXMP(File file, Collection<BibtexEntry> bibtexEntries, BibtexDatabase database,
        boolean writePDFInfo) throws IOException, TransformerException {

    if (database != null) {
        bibtexEntries = database.resolveForStrings(bibtexEntries, false);
    }

    PDDocument document = null;

    try {
        document = PDDocument.load(file.getAbsoluteFile());
        if (document.isEncrypted()) {
            throw new EncryptionNotSupportedException("Error: Cannot add metadata to encrypted document.");
        }

        if (writePDFInfo && (bibtexEntries.size() == 1)) {
            XMPUtil.writeDocumentInformation(document, bibtexEntries.iterator().next(), null);
            XMPUtil.writeDublinCore(document, bibtexEntries, null);
        }

        PDDocumentCatalog catalog = document.getDocumentCatalog();
        PDMetadata metaRaw = catalog.getMetadata();

        XMPMetadata meta;
        if (metaRaw != null) {
            meta = new XMPMetadata(XMLUtil.parse(metaRaw.createInputStream()));
        } else {
            meta = new XMPMetadata();
        }
        meta.addXMLNSMapping(XMPSchemaBibtex.NAMESPACE, XMPSchemaBibtex.class);

        // Remove all current Bibtex-schemas
        List<XMPSchema> schemas = meta.getSchemasByNamespaceURI(XMPSchemaBibtex.NAMESPACE);
        for (XMPSchema schema : schemas) {
            XMPSchemaBibtex bib = (XMPSchemaBibtex) schema;
            bib.getElement().getParentNode().removeChild(bib.getElement());
        }

        for (BibtexEntry e : bibtexEntries) {
            XMPSchemaBibtex bibtex = new XMPSchemaBibtex(meta);
            meta.addSchema(bibtex);
            bibtex.setBibtexEntry(e, null);
        }

        // Save to stream and then input that stream to the PDF
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        meta.save(os);
        ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
        PDMetadata metadataStream = new PDMetadata(document, is, false);
        catalog.setMetadata(metadataStream);

        // Save
        try {
            document.save(file.getAbsolutePath());
        } catch (COSVisitorException e) {
            throw new TransformerException("Could not write XMP-metadata: " + e.getLocalizedMessage());
        }

    } finally {
        if (document != null) {
            document.close();
        }
    }
}

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

License:Open Source License

protected void prepareDocument() throws IOException {
    String fullProducer = producer + " (via mustangproject.org " + org.mustangproject.ZUGFeRD.Version.VERSION
            + ")";

    PDDocumentCatalog cat = doc.getDocumentCatalog();
    metadata = new PDMetadata(doc);
    cat.setMetadata(metadata);

    xmp = XMPMetadata.createXMPMetadata();

    pdfaid = new PDFAIdentificationSchema(xmp);

    xmp.addSchema(pdfaid);//  w ww . j  a v a 2 s.  c  o m

    DublinCoreSchema dc = xmp.createAndAddDublinCoreSchema();

    dc.addCreator(creator);

    XMPBasicSchema xsb = xmp.createAndAddXMPBasicSchema();

    xsb.setCreatorTool(creatorTool);
    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);
    if (ensurePDFisUpgraded) {
        try {
            pdfaid.setConformance(conformanceLevel.getLetter());// $NON-NLS-1$ //$NON-NLS-1$
        } catch (BadFieldValueException ex) {
            // This should be impossible, because it would occur only if an illegal
            // conformance level is supplied,
            // however the enum enforces that the conformance level is valid.
            throw new Error(ex);
        }

        pdfaid.setPart(3);
    }
    addXMP(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);
    }
    documentPrepared = true;
}

From source file:org.olat.core.util.pdf.PdfDocument.java

License:Apache License

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);//from   w  w w  . j a  va2s  . co m
    info.setCreator(author);
    info.setCreationDate(date);
    info.setModificationDate(date);
    info.setTitle(title);
    info.setSubject(subject);

    XMPMetadata metadata = new XMPMetadata();
    XMPSchemaPDF pdfSchema = metadata.addPDFSchema();
    pdfSchema.setProducer("OpenOLAT");

    XMPSchemaBasic basicSchema = metadata.addBasicSchema();
    basicSchema.setModifyDate(date);
    basicSchema.setCreateDate(date);
    basicSchema.setCreatorTool("OpenOLAT");
    basicSchema.setMetadataDate(date);

    XMPSchemaDublinCore dcSchema = metadata.addDublinCoreSchema();
    dcSchema.setTitle(title);
    dcSchema.addCreator(author);
    dcSchema.setDescription(subject);

    PDMetadata metadataStream = new PDMetadata(document);
    metadataStream.importXMPMetadata(metadata);
    catalog.setMetadata(metadataStream);
}

From source file:org.xstudiosys.pdfxmp.AddMetadataFromDocInfo.java

License:Apache License

/**
 * This will print the documents data.//from w  w w  . j  ava2  s. c o m
 *
 * @param args The command line arguments.
 *
 * @throws Exception If there is an error parsing the document.
 */
public static void main(String[] args) throws Exception {
    if (args.length != 2) {
        usage();
    } else {
        PDDocument document = null;

        try {
            document = PDDocument.load(args[0]);
            if (document.isEncrypted()) {
                System.err.println("Error: Cannot add metadata to encrypted document.");
                System.exit(1);
            }
            PDDocumentCatalog catalog = document.getDocumentCatalog();
            PDDocumentInformation info = document.getDocumentInformation();

            XMPMetadata metadata = new XMPMetadata();

            XMPSchemaPDF pdfSchema = metadata.addPDFSchema();
            pdfSchema.setKeywords(info.getKeywords());
            pdfSchema.setProducer(info.getProducer());

            XMPSchemaBasic basicSchema = metadata.addBasicSchema();
            basicSchema.setModifyDate(info.getModificationDate());
            basicSchema.setCreateDate(info.getCreationDate());
            basicSchema.setCreatorTool(info.getCreator());
            basicSchema.setMetadataDate(new GregorianCalendar());

            XMPSchemaDublinCore dcSchema = metadata.addDublinCoreSchema();
            dcSchema.setTitle(info.getTitle());
            dcSchema.addCreator("PDFBox");
            dcSchema.setDescription(info.getSubject());

            PDMetadata metadataStream = new PDMetadata(document);
            metadataStream.importXMPMetadata(metadata);
            catalog.setMetadata(metadataStream);

            document.save(args[1]);
        } finally {
            if (document != null) {
                document.close();
            }
        }
    }
}

From source file:org.xstudiosys.pdfxmp.MarkBuilder.java

License:Open Source License

public void onComplete(PDDocument document) {
    try {/* w  ww.  jav a 2  s  .  c o  m*/

        PDDocumentCatalog catalog = document.getDocumentCatalog();
        PDDocumentInformation info = document.getDocumentInformation();

        XMPMetadata metadata = new XMPMetadata();

        XMPSchemaPDF pdfSchema = metadata.addPDFSchema();
        pdfSchema.setKeywords(info.getKeywords());
        pdfSchema.setProducer(info.getProducer());

        XMPSchemaBasic basicSchema = metadata.addBasicSchema();
        basicSchema.setModifyDate(info.getModificationDate());
        basicSchema.setCreateDate(info.getCreationDate());
        basicSchema.setCreatorTool(info.getCreator());
        basicSchema.setMetadataDate(new GregorianCalendar());

        XMPSchemaDublinCore dcSchema = metadata.addDublinCoreSchema();
        dcSchema.setTitle(info.getTitle());
        dcSchema.addCreator("PDFBox");
        dcSchema.setDescription(info.getSubject());

        PDMetadata metadataStream = new PDMetadata(document);
        metadataStream.importXMPMetadata(metadata);
        catalog.setMetadata(metadataStream);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.xstudiosys.pdfxmp.XMPUtil.java

License:Open Source License

/**
 * Try to write the given BibTexEntries as DublinCore XMP Schemas
 * /*from   w ww .j  a v a2 s.c om*/
 * Existing DublinCore schemas in the document are removed
 * 
 * @param document
 *            The pdf document to write to.
 * @param entries
 *            The Bibtex entries that are written as schemas
 * @param database
 *            maybenull An optional database which the given bibtex entries
 *            belong to, which will be used to resolve strings. If the
 *            database is null the strings will not be resolved.
 * @throws IOException
 * @throws TransformerException
 */
@SuppressWarnings("unchecked")
public static void writeDublinCore(PDDocument document, Collection<BibtexEntry> entries,
        BibtexDatabase database) throws IOException, TransformerException {

    if (database != null)
        entries = database.resolveForStrings(entries, false);

    PDDocumentCatalog catalog = document.getDocumentCatalog();
    PDMetadata metaRaw = catalog.getMetadata();

    XMPMetadata meta;
    if (metaRaw != null) {
        meta = new XMPMetadata(XMLUtil.parse(metaRaw.createInputStream()));
    } else {
        meta = new XMPMetadata();
    }

    // Remove all current Dublin-Core schemas
    List<XMPSchema> schemas = meta.getSchemasByNamespaceURI(XMPSchemaDublinCore.NAMESPACE);
    for (XMPSchema schema : schemas) {
        schema.getElement().getParentNode().removeChild(schema.getElement());
    }

    for (BibtexEntry entry : entries) {
        XMPSchemaDublinCore dcSchema = new XMPSchemaDublinCore(meta);
        writeToDCSchema(dcSchema, entry, null);
        meta.addSchema(dcSchema);
    }

    // Save to stream and then input that stream to the PDF
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    meta.save(os);
    ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
    PDMetadata metadataStream = new PDMetadata(document, is, false);
    catalog.setMetadata(metadataStream);
}