Import XML and insert into FormFields

Hi ,

I'm trying to import some product info in XML format into form fields in a PDF form. I have managed to output the contents of my XML into a PDF doc using Aspose.form but i cant manage to figure out if you can use it to form fill fields in an existing PDF form ..and then save it off as a new form. Is this even possible with Aspose ?

Thanks in advance for your help ,

Ali

Dear Ali,

Thanks for considering Aspose.Pdf.Kit.

With Form.ImportXml( ), you can import your product info into an existing PDF template with empty Fields. Finally you will get an output PDF document with the info. However, you must notice that the entries in XML should have the same name with the corresponding field. For example, there is an entry Luke.Fu in product.xml. Then the data “Luke.Fu” will be filled into a text field in PDF document with the field name “username”. To understand this relationship, you can filled your PDF in Adobe, then export the data into XML file and check it.

You can also learn from the [demo ](http://www.aspose.com/demos/file-format-components/aspose.pdf.kit/csharp/import-xml.aspx). If you still have puzzles on it, please feel free to tell us :slight_smile:

Best regards.

Thanks for the feedback Luke , this seems to be working for me now . Yes i see what you mean about the XML format , and needing the field names in the XML to map exactly the form field names in the PDF. I will have to generate another stylesheet to transform my XML into a format that will work with this solution.

One other question , i will need to dynamically repeat sections of my PDF , for example i have a section in my PDF for Applicants , but if i have 3 applicants , i need to dynamically create this section 3 times ...or if i only have 1 , i can just create it the once , is this possible with Aspose.pdf.kit as well ?

Thanks in Advance ,

Ali

Dear Ali,

I am sorry I am not very clear about your second question. Would you please explain it in detail? Do you mean that you want to copy some fields as repeated sections? If so, please look at FormEditor.CopyInnerField(), which can copy a field to another position within the same document.

Best regards.

Hi Luke ,

Sorry for the confusion . I mean that if i have multiple clients i.e. 3 and i have a client page in my PDF , i can dynamically create 3 of client pages for each client i have in my XML coming in. Or if i have 20 funds , and i have a fund section in my PDF , i can repeat this 20 times .I think some forms refer to them as "sub forms" in PDF form documents. Is this possible with the Aspose.pdf.kit ?

Thanks in advance for your help ,

Regards,

Ali

Hi Luke ,

When i interigate the methods on your Aspose dll they have no comments , for example :

Form form = new Form(templatePdf, outputStream);

The Form has 7 overides , but no comments to say what each parameter is used for , is this because i only have an evaluation version , if we ordered the full version would this version come with proper comments , or at least full documentation to tell us what each parameter and method is used for ?

Thanks ,

Ali

Dear Ali,

Thanks for your explaination. Unfortunately, Aspose.Pdf.Kit can only operate on all kinds of form fields, instead of any "sub forms". And we have no plan to provide such support.

As for your question about function comments, please refer to our online API. And these comments were expected to be included in the dll. I will contact with our product manager and confirm it.

Best regards.

Thanks for your feedback lukeyoyo.If i cant use subforms i will try and insert the pages i need. I now have a 1 page PDF file to insert into a multipage PDF. I want to insert the 1st page from the 1st file twice into the 2nd file at position 2. But i can only seem to insert the smae page once ? Can i insert the same page twice ?

Thanks ,

Ali

Hi Ali,

As I understand your requirement, you would like to add the same page twice means you would add the page 1 in the first file to position 2 and 3 in the output file. Is that what you're trying to achieve?

If yes, you can use PdfFileEditor.Insert method twice to insert a particular page at the positions 2 and 3 in the target file.

If there is some confusion, or I have misunderstood something, please do let us know.

Regards,

Thnaks , i can now insert the same page twice , can i then do a FormFill on the resulting stream ? When you insert a page into another PDF document do the form fields come with it , i seem to be having trouble doing a form fill on an inserted page after i have inserted it into another PDF ?

Dear Ali,

Thanks for shahzad's comment. As he mentioned above, you can insert specified page into a pdf document by PdfFileEditor.Insert(). Basically, you can insert this page twice by invoking PdfFileEditor.Insert() twice. However, two fields with exactly the same field name cannot be added into one single document. So it doesn't support pdf docs with form fields. FYI, we also found that repeatedly inserting documents with Form fields cannot be supported by Adobe Professional either. I am afraid that this feature cannot be provided in short time.

Sorry for the inconvenience.

Best regards.

Thanks for getting back to me so quickly.

So do formfields not work at all with inserted pages ? If i inserted one page , with form fields for say : forename , surename , address and postcode into another PDF , could i fill them as long as they are unique form field names ?

I need to be able to form Fill a PDF document after i have inserted a page(with form fields in it) , is this possible , see the code below , it is still producing a document with no fields filled in it from the incomming XML ...

string inputfile1 = GetResource("PFB6.pdf");

// pdf file from which pages will be insterted

string inputfile2 = GetResource("Applicant.pdf");

//// Pdf file to which pages will be inserted

FileStream inStream1 = File.Open(inputfile1, FileMode.Open, FileAccess.Read, FileShare.Read);

// Stream of Pdf file for pages

FileStream inStream2 = File.Open(inputfile2, FileMode.Open, FileAccess.Read, FileShare.Read);

HttpResponse response = HttpContext.Current.Response;

////Example 1 :insert page 2 pages at i postition

PdfFileEditor pdfEditor = new PdfFileEditor();

int location = 1;

int[] pages = new int[] { 1 };

//generate new Pdf file output stream

MemoryStream outStream = new MemoryStream();

pdfEditor.Insert(inStream1, location, inStream2, pages, outStream);

//---Formfill XML

Form form = new Form(outStream, "output.pdf");

string xml = GetResource("XML.xml");

//Open an existed fdf file.

System.IO.FileStream xmlInputStream = new FileStream(xml, FileMode.Open);

//Export all the pdf fields' value into the fdf file.

form.ImportXml(xmlInputStream);

form.Save();

xmlInputStream.Close();

// create a byte array that will hold the output pdf

byte[] outBuf = outStream.GetBuffer();

// specify the duration of time before a page,cached on a browser expires

response.Expires = 0;

// Specify the property to buffer the output page

response.Buffer = true;

// Erase any buffered HTML output

response.ClearContent();

//Add a new HTML header and value to the response sent to the client

response.AddHeader("content-disposition", "inline; filename=" + "output.pdf");

// Specify the HTTP content type for response as Pdf

response.ContentType = "application/pdf";

// Write specified information of current HTTP output to Byte array

response.BinaryWrite(outBuf);

// close the output stream

outStream.Close();

//end the processing of the current page to ensure that no other HTML content is sent

Dear Ali,

The answer is 'Yes'. After inserting a page with form field, centainly you can fill the output file as usual. I inserted a page with some fields into a Pdf document with other fields (of cause, they have different field names), and successfully got an output file, which can be filled correctly.

Would you please provide your source files so as to reproduced the problem?

Thanks & Best regards.

Hi Luke ,

Thanks for that , i got it working , yes as long as the field names are all unique i have no problems. Another question , if we are inserting pages etc , our sections and pages might be out of sinc. Can you Alter the Page numbers on the PDF dynamically after inserting pages?

Also can you add new form fields to a PDF document at runtime from the c# code using PDF.kit ?

Digital signatures: does PDF.kit support this ?

Hi Ali,

You can add new form fields into an existing PDF file using FormEditor class. The method to add the form field is AddField.

Digital signature feature is supported by Aspose.Pdf.Kit. Please read the topic 'Manipulate signatures using PdfFileSignature class' in our documentation.

As far your first question is concerned, I think Luke would be able to help you out in a better way. I'll ask him to update you.

Regards,

Hi Ali,

Thanks for shahzad's help.

For your first question, presently Kit cannot reset page number automatically. However, Kit provides PdfFileStamp.AddPageNumber() to allow users add page number in each pages. Please refer to the API and the following demo code:

PdfFileStamp fileStamp = new PdfFileStamp(inFile, outFile);
FormattedText ft = new FormattedText("Page #.",System.Drawing.Color.FromArgb((0,200,0),
FontStyle.TimesRoman,EncodingType.Winansi,false,12);
fileStamp.AddPageNumber(ft);
fileStamp.Close();

Hope it helps & Best regards.

Thanks for that .

I am also trying to save off new new PDF , this is my PDF that has pages inserted into it and fields filled , my code looks like this:

Form form = new Form(streamWork, outputstream);

//Export all the pdf fields' value into the fdf file.

form.ImportXml(streamXMLResults);

//Need to flatten fields

form.FlattenAllFields();

form.Save();

HttpResponse response = HttpContext.Current.Response;

//Take form output stream

byte[] outBuf = outputstream.GetBuffer();

// specify the duration of time before a page,cached on a browser expires

response.Expires = 0;

// Specify the property to buffer the output page

response.Buffer = true;

// Erase any buffered HTML output

// Specify the HTTP content type for response as Pdf

response.ContentType = "application/pdf";

// Write specified information of current HTTP output to Byte array

response.BinaryWrite(outBuf);

//Release resources

streamXMLResults.Close();

outputstream.Close();

//end the processing of the current page to ensure that no other HTML content is sent

response.End();

Now i want to save it off to Disk ..can this be done in pdf.kit for aspose ?

Hi, Ali

The output document was recorded in the 'outputstream' you defined before. After form.Save(), you can either display it in a browser or save it to disk, just as any common stream does.

Best regards.

Thanks , i have that working now , thank you for your help ,

Ali