如何在 C# 中打印前对 PDF 进行扁平化处理

This article was translated from English: Does it need improvement?
Translated
View the article in English

IronPrint 的 PrintSettings 类包含一个 Flatten 属性,该属性可在打印时将所有交互式表单字段、注释和图像图层合并为静态内容。 我们只需在 PrintSettings 对象上设置 Flatten = true,剩下的工作就交给 IronPrint 处理——无需单独的 PDF 处理库,也无需保存中间文件。

本指南介绍了在打印前何时以及如何对 PDF 进行扁平化处理,并提供了适用于同步、组合设置和异步工作流的 C# 示例代码。

快速入门:打印前将 PDF 文件扁平化

  1. 通过 NuGet 安装 IronPrint:Install-Package IronPrint
  2. 在文件中添加 using IronPrint;
  3. 创建一个 PrintSettings 对象
  4. Flatten 设置为 true
  5. 将设置传递给 Printer.Print()Printer.PrintAsync()
  1. 使用 NuGet 包管理器安装 https://www.nuget.org/packages/IronPrint

    PM > Install-Package IronPrint
  2. 复制并运行这段代码。

    using IronPrint;
    
    // Flatten and print a PDF with form fields
    Printer.Print("form-document.pdf", new PrintSettings
    {
        Flatten = true
    });
  3. 部署到您的生产环境中进行测试

    通过免费试用立即在您的项目中开始使用IronPrint

    arrow pointer

如何在 C# 中打印 PDF 之前将其扁平化?

我们在打印时通过将 PrintSettings 对象的 Flatten 属性设置为 true 来对 PDF 进行扁平化处理。 然后我们将该对象传递给 IronPrint 的任何打印方法

:path=/static-assets/print/content-code-examples/how-to/flatten-pdfs/flatten-pdfs-flatten-basic.cs
using IronPrint;

// Configure print settings with flattening enabled
PrintSettings settings = new PrintSettings();
settings.Flatten = true;

// Send the flattened document to the default printer
Printer.Print("application-form.pdf", settings);
Imports IronPrint

' Configure print settings with flattening enabled
Dim settings As New PrintSettings()
settings.Flatten = True

' Send the flattened document to the default printer
Printer.Print("application-form.pdf", settings)
$vbLabelText   $csharpLabel

首先实例化 PrintSettings,其初始化时会使用默认值——包括 Flatten = false。 随后我们将 Flatten 设置为 true,这会指示 IronPrint 在发送打印任务前,将所有交互元素合并到页面内容中。 当我们调用 Printer.Print 时,打印机将收到文档的完全静态版本。

在打印流程中,文件会在内存中进行扁平化处理。原始 PDF 文件在磁盘上仍保持交互性,因此最终用户仍可填写并重新提交表单。 这一点至关重要:我们是为了打印而进行扁平化处理,而非永久性地修改源文件。

Flatten 属性于 IronPrint v2024.7.2 版本中引入,并在所有后续版本中均可用。

何时应在打印PDF前对其进行扁平化处理?

当 PDF 包含可能无法在纸张上正确呈现的交互式元素时,平铺功能最为有用。 最常见的应用场景包括:

包含可填写表单字段的 PDF 文件。文本输入框、复选框、单选按钮和下拉菜单由 PDF 阅读器呈现,而非嵌入页面内容中。 某些打印机驱动程序会完全跳过这些层,导致打印页面上出现空白区域。 扁平化会将字段值强制映射到静态页面层。

带有注释或叠加层的 PDF 文件。评论、便签、图章和标注注释位于单独的图层上。 扁平化处理会将其合并到可见内容中,使其出现在打印输出中。

包含不同图层嵌入图像的 PDF 文件。由多个来源组合而成的文档可能包含位于不同 Z 图层上的图像。 将所有复合图层扁平化为单层,防止图像缺失或对齐错误。

当 PDF 仅包含静态文本和图像时(例如由 HTML 生成的报告或扫描文档),扁平化操作将不起作用。 在这种情况下,我们可以将 Flatten 保留为默认值 false,以避免不必要的处理开销。

如何将"Flatten"与其他打印设置结合使用?

PrintSettings 类提供了若干属性,同时还提供了 Flatten。 我们可以在单个打印任务中配置纸张尺寸方向DPI边距和复印份数,并同时进行扁平化处理。

