Skip to content

Commit 7d43478

Browse files
Merge pull request #1771 from syncfusion-content/Task-935636-FlattenField
Task-935636-How to preserve form fields after creating template from the PDF document
2 parents f43869b + f034ef4 commit 7d43478

File tree

1 file changed

+128
-0
lines changed

1 file changed

+128
-0
lines changed

Document-Processing/PDF/PDF-Library/NET/Working-with-forms.md

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5101,6 +5101,134 @@ doc.Close(True)
51015101

51025102
{% endtabs %}
51035103

5104+
You can download a complete working sample from [GitHub](https://github.com/SyncfusionExamples/PDF-Examples/tree/master/Forms/Auto-resize-the-text-of-textboxfield-in-a-PDF).
5105+
5106+
## Preserve form fields when creating a PDF Template from an existing page
5107+
5108+
When you create a [PdfTemplate](https://help.syncfusion.com/cr/document-processing/Syncfusion.Pdf.Graphics.PdfTemplate.html) from an existing page, interactive **AcroForm** fields (textbox, checkbox, etc.) are **not copied** to the template.
5109+
If you still need the visual appearance of those form fields in the final document, you can flatten the form using the [FlattenFields](https://help.syncfusion.com/cr/document-processing/Syncfusion.Pdf.Parsing.PdfLoadedForm.html#Syncfusion_Pdf_Parsing_PdfLoadedForm_FlattenFields) API.
5110+
5111+
Please refer the code sample to flatten the form fields before saving the PDF document.
5112+
5113+
N> Flattening permanently removes interactivity. The resulting PDF shows the form content exactly as it appears on screen, but users can no longer edit the fields.
5114+
5115+
{% tabs %}
5116+
5117+
{% highlight c# tabtitle="C# [Cross-platform]" playgroundButtonLink="https://raw.githubusercontent.com/SyncfusionExamples/PDF-Examples/master/Forms/Preserve-Formfields-in-the-Template-created-from-existing-PDF/Preserve-Formfields-in-the-Template-created-from-existing-PDF/Program.cs" %}
5118+
5119+
using Syncfusion.Drawing;
5120+
using Syncfusion.Pdf;
5121+
using Syncfusion.Pdf.Graphics;
5122+
using Syncfusion.Pdf.Parsing;
5123+
using System.IO;
5124+
5125+
//Open the source PDF that contains form fields.
5126+
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Form.pdf");
5127+
5128+
//Flatten all form fields to make them part of the page graphics.
5129+
PdfLoadedForm loadedForm = loadedDocument.Form;
5130+
loadedForm.FlattenFields();
5131+
5132+
//Create a template from the first page.
5133+
PdfLoadedPage loadedPage = loadedDocument.Pages[0] as PdfLoadedPage;
5134+
PdfTemplate template = loadedPage.CreateTemplate();
5135+
5136+
//Create the destination PDF (no page margins so the template fills it edge-to-edge).
5137+
PdfDocument newDocument = new PdfDocument();
5138+
newDocument.PageSettings.Margins.All = 0;
5139+
PdfPage newPage = newDocument.Pages.Add();
5140+
5141+
//Draw the template so it fills the entire new page.
5142+
newPage.Graphics.DrawPdfTemplate(
5143+
template,
5144+
PointF.Empty,
5145+
new SizeF(newPage.Size.Width, newPage.Size.Height));
5146+
5147+
//Save the result.
5148+
newDocument.Save("Output.pdf");
5149+
5150+
//Close documents.
5151+
loadedDocument.Close(true);
5152+
newDocument.Close(true);
5153+
{% endhighlight %}
5154+
5155+
{% highlight c# tabtitle="C# [Windows-specific]" %}
5156+
using System.Drawing;
5157+
using Syncfusion.Pdf;
5158+
using Syncfusion.Pdf.Graphics;
5159+
using Syncfusion.Pdf.Parsing;
5160+
using System.IO;
5161+
5162+
//Open the source PDF that contains form fields.
5163+
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Form.pdf");
5164+
5165+
//Flatten all form fields.
5166+
PdfLoadedForm loadedForm = loadedDocument.Form;
5167+
loadedForm.FlattenFields();
5168+
5169+
//Create a template from the first page.
5170+
PdfLoadedPage loadedPage = loadedDocument.Pages[0] as PdfLoadedPage;
5171+
PdfTemplate template = loadedPage.CreateTemplate();
5172+
5173+
//Create the destination PDF.
5174+
PdfDocument newDocument = new PdfDocument();
5175+
newDocument.PageSettings.Margins.All = 0;
5176+
PdfPage newPage = newDocument.Pages.Add();
5177+
5178+
//Draw the template so it fills the entire new page.
5179+
newPage.Graphics.DrawPdfTemplate(
5180+
template,
5181+
PointF.Empty,
5182+
new SizeF(newPage.Size.Width, newPage.Size.Height));
5183+
5184+
//Save the result.
5185+
newDocument.Save(@"Output.pdf");
5186+
5187+
//Close documents.
5188+
loadedDocument.Close(true);
5189+
newDocument.Close(true);
5190+
{% endhighlight %}
5191+
5192+
{% highlight vb.net tabtitle="VB.NET [Windows-specific]" %}
5193+
Imports Syncfusion.Pdf
5194+
Imports Syncfusion.Pdf.Graphics
5195+
Imports Syncfusion.Pdf.Parsing
5196+
Imports System.Drawing
5197+
Imports System.IO
5198+
5199+
'Open the source PDF that contains form fields.
5200+
Dim loadedDocument As New PdfLoadedDocument("Form.pdf")
5201+
5202+
'Flatten all form fields.
5203+
Dim loadedForm As PdfLoadedForm = loadedDocument.Form
5204+
loadedForm.FlattenFields()
5205+
5206+
'Create a template from the first page.
5207+
Dim loadedPage As PdfLoadedPage = TryCast(loadedDocument.Pages(0), PdfLoadedPage)
5208+
Dim template As PdfTemplate = loadedPage.CreateTemplate()
5209+
5210+
'Create the destination PDF.
5211+
Dim newDocument As New PdfDocument()
5212+
newDocument.PageSettings.Margins.All = 0
5213+
Dim newPage As PdfPage = newDocument.Pages.Add()
5214+
5215+
'Draw the template so it fills the entire new page.
5216+
newPage.Graphics.DrawPdfTemplate(
5217+
template,
5218+
PointF.Empty,
5219+
New SizeF(newPage.Size.Width, newPage.Size.Height))
5220+
5221+
'Save the result.
5222+
newDocument.Save("Output.pdf")
5223+
5224+
'Close documents.
5225+
loadedDocument.Close(True)
5226+
newDocument.Close(True)
5227+
End Using
5228+
{% endhighlight %}
5229+
5230+
{% endtabs %}
5231+
51045232
You can download a complete working sample from [GitHub](https://github.com/SyncfusionExamples/PDF-Examples/tree/master/Forms/Auto-resize-the-text-of-textboxfield-in-a-PDF).
51055233

51065234
## Troubleshooting

0 commit comments

Comments
 (0)