PDF Form as template

Hello

I use your PDF API, and I want merge some PDF documents with form fields. If I fill form fields and then save document on disk is form fields in document with data. But if I want merge documents as template I get document without form fields. 

//Documents with data.
public IList<PdfLoadedDocument> PdfParts { get; private set; }
....
....
//I test on one template
foreach (var item in PdfParts)
   {
    PdfLoadedForm loadedForm = item.Form;
    PdfLoadedFormFieldCollection coll = loadedForm.Fields;
    //Form fields are fill
    PdfLoadedPage lpage = item.Pages[0] as PdfLoadedPage;
    PdfTemplate template = lpage.CreateTemplate();
    g.DrawPdfTemplate(template, PointF.Empty, template.Size);
   }
//Form without data.
   mergeDocument.Save("merge.pdf", HttpContext.Current.Response, HttpReadType.Open);

Any idea. Thanks

3 Replies

SK Surya Kumar Syncfusion Team December 5, 2017 05:35 AM UTC

Hi Josef, 
 
Thank you for using Syncfusion products. 
 
While creating template from a PdfLoadedpage only the graphical elements of the page will be imported as a template, so the form fields will not be imported along with graphical elements of the page, this is the cause for the behavior which you have mentioned. 
 
So, in order to import any particular page from a loaded PDF document to a new document you may refer the below mentioned UG documentation link: 
 
or let us know your exact requirement so that we can help you better in this. Please let us know if you need any further information in this. 
 
Regards, 
Surya Kumar 



JD Josef Dotzauer December 6, 2017 10:13 PM UTC

Hi,

thank you for answer. This solution knows, but each content of document is on separate page in final document. 

My problem:
I have a few PDF files with form fields, these fields are filled from the database and then I want to merge these pdf files (part of page) in one file. These parts of the page (parts) have a certain height. And I want to add them to first page of document (page) until they fit in page size (mergeDocument.PageSettings.Size = PdfPageSize.A4;). If the sum of parts height overflows page height, I create a new page in documnet and add next part to another new created page of document.


#FIRST PAGE#
---------------
Page Header.pdf
---------------
Part1.pdf
Part2.pdf
Part3.pdf
---------------
Page Footer.pdf 
---------------

#NEXT PAGE#
---------------
Page Header.pdf
---------------
Part4.pdf
Part2.pdf
---------------
Page Footer.pdf 
---------------
...
...
...

Is there a solution to my problem?
Regards, Josef Dotzauer.


SK Surya Kumar Syncfusion Team December 7, 2017 12:12 PM UTC

Hi Josef, 
 
We have analyzed your requirement, it cannot be achieved directly since the pages have forms which cannot be exported as templates. But the form fields can be flattened and drawn as a template as shown in below code snippet: 
            //Creating document to be merged 
            PdfDocument mergeDocument = new PdfDocument(); 
            mergeDocument.PageSettings.Size = PdfPageSize.A4; 
             
            PdfPage page = mergeDocument.Pages.Add(); 
 
            // Loading form filled document 
            PdfLoadedDocument ldoc = new PdfLoadedDocument("sampleForm.pdf"); 
            //Flattening the forms 
            ldoc.Form.Flatten = true; 
            MemoryStream ms = new MemoryStream(); 
            //Save it to the stream 
            ldoc.Save(ms); 
            ldoc.Close(true); 
 
            //Load the flattened document 
            ldoc = new PdfLoadedDocument(ms); 
 
            //Create template and draw it to document to be merged 
 
            PdfTemplate temp = ldoc.Pages[0].CreateTemplate(); 
 
            page.Graphics.DrawPdfTemplate(temp, PointF.Empty, new SizeF(400,400)); 
 
            mergeDocument.Save(DataPathOutput + "template.pdf"); 
            mergeDocument.Close(true); 
            Process.Start(Path.Combine(DataPathOutput + "template.pdf")); 
 
 
Note: Flattening the form fields make all the existing forms read-only (cannot be edited). 
 
Please find the sample for the same below: 

Please let us know if you need any further information. 
 
Regards, 
Surya Kumar  
 


Loader.
Up arrow icon