Create FDFDoc from a Stream or String of XFDF data

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


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.(

So appearantly FDFDoc-Constructors are not for XFDF data.

Any help is appreciated. Thank you.

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=\"[](\" 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.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:

Thank you for the fast and helpful response.

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

Sure we will include extra docs as part of the next update :slight_smile: