Example usage for com.lowagie.text.pdf PdfWriter setXmpMetadata

List of usage examples for com.lowagie.text.pdf PdfWriter setXmpMetadata

Introduction

In this page you can find the example usage for com.lowagie.text.pdf PdfWriter setXmpMetadata.

Prototype

public void setXmpMetadata(byte[] xmpMetadata) 

Source Link

Document

Use this method to set the XMP Metadata.

Usage

From source file:de.unigoettingen.sub.commons.contentlib.pdflib.PDFManager.java

License:Apache License

/**
 * Write xmp metadata./*from   ww w .jav  a2 s  .c o  m*/
 * 
 * @param inWriter the in writer
 */
private void writeXMPMetadata(PdfWriter inWriter) {
    ByteArrayOutputStream os = new ByteArrayOutputStream();

    try {
        XmpWriter xmp = new XmpWriter(os);
        XmpSchema dc = new DublinCoreSchema();

        // set DublinCore metadata
        if (this.subject != null) {
            dc.setProperty(DublinCoreSchema.SUBJECT, this.subject);
        }
        if (this.title != null) {
            dc.setProperty(DublinCoreSchema.TITLE, this.title);
        }
        if (this.author != null) {
            dc.setProperty(DublinCoreSchema.CREATOR, this.author);
        }

        // add the DublinCore Simple Metadata to the RDF container

        xmp.addRdfDescription(dc);

        PdfSchema pdf = new PdfSchema();
        // set keywords
        pdf.setProperty(PdfSchema.KEYWORDS, "Hello World, XMP, Metadata");

        // set the version; must be 1.4 for PDF/A
        pdf.setProperty(PdfSchema.VERSION, "1.4");
        xmp.addRdfDescription(pdf);

        xmp.close();
    } catch (IOException e) {
        LOGGER.error("error occured while writing xmp metadata", e);
    }
    inWriter.setXmpMetadata(os.toByteArray());
}

From source file:org.alchemy.core.AlcSession.java

License:Open Source License

/** Save the canvas to a single paged PDF file
 * /*from ww w  . j  a  va 2s  .  c o  m*/
 * @param file  The file object to save the pdf to
 * @return      True if save worked, otherwise false
 */
boolean saveSinglePdf(File file) {
    // Get the current 'real' size of the canvas without margins/borders
    java.awt.Rectangle bounds = Alchemy.canvas.getVisibleRect();
    //int singlePdfWidth = Alchemy.window.getWindowSize().width;
    //int singlePdfHeight = Alchemy.window.getWindowSize().height;
    com.lowagie.text.Document document = new com.lowagie.text.Document(
            new com.lowagie.text.Rectangle(bounds.width, bounds.height), 0, 0, 0, 0);
    System.out.println("Save Single Pdf Called: " + file.toString());
    boolean noError = true;

    try {

        PdfWriter singleWriter = PdfWriter.getInstance(document, new FileOutputStream(file));
        document.addTitle("Alchemy Session");
        document.addAuthor(USER_NAME);
        document.addCreator("Alchemy <http://al.chemy.org>");

        // Add metadata and open the document
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        XmpWriter xmp = new XmpWriter(os);
        PdfSchema pdf = new PdfSchema();
        pdf.setProperty(PdfSchema.KEYWORDS, "Alchemy <http://al.chemy.org>");
        //pdf.setProperty(PdfSchema.VERSION, "1.4");
        xmp.addRdfDescription(pdf);
        xmp.close();
        singleWriter.setXmpMetadata(os.toByteArray());

        // To avoid transparent colurs being converted from RGB>CMYK>RGB
        // We have to add everything to a transparency group
        PdfTransparencyGroup transGroup = new PdfTransparencyGroup();
        transGroup.put(PdfName.CS, PdfName.DEVICERGB);

        document.open();

        PdfContentByte cb = singleWriter.getDirectContent();
        PdfTemplate tp = cb.createTemplate(bounds.width, bounds.height);

        document.newPage();

        cb.getPdfWriter().setGroup(transGroup);
        // Make sure the color space is Device RGB
        cb.setDefaultColorspace(PdfName.CS, PdfName.DEVICERGB);

        // Draw into the template and add it to the PDF 
        Graphics2D g2pdf = tp.createGraphics(bounds.width, bounds.height);
        Alchemy.canvas.setGuide(false);
        Alchemy.canvas.vectorCanvas.paintComponent(g2pdf);
        Alchemy.canvas.setGuide(true);
        g2pdf.dispose();
        cb.addTemplate(tp, 0, 0);

    } catch (DocumentException ex) {
        System.err.println(ex);
        noError = false;
    } catch (IOException ex) {
        System.err.println(ex);
        noError = false;
    }

    document.close();

    return noError;
}