Flatten PDF – C# Examples

PDF (Portable Document Format) is a popular file format widely used for exchanging and storing digital documents. This versatile format allows you to create complex documents with various elements including text, images, forms, and more. However, in certain situations, it may be necessary to flatten a PDF document, which means converting it into a static, non-interactive file that can be easily shared, printed, or archived. This article will explain what it means to flatten a PDF and how to do it using Aspose.HTML for .NET library.

What Does it Mean to Flatten a PDF?

To flatten a PDF file means to merge all document layers into a single background layer. This process effectively “flattens” the document by removing any interactive elements such as fillable form fields and editable text. The result is a static PDF that can no longer be edited or have its appearance altered, making it ideal for sharing, printing, or archiving.

Why Flatten a PDF?

Flattening a PDF document is a process that can have several benefits, depending on your specific needs. There are several reasons why you might want to flatten a PDF file:

Whether your goals are related to security, file size, printing, archiving, or ease of use, flattening a PDF can provide you with the results you need.

How to Flatten a PDF in C#?

Flattening a PDF document is a useful process for ensuring the stability and security of a document, as well as for improving its compatibility with printers or software applications. By converting HTML or MHTML files into flattened PDFs using C# code, we can easily create static, non-interactive PDF documents.

Aspose.HTML for .NET offers the FormFieldBehaviour property of the PdfSaveOptions class to flatten PDF documents after their conversion from HTML or MHTML. This property is used to specify the behavior of form fields in a PDF document. If the value is set to FormFieldBehaviour.Flattened all form fields in the PDF document will be flattened.

HTML to PDF conversion and PDF flattening

To flatten a PDF file using Aspose.HTML for .NET, you first need to convert an HTML file into a PDF document. The HTML file can contain interactive forms that need to be converted to PDF and flattened. Here is an example of how to convert HTML to PDF and use the FormFieldBehaviour property to flatten PDF:

 1using System.IO;
 2using Aspose.Html;
 3using Aspose.Html.Converters;
 4using Aspose.Html.Rendering.Pdf;
 5using Aspose.Html.Saving;
 6...
 7     
 8    // Prepare a path to an HTML source file
 9    var sourcePath = Path.Combine(DataDir, "SampleHtmlForm.html");
10
11    // Initialize an HTML document from the file
12    using HTMLDocument document = new HTMLDocument(sourcePath);
13
14    // Prepare PDF save options
15    var options = new PdfSaveOptions
16    {
17        // Flatten all form fields
18        FormFieldBehaviour = FormFieldBehaviour.Flattened
19    };
20
21    // Prepare a path to the result file
22    var resultPath = Path.Combine(OutputDir, "form-flattened.pdf");
23
24    // Convert HTML to PDF
25    Converter.ConvertHTML(document, options, resultPath);

This way, you can easily convert an HTML file into a flattened PDF using C#. This process is straightforward and can be done with a few lines of code. Let’s look at the steps we have taken:

  1. Use one of the constructors of the HTMLDocument class to load an HTML file. In the example, the HTMLDocument(sourcePath) constructor loads the HTML document from a local file system.
  2. Create an instance of the PdfSaveOptions class that is used to specify the options for saving a PDF document. In this case, the FormFieldBehaviour property of PdfSaveOptions is set to FormFieldBehaviour.Flattened, which means that all form fields in the HTML document will be flattened when the PDF document is created.
  3. Call the ConvertHTML() method to convert HTML to PDF using the specified options, and the resulting PDF document is saved to resultPath.

MHTML to PDF conversion and PDF flattening

The following C# example shows how to convert an MHTML document located at sourcePath to a flattened PDF document and save it to outputPath:

 1using System.IO;
 2using Aspose.Html.Converters;
 3using Aspose.Html.Rendering.Pdf;
 4using Aspose.Html.Saving;
 5...
 6     
 7    // Prepare a path to an MHTML source file
 8    var sourcePath = Path.Combine(DataDir, "SampleHtmlForm.mhtml");
 9
10    // Initialize PDF save options
11    var options = new PdfSaveOptions
12    {
13        // Flatten all form fields
14        FormFieldBehaviour = FormFieldBehaviour.Flattened
15    };
16
17    // Prepare a path to the result file
18    var outputPath = Path.Combine(OutputDir, "document-flattened.pdf");
19
20    // Convert MHTML to PDF
21    Converter.ConvertMHTML(sourcePath, options, outputPath);

To convert MHTML to PDF with FormFieldBehaviour property specifying, you should follow a few steps:

  1. Initialize an instance of the PdfSaveOptions class and specify the options for saving the PDF document. In this example, the FormFieldBehaviour property of PdfSaveOptions is set to FormFieldBehaviour.Flattened, which means that all form fields in the MHTML document will be flattened when the PDF document is created.

  2. Use the ConvertMHTML() method to convert MHTML to PDF, which takes the sourcepath, options, and outputPath as parameters.

What is the difference between PDF files saved with and without FormFieldBehaviour property?

  1. If the FormFieldBehaviour property is set to FormFieldBehaviour.Flattened, form fields in the PDF document will be merged into one layer of the document. This effectively flattens the form fields, making them non-interactive and impossible to edit. The result is a static PDF document that cannot be altered.
  2. On the other hand, if the FormFieldBehaviour property is set to FormFieldBehaviour.Interactive, form fields in the PDF document will remain interactive. This means that users can fill out the form fields and make changes.
  3. In Aspose.HTML, the default value for the FormFieldBehaviour property is FormFieldBehaviour.Interactive. If the FormFieldBehaviour property is not explicitly set, form fields in the PDF document will remain interactive, allowing users to fill out and edit them.

You can download the complete C# examples and data files from GitHub.

Aspose.HTML offers free online Converters for converting HTML, XHTML, MHTML, EPUB, XML, and Markdown files to various popular formats. You can easily convert HTML to PDF, HTML to JPG, SVG to PDF, MHTML to PDF, or MD to HTML. Just select the file, choose the format to convert, and you’re done. It’s fast and completely free!

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.