Ning Zhao wrote:
Hi!
thank you very much for the help! Yes, the PdfStamper stamper worked,
there is really modified xmp metadata in the output pdf file. But the
problem is, the old xmp metadata still exists. Now there are two blocks
of xmp metadata in the output pdf file. Now to fix this problem?
I've made a small standalone example for testing (see attachment)
and you are right. I've tried fixing it, but every solution I've
found so far could break other functionality: for instance: I can't
just remove that stream if it's referred to from a page (pages can
have a Metadata stream to).
br,
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.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();
alterXmp();
}
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 alterXmp() {
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_altered.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