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.
Flattening all form fields
To flatten all form fields in a PDF document, use the FlattenFormFields
method:
using GdPicturePDF gdpicturePDF = new GdPicturePDF();gdpicturePDF.LoadFromFile(@"C:\temp\source.pdf");// Flatten all form fields.gdpicturePDF.FlattenFormFields();gdpicturePDF.SaveToFile(@"C:\temp\output.pdf");
Using gdpicturePDF As GdPicturePDF = New GdPicturePDF() gdpicturePDF.LoadFromFile("C:\temp\source.pdf") ' Flatten all form fields. gdpicturePDF.FlattenFormFields() gdpicturePDF.SaveToFile("C:\temp\output.pdf")End Using
Used methods
Related topics
Flattening 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(@"C:\temp\source.pdf");// Flatten form fields on the second page.gdpicturePDF.FlattenFormFields(2);gdpicturePDF.SaveToFile(@"C:\temp\output.pdf");
Using gdpicturePDF As GdPicturePDF = New GdPicturePDF() gdpicturePDF.LoadFromFile("C:\temp\source.pdf") ' Flatten form fields on the second page. gdpicturePDF.FlattenFormFields(2) gdpicturePDF.SaveToFile("C:\temp\output.pdf")End Using
Used methods
Related topics
Flattening all annotations
To flatten all annotations in a PDF document, follow these steps:
- Create a
GdPicturePDF
object. - Load the source document by passing its path to the
LoadFromFile
method. - Determine the number of pages with the
GetPageCount
and loop through them. - Determine the number of annotations on each page with the
GetAnnotationCount
and loop through them. - Flatten each annotation by passing its index with the
FlattenAnnotation
method. - Save the output in a PDF document.
using GdPicturePDF gdpicturePDF = new GdPicturePDF();gdpicturePDF.LoadFromFile(@"C:\temp\source.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 = 0; annotationIndex < annotationCount; annotationIndex++) { // Flatten each annotation. gdpicturePDF.FlattenAnnotation(annotationIndex); }}gdpicturePDF.SaveToFile(@"C:\temp\output.pdf");
Using gdpicturePDF As GdPicturePDF = New GdPicturePDF() gdpicturePDF.LoadFromFile("C:\temp\source.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 = 0 To annotationCount - 1 ' Flatten each annotation. gdpicturePDF.FlattenAnnotation(annotationIndex) Next Next gdpicturePDF.SaveToFile("C:\temp\output.pdf")End Using
Related topics
Flattening 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(@"C:\temp\source.pdf");gdpicturePDF.FlattenVisibleOCGs();gdpicturePDF.SaveToFile(@"C:\temp\output.pdf");
Using gdpicturePDF As GdPicturePDF = New GdPicturePDF() gdpicturePDF.LoadFromFile("C:\temp\source.pdf") gdpicturePDF.FlattenVisibleOCGs(); gdpicturePDF.SaveToFile("C:\temp\output.pdf")End Using
Used methods
Related topics