I recently used iText for what may seem a strange purpose. I had a PDF
that I did not author with 70+ Flash videos that were linked via HTTP,
rather than embedded. I wanted to update the PDF so that the FLVs were
either on the local file system or embedded in the PDF. The reason for
this was to create an offline copy of the PDF in question, where the
videos would play without network access. It seems most iText
documentation is about creating PDFs rather than modifying existing
ones, so I thought I would share this. I use the Adobe syntax for
PdfNames.

0. (Usual stuff - create PdfReader, PdfStamper from reader, get
PdfWriter from stamper)
1. For each page in the PDF get all /Annots where /Subtype = /RichMedia
2. For Rich Media annotation, follow the dictionary path
/RichMediaSettings->/Activation->/Configuration to an array call
/Instances
3. For each instance(all examples I encountered only had one instance
per annotation) get the dictionary /Params and the key /FlashVars
4. /FlashVars, if you read the documentation, it is the parameters
passed to the embedded Flash video player. In my case, it looked like
this: 
"source=http://....s3.amazonws.com/path/to/file.flv&param1=XXX&param2=YYY....";
etc. It looks exactly like something that would get as arguments in a
URL.
5. I used regular expressions to pull apart the source argument in
FlashVars. I downloaded the indicated file to the local system. (I
actually did this ahead of time using wget). Create a "shortened name"
of the file. I didn't check the PDF spec, but probably it can't
contain slashes, etc. I changed all slashes to hyphens, and removed
the http:// from the source argument. I suppose you could just hash
the filepath, or the file itself and use that as a shortname. Whatever
you use, keep track of the short name.
6. Create a PdfFileSpecification using the shortened name, path to the
file and add it to the writer via addFileAttachment. Make sure you
save the value of getReference of the PdfFileSpecifcation and save it
for later. I acutally created a Map<String, PdfIndirectReference> of
embedded files in case any links were reused.
7. Change the /FlashVars value you got above to
"source=<shortname>...rest of orginal arguments". At this point, we
have changed the arguments to the Flash video player to reference an
embedded file. Because of the sandbox settings however, we are not
done yet. The Flash video player will not access the embedded file
without the following steps.
8. Back on the original Annotation object, get the dictionary path
/RichMediaContent->/Assets to the "array" called /Names. /Name is
actually a heterogeneous array. It consists of a string followed by an
object references. The string value is a "filename" that the Flash VM
can access and the reference is to the file object.
9. Add a PdfString with the file shortname (the one used as a argument
to PdfFileSpecification and that we modified the source parameter to
point to) to the /Names array
10. Add a reference to the PdfFileSpecification for this asset to the
/Names array.
(Usual stuff - close the stamper, etc.)

-Ron

------------------------------------------------------------------------------
Are you an open source citizen? Join us for the Open Source Bridge conference!
Portland, OR, June 17-19. Two days of sessions, one day of unconference: $250.
Need another reason to go? 24-hour hacker lounge. Register today!
http://ad.doubleclick.net/clk;215844324;13503038;v?http://opensourcebridge.org
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions

Buy the iText book: http://www.1t3xt.com/docs/book.php
Check the site with examples before you ask questions: 
http://www.1t3xt.info/examples/
You can also search the keywords list: http://1t3xt.info/tutorials/keywords/

Reply via email to