From cef89126681f74c2b21704a03c25853acbbf5406 Mon Sep 17 00:00:00 2001 From: Irfana Jaffer Sadhik Date: Wed, 12 Nov 2025 16:23:43 +0530 Subject: [PATCH 1/4] Task-935636-Flatten Field Create Template UG --- .../PDF/PDF-Library/NET/Working-with-forms.md | 130 ++++++++++++++++++ 1 file changed, 130 insertions(+) diff --git a/Document-Processing/PDF/PDF-Library/NET/Working-with-forms.md b/Document-Processing/PDF/PDF-Library/NET/Working-with-forms.md index fd0bfa624..4b9d723f1 100644 --- a/Document-Processing/PDF/PDF-Library/NET/Working-with-forms.md +++ b/Document-Processing/PDF/PDF-Library/NET/Working-with-forms.md @@ -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 + +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); + +//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 From 91d5b7e4f144b8872007291547eb33513ef01945 Mon Sep 17 00:00:00 2001 From: Irfana Jaffer Sadhik Date: Thu, 13 Nov 2025 16:20:14 +0530 Subject: [PATCH 2/4] Task-935636- Addressed feedbacks --- .../PDF/PDF-Library/NET/Working-with-forms.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Document-Processing/PDF/PDF-Library/NET/Working-with-forms.md b/Document-Processing/PDF/PDF-Library/NET/Working-with-forms.md index 4b9d723f1..b96f9b123 100644 --- a/Document-Processing/PDF/PDF-Library/NET/Working-with-forms.md +++ b/Document-Processing/PDF/PDF-Library/NET/Working-with-forms.md @@ -5103,7 +5103,7 @@ doc.Close(True) 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 +## 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. @@ -5114,7 +5114,8 @@ N> Flattening permanently removes interactivity. The resulting PDF shows the fo {% tabs %} -{% highlight c# tabtitle="C# [Cross-platform]" %} +{% 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" %} + using Syncfusion.Drawing; using Syncfusion.Pdf; using Syncfusion.Pdf.Graphics; @@ -5122,8 +5123,7 @@ 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); +PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Form.pdf"); //Flatten all form fields to make them part of the page graphics. PdfLoadedForm loadedForm = loadedDocument.Form; From 50b385c202a0de735aeffb287a3cf94d28474f23 Mon Sep 17 00:00:00 2001 From: Irfana Jaffer Sadhik Date: Thu, 13 Nov 2025 16:27:10 +0530 Subject: [PATCH 3/4] Task-935636- resolved errors --- Document-Processing/PDF/PDF-Library/NET/Working-with-forms.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Document-Processing/PDF/PDF-Library/NET/Working-with-forms.md b/Document-Processing/PDF/PDF-Library/NET/Working-with-forms.md index b96f9b123..904231c8e 100644 --- a/Document-Processing/PDF/PDF-Library/NET/Working-with-forms.md +++ b/Document-Processing/PDF/PDF-Library/NET/Working-with-forms.md @@ -5105,7 +5105,7 @@ You can download a complete working sample from [GitHub](https://github.com/Sync ## 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. +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. 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. @@ -5114,7 +5114,7 @@ N> Flattening permanently removes interactivity. The resulting PDF shows the fo {% tabs %} -{% 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" %} +{% 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" %} using Syncfusion.Drawing; using Syncfusion.Pdf; From 7d679170f3013343a9cd4b73176f3bda698b84ef Mon Sep 17 00:00:00 2001 From: Irfana Jaffer Sadhik Date: Thu, 13 Nov 2025 16:48:38 +0530 Subject: [PATCH 4/4] Task-935636- Updated code snippet --- .../PDF/PDF-Library/NET/Working-with-forms.md | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/Document-Processing/PDF/PDF-Library/NET/Working-with-forms.md b/Document-Processing/PDF/PDF-Library/NET/Working-with-forms.md index 904231c8e..5dc7e7b86 100644 --- a/Document-Processing/PDF/PDF-Library/NET/Working-with-forms.md +++ b/Document-Processing/PDF/PDF-Library/NET/Working-with-forms.md @@ -5160,8 +5160,7 @@ 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); +PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Form.pdf"); //Flatten all form fields. PdfLoadedForm loadedForm = loadedDocument.Form; @@ -5198,8 +5197,7 @@ 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) +Dim loadedDocument As New PdfLoadedDocument("Form.pdf") 'Flatten all form fields. Dim loadedForm As PdfLoadedForm = loadedDocument.Form