Skip to content
130 changes: 130 additions & 0 deletions Document-Processing/PDF/PDF-Library/NET/Working-with-forms.md
Original file line number Diff line number Diff line change
Expand Up @@ -5101,6 +5101,136 @@ doc.Close(True)

{% endtabs %}

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

## How to Preserve Form Fields While Creating a Template from an Existing PDF That Contains Form Fields
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Preserve form fields when creating a PDF Template from an existing page


When you generate a `PdfTemplate` from an existing page, interactive **AcroForm** fields (textbox, checkbox, etc.) are **not copied** to the template.
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.

Please refer the code sample to flatten the form fields before saving the PDF document.

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.

{% tabs %}

{% highlight c# tabtitle="C# [Cross-platform]" %}
using Syncfusion.Drawing;
using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
using Syncfusion.Pdf.Parsing;
using System.IO;

//Open the source PDF that contains form fields.
using FileStream fileStream = new FileStream("Form.pdf", FileMode.Open, FileAccess.Read);
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(fileStream);

//Flatten all form fields to make them part of the page graphics.
PdfLoadedForm loadedForm = loadedDocument.Form;
loadedForm.FlattenFields();

//Create a template from the first page.
PdfLoadedPage loadedPage = loadedDocument.Pages[0] as PdfLoadedPage;
PdfTemplate template = loadedPage.CreateTemplate();

//Create the destination PDF (no page margins so the template fills it edge-to-edge).
PdfDocument newDocument = new PdfDocument();
newDocument.PageSettings.Margins.All = 0;
PdfPage newPage = newDocument.Pages.Add();

//Draw the template so it fills the entire new page.
newPage.Graphics.DrawPdfTemplate(
template,
PointF.Empty,
new SizeF(newPage.Size.Width, newPage.Size.Height));

//Save the result.
newDocument.Save("Output.pdf");

//Close documents.
loadedDocument.Close(true);
newDocument.Close(true);
{% endhighlight %}

{% highlight c# tabtitle="C# [Windows-specific]" %}
using System.Drawing;
using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
using Syncfusion.Pdf.Parsing;
using System.IO;

//Open the source PDF that contains form fields.
using FileStream fileStream = new FileStream(@"Form.pdf", FileMode.Open, FileAccess.Read);
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(fileStream);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we load it directly instead of using file stream like

PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Form.pdf");


//Flatten all form fields.
PdfLoadedForm loadedForm = loadedDocument.Form;
loadedForm.FlattenFields();

//Create a template from the first page.
PdfLoadedPage loadedPage = loadedDocument.Pages[0] as PdfLoadedPage;
PdfTemplate template = loadedPage.CreateTemplate();

//Create the destination PDF.
PdfDocument newDocument = new PdfDocument();
newDocument.PageSettings.Margins.All = 0;
PdfPage newPage = newDocument.Pages.Add();

//Draw the template so it fills the entire new page.
newPage.Graphics.DrawPdfTemplate(
template,
PointF.Empty,
new SizeF(newPage.Size.Width, newPage.Size.Height));

//Save the result.
newDocument.Save(@"Output.pdf");

//Close documents.
loadedDocument.Close(true);
newDocument.Close(true);
{% endhighlight %}

{% highlight vb.net tabtitle="VB.NET [Windows-specific]" %}
Imports Syncfusion.Pdf
Imports Syncfusion.Pdf.Graphics
Imports Syncfusion.Pdf.Parsing
Imports System.Drawing
Imports System.IO

'Open the source PDF that contains form fields.
Using fileStream As New FileStream("Form.pdf", FileMode.Open, FileAccess.Read)
Dim loadedDocument As New PdfLoadedDocument(fileStream)

'Flatten all form fields.
Dim loadedForm As PdfLoadedForm = loadedDocument.Form
loadedForm.FlattenFields()

'Create a template from the first page.
Dim loadedPage As PdfLoadedPage = TryCast(loadedDocument.Pages(0), PdfLoadedPage)
Dim template As PdfTemplate = loadedPage.CreateTemplate()

'Create the destination PDF.
Dim newDocument As New PdfDocument()
newDocument.PageSettings.Margins.All = 0
Dim newPage As PdfPage = newDocument.Pages.Add()

'Draw the template so it fills the entire new page.
newPage.Graphics.DrawPdfTemplate(
template,
PointF.Empty,
New SizeF(newPage.Size.Width, newPage.Size.Height))

'Save the result.
newDocument.Save("Output.pdf")

'Close documents.
loadedDocument.Close(True)
newDocument.Close(True)
End Using
{% endhighlight %}

{% endtabs %}

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

## Troubleshooting
Expand Down