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:
- In terms of security, flattening a PDF document can prevent unauthorized changes or forgery. Flattening a PDF merges all form fields into the background layer, making it difficult for anyone to change the content.
- Flattening a PDF can reduce its file size by removing unnecessary information, such as form fields, annotations, and layers. This can make it easier to share, email, or store the document, especially if it is large or complex.
- Flattening a PDF file can ensure that it prints correctly, mainly if it contains complex graphics, images, or form fields. In addition, flattening the document removes any transparency effects and merges all layers into a single layer, which can help prevent printing errors and improve the overall print quality.
- Flattening a PDF document makes it easy to use because it excludes the need to interact with interactive elements, making it simpler to view, print, or share.
- If you need to save a PDF document for later use and archive it, flattening a document can ensure that it is readable and accessible for many years. Flattening eliminates the need for any specialized software or plugins, as the document will be a static, non-interactive PDF file.
- and more.
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:
1// Prepare a path to an HTML source file
2var sourcePath = Path.Combine(DataDir, "SampleHtmlForm.html");
3
4// Initialize an HTML document from the file
5using HTMLDocument document = new HTMLDocument(sourcePath);
6
7// Prepare PDF save options
8var options = new PdfSaveOptions
9{
10 // Flatten all form fields
11 FormFieldBehaviour = FormFieldBehaviour.Flattened
12};
13
14// Prepare a path to the result file
15var resultPath = Path.Combine(OutputDir, "form-flattened.pdf");
16
17// Convert HTML to PDF
18Converter.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:
- 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. - 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. - 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
:
1// Prepare a path to an MHTML source file
2var sourcePath = Path.Combine(DataDir, "SampleHtmlForm.mhtml");
3
4// Initialize PDF save options
5var options = new PdfSaveOptions
6{
7 // Flatten all form fields
8 FormFieldBehaviour = FormFieldBehaviour.Interactive
9};
10
11// Prepare a path to the result file
12var resultPath = Path.Combine(OutputDir, "document-flattened.pdf");
13
14// Convert MHTML to PDF
15Converter.ConvertMHTML(sourcePath, options, resultPath);
To convert MHTML to PDF with FormFieldBehaviour property specifying, you should follow a few steps:
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.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?
- 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. - 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. - 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!