Bruno,

Your example is incomplete.  XMP is based on RFD and it is more
convoluted than in your example.  

To extract the XMP metadata from a document:

FileInputStream fis = new FileInputStream(fileName);
com.lowagie.text.pdf.PdfReader pdfReader = new
com.lowagie.text.pdf.PdfReader(fis);
byte meta[] = pdfReader.getMetadata();

The XMP packet in the byte array is composed of XMP headers and RDF
data.  I don't know at this point of any Java XMP parser that handles
both.  My solution is to update the RFD data with an RFD parser and to
add the XMP headers like this:

Byte rfdData[] = ...
Static String EXTRASPACE = 
"
\n"; // XMP has padding see specs.
            
String buf1 = new String(meta, "UTF-8");
StringBuffer result = new StringBuffer(rfdData.length + 100);
result.append("<?xpacket begin='\uFEFF' id='W5M0MpCehiHzreSzNTczkc9d'
?>");
result.append("<?adobe-xap-filters esc=\"CRLF\"?>\n");
result.append("<x:xmpmeta xmlns:x='adobe:ns:meta/'>\n");
result.append(buf1);
result.append("</x:xmpmeta>\n");
int padd = 7;
for (int i = 0 ;i < padd; i++)
   result.append(EXTRASPACE);

result.append("<?xpacket ends='w'?>\n");
rfdData = result.toString().getBytes("UTF-8");

To add the metadata to a PdfWriter instance:

pdfWriter.setXmpMetadata(rfdData);

When creating new XMP data from scratch, create the RFD data first, add
the XMP headers and then set the result metadata to a PdfWriter.

RFD parsers are off topic for this email list.

I hope the above code helps.  Some of this code appears in
com.lowagie.text.xml.xmp.XmpWriter, but the code works only when
creating XMP from scratch.  Check the source code.  XmpWriter does not
parse or update existing metadata.

------------
Eli Segev



-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of bruno
Sent: Tuesday, March 14, 2006 2:46 AM
To: Phil Poupart
Cc: [email protected]
Subject: Re: [iText-questions] Add XMP tags to PDF

Leonard Rosenthol wrote:

> At 03:56 PM 3/13/2006, Phil Poupart wrote:
>
>> There has to be a simple way to add XMP tags to an existing PDF.
>
>
>         Depends on your definition of simple...
>
>         But basically, you get the XMP data from the PDF via iText.  
> You use you favorite XML technology to modify the XMP.   Then you put 
> the XMP back via iText.

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);

and so on...
Create any XmpSchema implementation you want and
add it to the XmpWriter, then close the XmpWriter:

xmp.close();
writer.setXmpMetadata(os.toByteArray());

I don't see why people think this is difficult?
br,
Bruno


-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting
language
that extends applications into web and mobile media. Attend the live
webcast
and join the prime developer group breaking into this new coding
territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions


-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language
that extends applications into web and mobile media. Attend the live webcast
and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid0944&bid$1720&dat1642
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions

Reply via email to