---
title: "Flatten form fields, annotations, and OCGs in PDFs | Nutrient .NET SDK"
canonical_url: "https://www.nutrient.io/guides/dotnet/optimization/flatten/"
md_url: "https://www.nutrient.io/guides/dotnet/optimization/flatten.md"
last_updated: "2026-05-21T17:12:02.207Z"
description: "Learn how to flatten PDF documents programmatically in C# .NET using Nutrient .NET SDK. Merge interactive elements into static content for improved compatibility."
---

# 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](https://www.nutrient.io/api/gdpicture/GdPicture.NET.14.API~GdPicture14.GdPicturePDF~FlattenFormFields().html):

### C#

```csharp

using GdPicturePDF gdpicturePDF = new GdPicturePDF();
gdpicturePDF.LoadFromFile(@"C:\temp\source.pdf");
// Flatten all form fields.
gdpicturePDF.FlattenFormFields();
gdpicturePDF.SaveToFile(@"C:\temp\output.pdf");

```

### VB.NET

```vb

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

- [`FlattenFormFields`](/api/gdpicture/GdPicture.NET.14.API~GdPicture14.GdPicturePDF~FlattenFormFields\(\).html)

- [`LoadFromFile`](/api/gdpicture/GdPicture.NET.14.API~GdPicture14.GdPicturePDF~LoadFromFile.html)

- [`SaveToFile`](/api/gdpicture/GdPicture.NET.14.API~GdPicture14.GdPicturePDF~SaveToFile.html)

#### Related topics

- [Load a file](/guides/dotnet/load-a-file.md)

- [Save a file](/guides/dotnet/save-a-file.md)

## Flattening form fields on a specified page

To flatten all form fields on a specific page of a PDF document, use the [`FlattenFormFields` method](https://www.nutrient.io/api/gdpicture/GdPicture.NET.14.API~GdPicture14.GdPicturePDF~FlattenFormFields(Int32).html) and specify the page number as its parameter:

### C#

```csharp

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");

```

### VB.NET

```vb

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

- [`FlattenFormFields`](/api/gdpicture/GdPicture.NET.14.API~GdPicture14.GdPicturePDF~FlattenFormFields\(Int32\).html)

- [`LoadFromFile`](/api/gdpicture/GdPicture.NET.14.API~GdPicture14.GdPicturePDF~LoadFromFile.html)

- [`SaveToFile`](/api/gdpicture/GdPicture.NET.14.API~GdPicture14.GdPicturePDF~SaveToFile.html)

#### Related topics

- [Load a file](/guides/dotnet/load-a-file.md)

- [Save a file](/guides/dotnet/save-a-file.md)




## Flattening all annotations

To flatten all annotations in a PDF document, follow these steps:

1. Create a `GdPicturePDF` object.

2. Load the source document by passing its path to the `LoadFromFile` method.

3. Determine the number of pages with the `GetPageCount` and loop through them.

4. Determine the number of annotations on each page with the `GetAnnotationCount` and loop through them.

5. Flatten each annotation by passing its index with the `FlattenAnnotation` method.

6. Save the output in a PDF document.

### C#

```csharp

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");

```

### VB.NET

```vb

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

```

#### Used methods

- [`FlattenAnnotation`](/api/gdpicture/GdPicture.NET.14.API~GdPicture14.GdPicturePDF~FlattenAnnotation.html)

- [`GetAnnotationCount`](/api/gdpicture/GdPicture.NET.14.API~GdPicture14.GdPicturePDF~GetAnnotationCount.html)

- [`GetPageCount`](/api/gdpicture/GdPicture.NET.14.API~GdPicture14.GdPicturePDF~GetPageCount.html)

- [`LoadFromFile`](/api/gdpicture/GdPicture.NET.14.API~GdPicture14.GdPicturePDF~LoadFromFile.html)

- [`SaveToFile`](/api/gdpicture/GdPicture.NET.14.API~GdPicture14.GdPicturePDF~SaveToFile.html)

#### Related topics

- [Load a file](/guides/dotnet/load-a-file.md)

- [Save a file](/guides/dotnet/save-a-file.md)

## Flattening all visible optional content groups

To flatten all visible optional content groups (OCGs) in a PDF document, use the `FlattenVisibleOCGs` method:

### C#

```csharp

using GdPicturePDF gdpicturePDF = new GdPicturePDF();
gdpicturePDF.LoadFromFile(@"C:\temp\source.pdf");
gdpicturePDF.FlattenVisibleOCGs();
gdpicturePDF.SaveToFile(@"C:\temp\output.pdf");

```

### VB.NET

```vb

Using gdpicturePDF As GdPicturePDF = New GdPicturePDF()
    gdpicturePDF.LoadFromFile("C:\temp\source.pdf")
    gdpicturePDF.FlattenVisibleOCGs();
    gdpicturePDF.SaveToFile("C:\temp\output.pdf")
End Using

```

#### Used methods

- [`FlattenVisibleOCGs`](/api/gdpicture/GdPicture.NET.14.API~GdPicture14.GdPicturePDF~FlattenVisibleOCGs.html)

- [`LoadFromFile`](/api/gdpicture/GdPicture.NET.14.API~GdPicture14.GdPicturePDF~LoadFromFile.html)

- [`SaveToFile`](/api/gdpicture/GdPicture.NET.14.API~GdPicture14.GdPicturePDF~SaveToFile.html)

#### Related topics

- [Load a file](/guides/dotnet/load-a-file.md)

- [Save a file](/guides/dotnet/save-a-file.md)
---

## Related pages

- [Optimize PDFs using C#](/guides/dotnet/optimization.md)
- [MRC compression in C#](/guides/dotnet/optimization/hyper-compress-mrc.md)
- [Image and PDF compression in C#](/guides/dotnet/optimization/compress.md)
- [Linearize PDFs in C# .NET](/guides/dotnet/optimization/linearize.md)

