如何使用 IronPDF 在 C# 中平坦化 PDF 圖像

在 C# 中展平 PDF

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

IronPDF使用一行 C# 程式碼即可將 PDF 文件扁平化,將互動式表單欄位轉換為靜態內容,以防止進一步修改並確保文件完整性。

PDF 文件通常包含互動式表單,其中包含可填寫的小部件,例如單選按鈕、複選框、文字方塊和清單。 為了確保文件安全或便於存檔,需要將其設定為不可編輯狀態,IronPDF需要將 PDF 文件展平。 IronPDF 只需一行程式碼即可實現此功能。 在商業應用、法律文件或任何需要永久保存文件的場景中處理PDF 表單時,此功能至關重要。

可填寫 PDF 表單橫幅,顯示 Adob​​e PDF 徽標和兩個帶有輸入欄位的互動式表單範例文檔
帶有禁止符號的鉛筆表示編輯限製或唯讀權限
帶有紅色標題和弧形徽標的 Adob​​e PDF 文件圖標

快速入門:一行即可平整 PDF

使用IronPDF將 PDF 文件展平,移除所有互動功能,建立永久性、不可編輯的內容。 這 C# 單行程式碼載入一個現有的 PDF 文件,刪除所有可填寫的小工具,並儲存安全文件。

  1. 使用NuGet套件管理器安裝https://www.nuget.org/packages/IronPdf

    PM > Install-Package IronPdf
  2. 複製並運行這段程式碼。

    IronPdf.PdfDocument.FromFile("input.pdf").Flatten().SaveAs("flattened.pdf");
  3. 部署到您的生產環境進行測試

    今天就在您的專案中開始使用免費試用IronPDF

    arrow pointer

如何在 C# 中展平 PDF 文件?

安裝IronPDF後,您只需一行程式碼即可將 PDF 檔案展平。 此流程適用於由HTML 檔案HTML 字串或現有 PDF 文件建立的 PDF。

下面的程式碼範例使用 PdfDocument 類別來載入現有的 PDF。 若要產生動態 PDF,請使用 ChromePdfRenderer 類別。 IronPDF 的Chrome 渲染引擎可確保在展平之前準確渲染複雜形狀。

若要展平 PDF 文件,請呼叫 Flatten 方法。 這將移除所有互動式控件,包括單選按鈕、複選框和文字字段,使文件完全無法編輯。

:path=/static-assets/pdf/content-code-examples/how-to/pdf-image-flatten-csharp-flatten-pdf.cs
using IronPdf;

// Select the desired PDF File
PdfDocument pdf = PdfDocument.FromFile("before.pdf");

// Flatten the pdf
pdf.Flatten();

// Save as a new file
pdf.SaveAs("after_flatten.pdf");
using IronPdf;

// Select the desired PDF File
PdfDocument pdf = PdfDocument.FromFile("before.pdf");

// Flatten the pdf
pdf.Flatten();

// Save as a new file
pdf.SaveAs("after_flatten.pdf");
using IronPdf;

// Select the desired PDF File
PdfDocument pdf = PdfDocument.FromFile("before.pdf");

// Flatten the pdf
pdf.Flatten();

// Save as a new file
pdf.SaveAs("after_flatten.pdf");
$vbLabelText   $csharpLabel

對於複雜場景,您可以先展平特定頁面,或在展平之前操作表單資料

using IronPdf;

// Load a PDF with fillable forms
PdfDocument pdf = PdfDocument.FromFile("form-document.pdf");

// Optionally, pre-fill form fields before flattening
pdf.Form.Fields[0].Value = "John Doe";
pdf.Form.Fields[1].Value = "john@example.com";

// Flatten only specific pages (pages 1-3)
pdf.FlattenPagesRange(0, 2);

// Or flatten the entire document
pdf.Flatten();

// Save the result
pdf.SaveAs("flattened-form.pdf");
using IronPdf;

// Load a PDF with fillable forms
PdfDocument pdf = PdfDocument.FromFile("form-document.pdf");