:path=/static-assets/print/content-code-examples/how-to/flatten-pdfs/flatten-pdfs-combined-settings.cs
using IronPrint;

// Flatten and print with full configuration
PrintSettings settings = new PrintSettings
{
    Flatten = true,
    PaperSize = PaperSize.A4,
    PaperOrientation = PaperOrientation.Portrait,
    Dpi = 300,
    NumberOfCopies = 2,
    PaperMargins = new Margins(10, 10, 10, 10),
    Grayscale = false
};

// Print a filled-out insurance claim form
Printer.Print("insurance-claim.pdf", settings);
Imports IronPrint

' Flatten and print with full configuration
Dim settings As New PrintSettings With {
    .Flatten = True,
    .PaperSize = PaperSize.A4,
    .PaperOrientation = PaperOrientation.Portrait,
    .Dpi = 300,
    .NumberOfCopies = 2,
    .PaperMargins = New Margins(10, 10, 10, 10),
    .Grayscale = False
}

' Print a filled-out insurance claim form
Printer.Print("insurance-claim.pdf", settings)
$vbLabelText   $csharpLabel

为确保清晰度,我们采用了对象初始化器语法。 Flatten = true 确保所有表单数据均显示在打印页面上。 PaperSize.A4PaperOrientation.Portrait 符合商业表格的标准格式。 Dpi 在 300 像素下,可在原本以屏幕分辨率渲染的填充字段上生成清晰的文本。 PaperMargins 通过 Margins 构造函数接受四个以毫米为单位的数值。

这些要求之间并不冲突。 IronPrint 首先执行"扁平化"操作,然后根据剩余设置对生成的静态文档进行格式化,最后将打印任务发送至打印机。 有关打印机选择纸盒配置等更多打印设置选项,请参阅完整的配置指南。

如何进行扁平化处理并异步打印?

对于不允许阻塞主线程的应用程序(例如 WPF 或 WinForms 应用程序),我们使用 Printer.PrintAsync。 该方法接受相同的 PrintSettings 对象,并返回一个 Task

:path=/static-assets/print/content-code-examples/how-to/flatten-pdfs/flatten-pdfs-async-flatten.cs
using IronPrint;
using System.Threading.Tasks;

public class FormPrinter
{
    public async Task PrintFlattenedFormAsync(string pdfPath)
    {
        // Flatten form fields and print asynchronously
        PrintSettings settings = new PrintSettings
        {
            Flatten = true,
            PaperSize = PaperSize.Letter,
            Dpi = 300
        };

        // Non-blocking print — UI thread stays responsive
        await Printer.PrintAsync(pdfPath, settings);
    }
}
Imports IronPrint
Imports System.Threading.Tasks

Public Class FormPrinter
    Public Async Function PrintFlattenedFormAsync(pdfPath As String) As Task
        ' Flatten form fields and print asynchronously
        Dim settings As New PrintSettings With {
            .Flatten = True,
            .PaperSize = PaperSize.Letter,
            .Dpi = 300
        }

        ' Non-blocking print — UI thread stays responsive
        Await Printer.PrintAsync(pdfPath, settings)
    End Function
End Class
$vbLabelText   $csharpLabel

当打印逻辑位于服务层时,这种基于类的模式非常有用。 对于美国格式的表单,我们将 Flatten = truePaperSize.Letter 配合使用。 await 关键字会在 IronPrint 处理扁平化操作并发送打印任务时释放调用线程。

IronPrint 的异步方法(包括 PrintAsyncShowPrintDialogAsync)均与它们的同步对应方法一样,完全遵循 Flatten 设置。 无论我们是静默打印还是通过打印对话框进行打印,扁平化处理的行为始终保持一致。

我的下一个步骤是什么?

我们介绍了如何在 C# 中使用 IronPrint 的 PrintSettings.Flatten 属性,在打印前对 PDF 进行扁平化处理。 关键要点:将 Flatten = true 设置为在打印时将交互式表单字段、注释和图像图层合并到静态内容中——无需修改源文件。

在此基础上继续完善:

立即开始 30 天试用,在您的打印工作流中测试 PDF 扁平化功能,或查看用于生产环境的许可选项

Curtis Chau
技术作家

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。

除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。

准备开始了吗?
Nuget 下载 38,930 | 版本: 2026.4 刚刚发布
Still Scrolling Icon

还在滚动吗?

想快速获得证据? PM > Install-Package IronPrint
运行示例 观看您的文档打到打印机上。