-
Notifications
You must be signed in to change notification settings - Fork 10
Task-935636-How to preserve form fields after creating template from the PDF document #1771
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
chinnumuniyappan
merged 7 commits into
hotfix/hotfix-v31.2.2
from
Task-935636-FlattenField
Nov 17, 2025
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
cef8912
Task-935636-Flatten Field Create Template UG
irfanajaffer 91d5b7e
Task-935636- Addressed feedbacks
irfanajaffer 50b385c
Task-935636- resolved errors
irfanajaffer 7198ad7
Merge branch 'hotfix/hotfix-v31.2.2' into Task-935636-FlattenField
irfanajaffer 7d67917
Task-935636- Updated code snippet
irfanajaffer 863ec1d
Merge branch 'Task-935636-FlattenField' of https://github.com/syncfus…
irfanajaffer f034ef4
Merge branch 'hotfix/hotfix-v31.2.2' into Task-935636-FlattenField
irfanajaffer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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