Flatten PDFs in C# .NET
Flattening PDFs means removing objects such as form fields, annotations, and optional content groups (OCGs) from a document’s internal structure and placing their data in the PDF as regular items.
Download sampleFlattening all form fields
To flatten all form fields in a PDF document, use the FlattenFormFields method:
Register the SDK license before running SDK operations. For setup details, refer to the getting started with .NET SDK guide.
using GdPicture14;
LicenseManager license = new LicenseManager();license.RegisterKEY("");using GdPicturePDF gdpicturePDF = new GdPicturePDF();gdpicturePDF.LoadFromFile(@"input_forms.pdf");// Flatten all form fields.gdpicturePDF.FlattenFormFields();gdpicturePDF.SaveToFile(@"output.pdf");Register the SDK license before running SDK operations. For setup details, refer to the getting started with .NET SDK guide.
Imports GdPicture14
Dim license As LicenseManager = New LicenseManager()license.RegisterKEY("")Using gdpicturePDF As GdPicturePDF = New GdPicturePDF() gdpicturePDF.LoadFromFile("input_forms.pdf") ' Flatten all form fields. gdpicturePDF.FlattenFormFields() gdpicturePDF.SaveToFile("output.pdf")End UsingFlattening form fields on a specified page
To flatten all form fields on a specific page of a PDF document, use the FlattenFormFields method and specify the page number as its parameter:
using GdPicturePDF gdpicturePDF = new GdPicturePDF();gdpicturePDF.LoadFromFile(@"input.pdf");// Flatten form fields on the second page.gdpicturePDF.FlattenFormFields(2);gdpicturePDF.SaveToFile(@"output.pdf");Using gdpicturePDF As GdPicturePDF = New GdPicturePDF() gdpicturePDF.LoadFromFile("input.pdf") ' Flatten form fields on the second page. gdpicturePDF.FlattenFormFields(2) gdpicturePDF.SaveToFile("output.pdf")End UsingFlattening all annotations
To flatten all annotations in a PDF document, follow these steps:
- Create a
GdPicturePDFobject. - Load the source document by passing its path to the
LoadFromFilemethod. - Determine the number of pages with the
GetPageCountand loop through them. - Determine the number of annotations on each page with the
GetAnnotationCountand loop through them. - Flatten each annotation by passing its index with the
FlattenAnnotationmethod. - Save the output in a PDF document.
using GdPicturePDF gdpicturePDF = new GdPicturePDF();gdpicturePDF.LoadFromFile(@"input.pdf");// Loop through each annotation on each page.int pageCount = gdpicturePDF.GetPageCount();for (int pageIndex = 1; pageIndex <= pageCount; pageIndex++){ gdpicturePDF.SelectPage(pageIndex); int annotationCount = gdpicturePDF.GetAnnotationCount(); for (int annotationIndex = annotationCount - 1; annotationIndex >= 0; annotationIndex--) { // Flatten each annotation. gdpicturePDF.FlattenAnnotation(annotationIndex); }}gdpicturePDF.SaveToFile(@"output.pdf");Using gdpicturePDF As GdPicturePDF = New GdPicturePDF() gdpicturePDF.LoadFromFile("input.pdf") ' Loop through each annotation on each page. Dim pageCount As Integer = gdpicturePDF.GetPageCount() For pageIndex = 1 To pageCount gdpicturePDF.SelectPage(pageIndex) Dim annotationCount As Integer = gdpicturePDF.GetAnnotationCount() For annotationIndex = annotationCount - 1 To 0 Step -1 ' Flatten each annotation. gdpicturePDF.FlattenAnnotation(annotationIndex) Next Next gdpicturePDF.SaveToFile("output.pdf")End UsingFlattening all visible optional content groups
To flatten all visible optional content groups (OCGs) in a PDF document, use the FlattenVisibleOCGs method:
using GdPicturePDF gdpicturePDF = new GdPicturePDF();gdpicturePDF.LoadFromFile(@"input.pdf");gdpicturePDF.FlattenVisibleOCGs();gdpicturePDF.SaveToFile(@"output.pdf");Using gdpicturePDF As GdPicturePDF = New GdPicturePDF() gdpicturePDF.LoadFromFile("input.pdf") gdpicturePDF.FlattenVisibleOCGs() gdpicturePDF.SaveToFile("output.pdf")End Using