// Optionally, pre-fill form fields before flattening
pdf.Form.Fields[0].Value = "John Doe";
pdf.Form.Fields[1].Value = "john@example.com";

// Flatten only specific pages (pages 1-3)
pdf.FlattenPagesRange(0, 2);

// Or flatten the entire document
pdf.Flatten();

// Save the result
pdf.SaveAs("flattened-form.pdf");
$vbLabelText   $csharpLabel

如何驗證PDF是否已展平?

下面的輸出顯示了處理前後的狀態。 第一個PDF文件包含可編輯的表單欄位。 應用 IronPDF 的扁平化方法後,文件將完全無法編輯。 這段程式碼適用於任何.NET項目,包括ASP.NET應用程式Blazor伺服器

使用

請注意方法後,表單將無法被偵測到。 Flatten 方法。

若要驗證是否成功扁平化,請檢查表單欄位數量:

using IronPdf;

// Load the flattened PDF
PdfDocument flattenedPdf = PdfDocument.FromFile("flattened.pdf");

// Check if any form fields exist
if (flattenedPdf.Form.Fields.Count == 0)
{
    Console.WriteLine("PDF has been successfully flattened - no interactive fields remain.");
}
else
{
    Console.WriteLine($"Warning: {flattenedPdf.Form.Fields.Count} form fields still exist.");
}
using IronPdf;

// Load the flattened PDF
PdfDocument flattenedPdf = PdfDocument.FromFile("flattened.pdf");

// Check if any form fields exist
if (flattenedPdf.Form.Fields.Count == 0)
{
    Console.WriteLine("PDF has been successfully flattened - no interactive fields remain.");
}
else
{
    Console.WriteLine($"Warning: {flattenedPdf.Form.Fields.Count} form fields still exist.");
}
$vbLabelText   $csharpLabel

表單欄位扁平化後會發生什麼變化?

將 PDF 文件展平後,所有互動式表單元素都會永久性變更。 表單欄位會轉換為靜態頁面內容,成為文件視覺層的一部分:

-文字欄位會變成頁面上的普通文本 複選框和單選按鈕會變成靜態影像,顯示它們的選取狀態。 下拉式選單僅以純文字顯示所選值。 數位簽名在視覺上得以保留,但失去了加密驗證功能。

這個過程是不可逆的。 如果將來需要進行編輯,請保留原始互動式 PDF 檔案的副本。 對於既需要安全性又需要可編輯性的文檔,請使用PDF 權限和密碼,而不是將其展平。

何時應該將PDF文件展平?

在以下業務場景中,PDF 扁平化至關重要:

1.法律文件歸檔:簽署後將合約和協議展平,以防止內容被篡改,並維護法律完整性。

2.報表分發:在分發之前,將包含計算欄位的財務報表和資料表展平,以防止竄改。

3.表單提交處理:使用者完成線上表單後,將 PDF 檔案展平,建立永久記錄。

4.列印優化:扁平化的 PDF 檔案列印起來更可靠,因為印表機不會處理互動式元素。

5.檔案大小減少:使用PDF 壓縮時,扁平化可以透過刪除表單欄位資料結構來減少檔案大小。

以下是一個批次處理範例,用於歸檔多個已完成的表單:

using IronPdf;
using System.IO;

public class BatchPdfFlattener
{
    public static void FlattenAllPdfsInDirectory(string sourceDir, string outputDir)
    {
        // Ensure output directory exists
        Directory.CreateDirectory(outputDir);

        // Get all PDF files in source directory
        string[] pdfFiles = Directory.GetFiles(sourceDir, "*.pdf");

        foreach (string pdfFile in pdfFiles)
        {
            try
            {
                // Load the PDF
                PdfDocument pdf = PdfDocument.FromFile(pdfFile);

                // Flatten the document
                pdf.Flatten();

                // Save to output directory with "_flattened" suffix
                string fileName = Path.GetFileNameWithoutExtension(pdfFile);
                string outputPath = Path.Combine(outputDir, $"{fileName}_flattened.pdf");
                pdf.SaveAs(outputPath);

                Console.WriteLine($"Flattened: {fileName}");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error processing {pdfFile}: {ex.Message}");
            }
        }
    }
}
using IronPdf;
using System.IO;

