Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
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.
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.
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.
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.
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// Flatten PDF during HTML to PDF conversion using C#
2
3// Prepare a path to an HTML source file
4string sourcePath = Path.Combine(DataDir, "SampleHtmlForm.html");
5
6// Initialize an HTML document from the file
7using HTMLDocument document = new HTMLDocument(sourcePath);
8
9// Prepare PDF save options
10PdfSaveOptions options = new PdfSaveOptions
11{
12 // Flatten all form fields
13 FormFieldBehaviour = FormFieldBehaviour.Flattened
14};
15
16// Prepare a path to the result file
17string resultPath = Path.Combine(OutputDir, "form-flattened.pdf");
18
19// Convert HTML to PDF
20Converter.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:
sourcePath) constructor loads the HTML document from a local file system.FormFieldBehaviour.Flattened, which means that all form fields in the HTML document will be flattened when the PDF document is created.resultPath.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// Flatten PDF during MHTML to PDF conversion using C#
2
3// Prepare a path to an MHTML source file
4string sourcePath = Path.Combine(DataDir, "SampleHtmlForm.mhtml");
5
6// Initialize PDF save options
7PdfSaveOptions options = new PdfSaveOptions
8{
9 // Flatten all form fields
10 FormFieldBehaviour = FormFieldBehaviour.Flattened
11};
12
13// Prepare a path to the result file
14string resultPath = Path.Combine(OutputDir, "document-flattened.pdf");
15
16// Convert MHTML to PDF
17Converter.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.
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.FormFieldBehaviour.Interactive, form fields in the PDF document will remain interactive. This means that users can fill out the form fields and make changes.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!
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.