如何在 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 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。

準備好開始了嗎?
Nuget 下載 38,930 | 版本: 2026.4 剛剛發布
Still Scrolling Icon

還在捲動嗎?

想要快速證明? PM > Install-Package IronPrint
執行範例 觀看您的文件打到印表機上。