Önder D.
Önder D.

Reputation: 23

I can not set xfa data with itext

I have a pdf which is produced by GMC Printnet Designer 7.0. And i have an xml file with xfa template. I want to merge them and embed data in pdf. (I don't want to attach xml!)

I can read the xml data, i can read the input pdf, i can start a memory stream and produce the output data with it. But when i check the pdf which i produce with itext, i see no xfa form.

here is my c# code and xml file:

PdfReader reader = new PdfReader("in.pdf");
    MemoryStream ms = new MemoryStream();
    PdfStamper stamper = new PdfStamper(reader, ms);
    XfaForm xfa = new XfaForm(reader);
    XmlDocument doc = new XmlDocument();
    doc.Load("in.xml");
    xfa.DomDocument = doc;
    xfa.Changed = true;
    XfaForm.SetXfa(xfa, stamper.Reader, stamper.Writer);
    xfa.XfaPresent = true;
    stamper.Close();
    File.WriteAllBytes("out.pdf", ms.ToArray());

XML file

<?xml version="1.0" encoding="UTF-8"?><xfa:data xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
<Invoice>
<InvoiceNr>1010101010101010</InvoiceNr>
<DateIssuance>2014-07-22</DateIssuance>
<PaymentDueDate>2014-09-09</PaymentDueDate>
<AmountToBePaid>53,25</AmountToBePaid>
</Invoice>
</xfa:data>

Upvotes: 1

Views: 1205

Answers (1)

Bruno Lowagie
Bruno Lowagie

Reputation: 77566

As I explained in the comments, I don't understand your code (nor do I know where you've found it). The official documentation says you should fill the form like this:

using (PdfStamper stamper = new PdfStamper(reader, ms)) {
    AcroFields form = stamper.AcroFields;
    XfaForm xfa = form.Xfa;
    xfa.FillXfaForm(XmlReader.Create(new StringReader(xml)));
} 

See the XfaMovies example. For the C# version, see http://tinyurl.com/itextsharpIIA2C08

Update:

in the comments, you have added links to the PDF and to an XML file. Both files are problematic:

  1. The PDF file isn't a form. At the most, you could use it as a background PDF in an XFAF form. This can only be done manually and requires a designer such as Adobe LiveCycle Designer.
  2. The XML file is not an XFA stream! As documented in my book (which the OP defines as NOT HELPING), an XFA stream consists of different parts such as a config part, a localeSet part, an xmp part, but more importantly a template part and a datasets part that consists of a data and a datadescription part. The XML that is shared only contains the data part, there is no datadescription and there is no template. Hence this XML can never be integrated into a PDF file with an XFA form as result: there is just too much information missing.

Now that I've seen the files you were talking about in your question, it is clear that you're asking something that is impossible. You need to take the xsd of your XML, import it into a tool such as Adobe LiveCycle Designer, then manually define the location of the fields in your template (that information is missing in your XML) and define the PDF as the background for your form.

There is no way to automate this process, given the files you have shared.

Upvotes: 2

Related Questions