Flatten PDF in C#

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.

What Does It Mean to Flatten a PDF?

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.

When to Flatten a PDF

Flatten a PDF when the output should be fixed after conversion:

Flatten HTML to PDF in C#

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:

  1. Load the source HTML document with HTMLDocument.
  2. Create a PdfSaveOptions instance.
  3. Set options.FormFieldBehaviour = FormFieldBehaviour.Flattened.
  4. Call Converter.ConvertHTML() with the document, options, and output path.
  5. Save the resulting PDF with form fields flattened into static content.
 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);

Flatten MHTML to PDF in C#

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:

  1. Open the source MHTML file as a stream.
  2. Create a PdfSaveOptions instance.
  3. Set options.FormFieldBehaviour = FormFieldBehaviour.Flattened.
  4. Call Converter.ConvertMHTML(stream, options, outputPath).
  5. Save the PDF with non-interactive form-field output.
 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 Values

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.

ValueResult in the output PDFUse it when
FormFieldBehaviour.FlattenedForm fields are merged into static page content and are no longer editable.The PDF is final and should be shared, printed, or archived.
FormFieldBehaviour.InteractiveForm 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.

Common PDF Flattening Mistakes

MistakeHow to fix it
Expecting Aspose.HTML to flatten an existing arbitrary PDF fileUse this workflow when generating PDF from HTML or MHTML. Aspose.HTML controls flattening during conversion to PDF.
Forgetting to set FormFieldBehaviourSet options.FormFieldBehaviour = FormFieldBehaviour.Flattened; the default value is Interactive.
Flattening a PDF that should remain fillableUse FormFieldBehaviour.Interactive when users need to complete form fields after conversion.
Assuming CSS alone controls PDF form behaviorUse PdfSaveOptions.FormFieldBehaviour for PDF form-field behavior; CSS controls visual styling, not interactivity in the output PDF.
Mixing HTML and MHTML conversion APIsUse Converter.ConvertHTML() for HTML documents and Converter.ConvertMHTML() for MHTML streams.

Related PDF Workflows

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.