Create FDFDoc from a Stream or String of XFDF data

388 views
Skip to first unread message

Alexander Teich

unread,
Dec 2, 2014, 4:50:57 AM12/2/14
to pdfne...@googlegroups.com
I would like to merge all data from an XFDF string (which I receive from Pdftron's Webviewer) to an existing PDF.

The only way I found so far is to read the XFDF from a file, like this:

FDFDoc fdfDoc = FDFDoc.createFromXFDF("/path/to/file.xfdf");
pdfDoc
= new PDFDoc(document);
pdfDoc
.fdfMerge(fdfDoc);
pdfDoc
.flattenAnnotations(false);

I do not want to save the XFDF - which I already have in memory -  to a temporary file, just to load it back into memory a little later.

Is there a way to read the data of the XFDF string (into an FDFDoc) directly from the String itself or a Stream?

I tried to use one the FDFDoc constructors, but to no avail. With new FDFDoc(new FileInputStream("/path/to/file.xfdf")) I am getting the following Exception:

 Message: Header not found
 
Conditional expression: false
 
Filename : Parser.cpp
 
Function : trn::SDF::Parser::SkipHeader
 
Linenumber : 759

 
Error code : 0
   at pdftron
.FDF.FDFDoc.FDFDocCreate(Native Method)
   at pdftron
.FDF.FDFDoc.<init>(FDFDoc.java:59)

So appearantly FDFDoc-Constructors are not for XFDF data.

Any help is appreciated. Thank you.
 

Support

unread,
Dec 2, 2014, 1:56:45 PM12/2/14
to pdfne...@googlegroups.com
Hi Alexander,

FDFDoc.CreateFromXFDF can take either XFDF data represented as a string or it may be a filename on disk. The latter will be attempted if the string is not valid XFDF.

For example: 

string str = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?><xfdf xmlns=\"http://ns.adobe.com/xfdf\" xml:space=\"preserve\"><square subject=\"Rectangle\" page=\"0\" name=\"cf4d2e58-e9c5-2a58-5b4d-9b4b1a330e45\" title=\"user\" creationdate=\"D:20120827112326-07'00'\" date=\"D:20120827112326-07'00'\" rect=\"227.7814207650273,597.6174863387978,437.07103825136608,705.0491803278688\" color=\"#000000\" interior-color=\"#FFFF00\" flags=\"print\" width=\"1\"><popup flags=\"print,nozoom,norotate\" open=\"no\" page=\"0\" rect=\"0,792,0,792\" /></square></xfdf>";
 
using (FDFDoc fdoc = new FDFDoc(FDFDoc.CreateFromXFDF(str))) {
in_doc.FDFMerge(fdoc);
in_doc.Save("annots_merged.pdf", SDFDoc.SaveOptions.e_linearized);
}
 
// Extract XFDF as string
FDFDoc fdoc_new = in_doc.FDFExtract(PDFDoc.ExtractFlag.e_both);
string XFDF_str = fdoc_new.SaveAsXFDF();


For a full & ready to run example, please take a look at code sample #4 in FDF sample project:
  https://www.pdftron.com/pdfnet/samplecode.html#FDF

Alexander Teich

unread,
Dec 2, 2014, 7:13:37 PM12/2/14
to pdfne...@googlegroups.com
Thank you for the fast and helpful response.

In my opinion this is something which should make it into the Java-API-Docs.

Support

unread,
Dec 5, 2014, 2:42:25 PM12/5/14
to pdfne...@googlegroups.com

Sure we will include extra docs as part of the next update :) 
Reply all
Reply to author
Forward
0 new messages