Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
Flattening a PDF means turning interactive PDF content, such as form fields, into fixed page content. The output PDF is easier to share, print, archive, or send to users who should not edit form values after the file is created.
Aspose.HTML for .NET flattens form fields when creating a PDF from HTML or MHTML. Create PdfSaveOptions, set FormFieldBehaviour = FormFieldBehaviour.Flattened, and call Converter.ConvertHTML() or Converter.ConvertMHTML() to save a fixed PDF file.
To flatten a PDF file is to make interactive elements part of the static page appearance. In this article, the important case is form fields: text boxes, checkboxes, and other form controls are written into the PDF as non-interactive content instead of remaining editable fields.
This is useful when a generated PDF should preserve the final visual result and not behave like a fillable form. It can also improve compatibility with PDF viewers, printers, document exchange systems, and archive workflows.
Flatten a PDF when the output should be fixed after conversion:
To flatten a PDF during HTML to PDF conversion, set the
FormFieldBehaviour property on
PdfSaveOptions. The default value is FormFieldBehaviour.Interactive, so form fields remain editable unless you explicitly set FormFieldBehaviour.Flattened.
To convert HTML to a flattened PDF:
PdfSaveOptions instance.options.FormFieldBehaviour = FormFieldBehaviour.Flattened. 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);MHTML can also contain HTML forms and related resources in one archive file. The flattening approach is the same: create PdfSaveOptions, set FormFieldBehaviour.Flattened, and pass the options to
Converter.ConvertMHTML().
To convert MHTML to a flattened PDF:
PdfSaveOptions instance.options.FormFieldBehaviour = FormFieldBehaviour.Flattened.Converter.ConvertMHTML(stream, options, 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);FormFieldBehaviour controls what happens to form fields when Aspose.HTML creates the output PDF. Use Flattened when the PDF should preserve the final appearance, and use Interactive when the PDF should remain fillable.
| Value | Result in the output PDF | Use it when |
|---|---|---|
FormFieldBehaviour.Flattened | Form fields are merged into static page content and are no longer editable. | The PDF is final and should be shared, printed, or archived. |
FormFieldBehaviour.Interactive | Form fields remain interactive and can be filled or edited in a PDF viewer. This is the default value. | Users should complete or edit the form after conversion. |
| Mistake | How to fix it |
|---|---|
| Expecting Aspose.HTML to flatten an existing arbitrary PDF file | Use this workflow when generating PDF from HTML or MHTML. Aspose.HTML controls flattening during conversion to PDF. |
Forgetting to set FormFieldBehaviour | Set options.FormFieldBehaviour = FormFieldBehaviour.Flattened; the default value is Interactive. |
| Flattening a PDF that should remain fillable | Use FormFieldBehaviour.Interactive when users need to complete form fields after conversion. |
| Assuming CSS alone controls PDF form behavior | Use PdfSaveOptions.FormFieldBehaviour for PDF form-field behavior; CSS controls visual styling, not interactivity in the output PDF. |
| Mixing HTML and MHTML conversion APIs | Use Converter.ConvertHTML() for HTML documents and Converter.ConvertMHTML() for MHTML streams. |
Converter.ConvertHTML() and PdfSaveOptions.Converter.ConvertMHTML().FormFieldBehaviour.The complete C# examples and data files are available in the Aspose.HTML for .NET GitHub repository. You can also test HTML to PDF output with the online HTML to PDF Converter.
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.