Flatten PDF in C#

PDF documents often include forms featuring interactive fillable widgets, such as radio buttons, checkboxes, text boxes, lists, etc. In order to make it non-editable for different application purposes, we need to flatten the PDF file. IronPDF provides the function to flatten your PDF in C# with just one line of code.

Code Examples

Flatten PDF in C#
using IronPdf;
using System.Windows.Forms;

namespace flattenpdf
{
	public partial class Form1 : Form
	{
		public Form1()
		{
			InitializeComponent();

			//Select the Desired PDF File
			PdfDocument PDF = PdfDocument.FromFile("before.pdf");

			//Flatten method compress the file & reduce the size of a pdf with same quality
			PDF.Flatten();

			//SaveAs the new Flattened.pdf
			PDF.SaveAs("after_flatten.pdf");
			
		}
	}
}
Imports IronPdf
Imports System.Windows.Forms

Namespace flattenpdf
	Partial Public Class Form1
		Inherits Form

		Public Sub New()
			InitializeComponent()

			'Select the Desired PDF File
			Dim PDF As PdfDocument = PdfDocument.FromFile("before.pdf")

			'Flatten method compress the file & reduce the size of a pdf with same quality
			PDF.Flatten()

			'SaveAs the new Flattened.pdf
			PDF.SaveAs("after_flatten.pdf")

		End Sub
	End Class
End Namespace
Jump to Article
Try IronPDF free for development


Step 1

1. Install the IronPDF Software

First things first, let's install IronPDF, available free for development projects. Get it from:

Direct Download ZIP or through the NuGet format here.


 PM > Install-Package IronPdf


How to Tutorial

2. Flatten C# PDF Document

Once the IronPDF package is loaded in your Visual Studio project, you can use it to flatten your PDF file with just one line of code.

In the below code example, we have selected our own PDF using the “PDFDocument” class. If your project calls for it, you can also create a PDF using “HtmltoPdf()” method.

Then, to flatten a PDF file, the method is “Flatten()”. This will make it uneditable using the interactive widgets that may have accompanied the file generation, such as radio buttons and checkboxes.

Let's see it in action in the flatten PDF C# code example below.

using IronPdf;
using System.Windows.Forms;

namespace flattenpdf
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            //Select the Desired PDF File
            PdfDocument PDF = PdfDocument.FromFile("before.pdf");

            //Flatten method compress the file & reduce the size of a pdf with same quality
            PDF.Flatten();

            //SaveAs the new Flattened.pdf
            PDF.SaveAs("after_flatten.pdf");

        }
    }
}
Imports IronPdf
Imports System.Windows.Forms

Namespace flattenpdf
	Partial Public Class Form1
		Inherits Form

		Public Sub New()
			InitializeComponent()

			'Select the Desired PDF File
			Dim PDF As PdfDocument = PdfDocument.FromFile("before.pdf")

			'Flatten method compress the file & reduce the size of a pdf with same quality
			PDF.Flatten()

			'SaveAs the new Flattened.pdf
			PDF.SaveAs("after_flatten.pdf")

		End Sub
	End Class
End Namespace
VB   C#


3. Check the Flattened Document

In the below output, the first PDF is editable, our original file. Using IronPDF and the code above, we have made it flat or non-editable. You can use this code for any of your .NET PDF project needs.


Library Quick Access

Read More Documentation

Read the Documentation for more on how to flatten PDFs, edit and manipulate them, and more.

Read More Documentation