Ning Zhao wrote:
P.S.: Just bought the book "iText in action" and reading Chapter 18.

I thought of replacing the XMP stream at the lower level too:
with PRStream.setData(). The problem you'll experience is that
setData() compresses the stream by default. That's typically not
what you want to happen with an XMP stream. I've adapted the
example I've sent before to work around this problem (look at
the lines where I change the value of Document.compress). That's
not a very elegant solution. Maybe I'll provide an extra setData
method that takes a boolean to let the developer decide whether
or not the data has to be compressed.
best regards,
Bruno
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PRStream;
import com.lowagie.text.pdf.PdfDictionary;
import com.lowagie.text.pdf.PdfName;
import com.lowagie.text.pdf.PdfObject;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.xml.xmp.DublinCoreSchema;
import com.lowagie.text.xml.xmp.PdfSchema;
import com.lowagie.text.xml.xmp.XmpArray;
import com.lowagie.text.xml.xmp.XmpSchema;
import com.lowagie.text.xml.xmp.XmpWriter;

public class XmpMetadataTest {

        /**
         * Generates a PDF, RTF and HTML file with the text 'Hello World' with 
some
         * metadata.
         * 
         * @param args
         *            no arguments needed here
         */
        public static void main(String[] args) {
                createOriginal();
                alterXmp1();
                alterXmp2();
        }
        
        public static void createOriginal() {
                // step 1: creation of a document-object
                Document document = new Document();
                try {
                        // step 2:
                        PdfWriter writer = PdfWriter.getInstance(
                        // that listens to the document
                                        document,
                                        // and directs a PDF-stream to a file
                                        new 
FileOutputStream("xmp_original.pdf"));
                        // step 3: we add metadata and open the document
                        ByteArrayOutputStream os = new ByteArrayOutputStream();
                        XmpWriter xmp = new XmpWriter(os);
                        XmpSchema dc = new DublinCoreSchema();
                        XmpArray subject = new XmpArray(XmpArray.UNORDERED);
                        subject.add("Hello World");
                        subject.add("XMP & Metadata");
                        subject.add("Metadata");
                        dc.setProperty(DublinCoreSchema.SUBJECT, subject);
                        xmp.addRdfDescription(dc);
                        PdfSchema pdf = new PdfSchema();
                        pdf.setProperty(PdfSchema.KEYWORDS, "Hello World, XMP, 
Metadata");
                        pdf.setProperty(PdfSchema.VERSION, "1.4");
                        xmp.addRdfDescription(pdf);
                        xmp.close();
                        writer.setXmpMetadata(os.toByteArray());
                        document.open();
                        // step 4: we add a paragraph to the document
                        document.add(new Paragraph("Hello World"));
                } catch (DocumentException de) {
                        System.err.println(de.getMessage());
                } catch (IOException ioe) {
                        System.err.println(ioe.getMessage());
                }

                // step 5: we close the document
                document.close();
        }
        
        public static void alterXmp1() {
                PdfReader reader;
                try {
                        reader = new PdfReader("xmp_original.pdf");
                        PdfDictionary catalog = reader.getCatalog();
                        PdfObject obj = catalog.get(PdfName.METADATA);
                        PRStream stream = (PRStream)PdfReader.getPdfObject(obj);
                        String metadata = new 
String(PdfReader.getStreamBytes(stream));
                        metadata = metadata.replaceAll("Hello World", "Hello 
Universe");
                        Document.compress = false;
                        stream.setData(metadata.getBytes());
                        Document.compress = true;
                        PdfStamper stamper = new PdfStamper(reader, new 
FileOutputStream("xmp_altered1.pdf"));
                        stamper.close();
                } catch (IOException e) {
                        e.printStackTrace();
                } catch (DocumentException e) {
                        e.printStackTrace();
                }
        }
        
        public static void alterXmp2() {
                PdfReader reader;
                try {
                        reader = new PdfReader("xmp_original.pdf");
                        String metadata = new String(reader.getMetadata());
                        metadata = metadata.replaceAll("Hello World", "Hello 
Universe");
                        PdfStamper stamper = new PdfStamper(reader, new 
FileOutputStream("xmp_altered2.pdf"));
                        stamper.setXmpMetadata(metadata.getBytes());
                        stamper.close();
                } catch (IOException e) {
                        e.printStackTrace();
                } catch (DocumentException e) {
                        e.printStackTrace();
                }
        }
}
-------------------------------------------------------------------------
This SF.net email is sponsored by the 2008 JavaOne(SM) Conference 
Don't miss this year's exciting event. There's still time to save $100. 
Use priority code J8TL2D2. 
http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone
_______________________________________________
iText-questions mailing list
iText-questions@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/itext-questions

Do you like iText?
Buy the iText book: http://www.1t3xt.com/docs/book.php
Or leave a tip: https://tipit.to/itexttipjar

Reply via email to