Skip to content

Commit cef8912

Browse files
committed
Task-935636-Flatten Field Create Template UG
1 parent 295bd48 commit cef8912

File tree

1 file changed

+130
-0
lines changed

1 file changed

+130
-0
lines changed

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

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5101,6 +5101,136 @@ 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+
## How to Preserve Form Fields While Creating a Template from an Existing PDF That Contains Form Fields
5107+
5108+
When you generate a `PdfTemplate` 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]" %}
5118+
using Syncfusion.Drawing;
5119+
using Syncfusion.Pdf;
5120+
using Syncfusion.Pdf.Graphics;
5121+
using Syncfusion.Pdf.Parsing;
5122+
using System.IO;
5123+
5124+
//Open the source PDF that contains form fields.
5125+
using FileStream fileStream = new FileStream("Form.pdf", FileMode.Open, FileAccess.Read);
5126+
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(fileStream);
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+
using FileStream fileStream = new FileStream(@"Form.pdf", FileMode.Open, FileAccess.Read);
5164+
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(fileStream);
5165+
5166+
//Flatten all form fields.
5167+
PdfLoadedForm loadedForm = loadedDocument.Form;
5168+
loadedForm.FlattenFields();
5169+
5170+
//Create a template from the first page.
5171+
PdfLoadedPage loadedPage = loadedDocument.Pages[0] as PdfLoadedPage;
5172+
PdfTemplate template = loadedPage.CreateTemplate();
5173+
5174+
//Create the destination PDF.
5175+
PdfDocument newDocument = new PdfDocument();
5176+
newDocument.PageSettings.Margins.All = 0;
5177+
PdfPage newPage = newDocument.Pages.Add();
5178+
5179+
//Draw the template so it fills the entire new page.
5180+
newPage.Graphics.DrawPdfTemplate(
5181+
template,
5182+
PointF.Empty,
5183+
new SizeF(newPage.Size.Width, newPage.Size.Height));
5184+
5185+
//Save the result.
5186+
newDocument.Save(@"Output.pdf");
5187+
5188+
//Close documents.
5189+
loadedDocument.Close(true);
5190+
newDocument.Close(true);
5191+
{% endhighlight %}
5192+
5193+
{% highlight vb.net tabtitle="VB.NET [Windows-specific]" %}
5194+
Imports Syncfusion.Pdf
5195+
Imports Syncfusion.Pdf.Graphics
5196+
Imports Syncfusion.Pdf.Parsing
5197+
Imports System.Drawing
5198+
Imports System.IO
5199+
5200+
'Open the source PDF that contains form fields.
5201+
Using fileStream As New FileStream("Form.pdf", FileMode.Open, FileAccess.Read)
5202+
Dim loadedDocument As New PdfLoadedDocument(fileStream)
5203+
5204+
'Flatten all form fields.
5205+
Dim loadedForm As PdfLoadedForm = loadedDocument.Form
5206+
loadedForm.FlattenFields()
5207+
5208+
'Create a template from the first page.
5209+
Dim loadedPage As PdfLoadedPage = TryCast(loadedDocument.Pages(0), PdfLoadedPage)
5210+
Dim template As PdfTemplate = loadedPage.CreateTemplate()
5211+
5212+
'Create the destination PDF.
5213+
Dim newDocument As New PdfDocument()
5214+
newDocument.PageSettings.Margins.All = 0
5215+
Dim newPage As PdfPage = newDocument.Pages.Add()
5216+
5217+
'Draw the template so it fills the entire new page.
5218+
newPage.Graphics.DrawPdfTemplate(
5219+
template,
5220+
PointF.Empty,
5221+
New SizeF(newPage.Size.Width, newPage.Size.Height))
5222+
5223+
'Save the result.
5224+
newDocument.Save("Output.pdf")
5225+
5226+
'Close documents.
5227+
loadedDocument.Close(True)
5228+
newDocument.Close(True)
5229+
End Using
5230+
{% endhighlight %}
5231+
5232+
{% endtabs %}
5233+
51045234
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).
51055235

51065236
## Troubleshooting

0 commit comments

Comments
 (0)