public class BatchPdfFlattener
{
    public static void FlattenAllPdfsInDirectory(string sourceDir, string outputDir)
    {
        // Ensure output directory exists
        Directory.CreateDirectory(outputDir);

        // Get all PDF files in source directory
        string[] pdfFiles = Directory.GetFiles(sourceDir, "*.pdf");

        foreach (string pdfFile in pdfFiles)
        {
            try
            {
                // Load the PDF
                PdfDocument pdf = PdfDocument.FromFile(pdfFile);

                // Flatten the document
                pdf.Flatten();

                // Save to output directory with "_flattened" suffix
                string fileName = Path.GetFileNameWithoutExtension(pdfFile);
                string outputPath = Path.Combine(outputDir, $"{fileName}_flattened.pdf");
                pdf.SaveAs(outputPath);

                Console.WriteLine($"Flattened: {fileName}");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error processing {pdfFile}: {ex.Message}");
            }
        }
    }
}
$vbLabelText   $csharpLabel

有關進階 PDF 操作技巧,包括展平後合併或分割 PDF ,請參閱 IronPDF 的綜合文件。


圖書館快速訪問

文件

閱讀更多文檔

閱讀文檔,以了解更多有關如何展平 PDF、編輯和操作 PDF 等的資訊。

訪問IronPDF文檔

準備好要看看你還能做什麼了嗎? 請造訪我們的教學頁面:附加功能

常見問題解答

壓平 PDF 是什麼意思?

扁平化 PDF 可將所有互動式表單欄位(如檢查方塊、文字方塊和單選按鈕)轉換為靜態、不可編輯的內容。IronPDF 提供此功能以確保文件的完整性,並防止進一步的修改。

如何用 C# 將 PDF 平面化?

使用 IronPDF,您只需要一行代碼就可以將 PDF 扁平化:IronPdf.PdfDocument.FromFile("input.pdf").Flatten().SaveAs("flattened.pdf").這會載入 PDF,移除所有互動元素,並儲存安全的文件。

我可以扁平化特定頁面而非整個文件嗎?

是的,IronPDF 允許您使用 FlattenPagesRange 方法來壓平特定頁面。例如,pdf.FlattenPagesRange(0, 2) 將只壓平文件的第 1-3 頁,而保留其他互動頁面。

哪些類型的表單欄位能被扁平化?

IronPDF 可以扁平化所有互動式小工具,包括單選按鈕、複選框、文字欄位、下拉式清單,以及任何其他可填寫的表單元素,將它們轉換為永久的靜態內容。

我可以在壓平 PDF 之前填寫表格欄位嗎?

是的,IronPDF 允許您在扁平化之前預先填入表格欄位。您可以在呼叫 Flatten 方法之前,先設定 pdf.Form.Fields[0].Value = "John Doe" 之類的值,以建立一個完成的、不可編輯的文件。

PDF 平面化過程使用何種渲染引擎?

IronPDF 使用 Chrome 渲染引擎,確保在扁平化之前準確渲染複雜表格,在整個過程中保持文件的視覺完整性。

為什麼我需要壓平 PDF 文件?

使用 IronPDF 對 PDF 進行扁平化處理,對於安全性、歸檔目的、法律文件或任何需要永久保存文檔的場合(您需要防止進一步修改表格資料)而言,都是不可或缺的。

Curtis Chau
技術作家

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

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

準備好開始了嗎?
Nuget 下載 17,803,474 | 版本: 2026.3 剛剛發布
Still Scrolling Icon

還在滾動嗎?

想快速取得證據? PM > Install-Package IronPdf
運行範例看著你的HTML程式碼變成PDF檔。