PDF interactive form flattening is the process of making a PDF document non-interactive by taking the appearance of form fields, incorporating their appearance into the static contents of the PDF page containing them, and finally removing the form fields.
In the realm of digital document management, interactive PDF forms are a powerful tool for collecting data. However, there are scenarios where you might need to convert these interactive forms into a static format. This process is known as "flattening" PDF forms. Flattening a PDF form means converting the interactive form fields into static content, making the document non-interactive. This article will guide you through the process of flattening PDF interactive form fields using C# and .NET with the help of the SautinSoft PDF.Net library.
Pay attention to what the smoothing of interactive forms is for:
Step-by-Step Guide:
Полный код
using System;
using System.IO;
using SautinSoft;
using SautinSoft.Pdf;
using SautinSoft.Pdf.Content;
using SautinSoft.Pdf.Forms;
namespace Sample
{
class Sample
{
/// <summary>
/// Fill in PDF interactive forms.
/// </summary>
/// <remarks>
/// Details: https://sautinsoft.com/products/pdf/help/net/developer-guide/flatten-interactive-forms.php
/// </remarks>
static void Main(string[] args)
{
// Before starting this example, please get a free 100-day trial key:
// https://sautinsoft.com/start-for-free/
// Apply the key here:
// PdfDocument.SetLicense("...");
string pdfFile = Path.GetFullPath(@"..\..\..\FormFilled.pdf");
using (var document = PdfDocument.Load(pdfFile))
{
// A flag specifying whether to construct appearance for all form fields in the document.
bool needAppearances = document.Form.NeedAppearances;
foreach (var field in document.Form.Fields)
{
// Do not flatten button fields.
if (field.FieldType == PdfFieldType.Button)
continue;
// Construct appearance, if needed.
if (needAppearances)
field.Appearance.Refresh();
// Get the field's appearance form.
var fieldAppearance = field.Appearance.Get();
// If the field doesn't have an appearance, skip it.
if (fieldAppearance == null)
continue;
// Add a new content group to the field's page and
// add new form content with the field's appearance form to the content group.
// The content group is added so that transformation from the next statement is localized to the content group.
var flattenedContent = field.Page.Content.Elements.AddGroup().Elements.AddForm(fieldAppearance);
// Translate the form content to the same position on the page that the field is in.
var fieldBounds = field.Bounds;
flattenedContent.Transform = PdfMatrix.CreateTranslation(fieldBounds.Left, fieldBounds.Bottom);
}
// Remove all fields, thus making the document non-interactive,
// since their appearance is now contained directly in the content of their pages.
document.Form.Fields.Clear();
document.Save("FormFlattened.pdf");
}
System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo("FormFlattened.pdf") { UseShellExecute = true });
}
}
}
Option Infer On
Imports System
Imports System.IO
Imports SautinSoft
Imports SautinSoft.Pdf
Imports SautinSoft.Pdf.Content
Imports SautinSoft.Pdf.Forms
Namespace Sample
Friend Class Sample
''' <summary>
''' Fill in PDF interactive forms.
''' </summary>
''' <remarks>
''' Details: https://sautinsoft.com/products/pdf/help/net/developer-guide/flatten-interactive-forms.php
''' </remarks>
Shared Sub Main(ByVal args() As String)
' Before starting this example, please get a free 100-day trial key:
' https://sautinsoft.com/start-for-free/
' Apply the key here:
' PdfDocument.SetLicense("...");
Dim pdfFile As String = Path.GetFullPath("..\..\..\FormFilled.pdf")
Using document = PdfDocument.Load(pdfFile)
' A flag specifying whether to construct appearance for all form fields in the document.
Dim needAppearances As Boolean = document.Form.NeedAppearances
For Each field In document.Form.Fields
' Do not flatten button fields.
If field.FieldType = PdfFieldType.Button Then
Continue For
End If
' Construct appearance, if needed.
If needAppearances Then
field.Appearance.Refresh()
End If
' Get the field's appearance form.
Dim fieldAppearance = field.Appearance.Get()
' If the field doesn't have an appearance, skip it.
If fieldAppearance Is Nothing Then
Continue For
End If
' Add a new content group to the field's page and
' add new form content with the field's appearance form to the content group.
' The content group is added so that transformation from the next statement is localized to the content group.
Dim flattenedContent = field.Page.Content.Elements.AddGroup().Elements.AddForm(fieldAppearance)
' Translate the form content to the same position on the page that the field is in.
Dim fieldBounds = field.Bounds
flattenedContent.Transform = PdfMatrix.CreateTranslation(fieldBounds.Left, fieldBounds.Bottom)
Next field
' Remove all fields, thus making the document non-interactive,
' since their appearance is now contained directly in the content of their pages.
document.Form.Fields.Clear()
document.Save("FormFlattened.pdf")
End Using
System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo("FormFlattened.pdf") With {.UseShellExecute = True})
End Sub
End Class
End Namespace
Если вам нужен пример кода или у вас есть вопрос: напишите нам по адресу support@sautinsoft.ru или спросите в онлайн-чате (правый нижний угол этой страницы) или используйте форму ниже: