如何在 C# 中展平 PDF 文件並防止 PDF編輯
IronPDF使用一行 C# 程式碼即可將 PDF 文件扁平化,將互動式表單欄位轉換為靜態內容,以防止進一步修改並確保文件完整性。
PDF 文件通常包含互動式表單,其中包含可填寫的小部件,例如單選按鈕、複選框、文字方塊和清單。 為了確保文件安全或便於存檔,需要將其設定為不可編輯狀態,IronPDF需要將 PDF 文件展平。 IronPDF 只需一行程式碼即可實現此功能。 在商業應用、法律文件或任何需要永久保存文件的場景中處理PDF 表單時,此功能至關重要。
快速入門:一行即可平整 PDF
使用IronPDF將 PDF 文件展平,移除所有互動功能,建立永久性、不可編輯的內容。 這 C# 單行程式碼載入一個現有的 PDF 文件,刪除所有可填寫的小工具,並儲存安全文件。
最簡工作流程(5個步驟)
- 從NuGet套件管理器安裝IronPDF
- 載入現有 PDF 或從 HTML 建立新 PDF
- 呼叫`Flatten`方法
- 儲存已展平的 PDF 文檔
- 確認表單欄位已刪除
如何在 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");
Imports IronPdf
' Select the desired PDF File
Dim pdf As PdfDocument = PdfDocument.FromFile("before.pdf")
' Flatten the pdf
pdf.Flatten()
' Save as a new file
pdf.SaveAs("after_flatten.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");
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");
Imports IronPdf
' Load a PDF with fillable forms
Dim pdf As PdfDocument = 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")
如何驗證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.");
}
Imports IronPdf
' Load the flattened PDF
Dim flattenedPdf As PdfDocument = PdfDocument.FromFile("flattened.pdf")
' Check if any form fields exist
If flattenedPdf.Form.Fields.Count = 0 Then
Console.WriteLine("PDF has been successfully flattened - no interactive fields remain.")
Else
Console.WriteLine($"Warning: {flattenedPdf.Form.Fields.Count} form fields still exist.")
End If
表單欄位扁平化後會發生什麼變化?
將 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}");
}
}
}
}
Imports IronPdf
Imports System.IO
Public Class BatchPdfFlattener
Public Shared Sub FlattenAllPdfsInDirectory(sourceDir As String, outputDir As String)
' Ensure output directory exists
Directory.CreateDirectory(outputDir)
' Get all PDF files in source directory
Dim pdfFiles As String() = Directory.GetFiles(sourceDir, "*.pdf")
For Each pdfFile As String In pdfFiles
Try
' Load the PDF
Dim pdf As PdfDocument = PdfDocument.FromFile(pdfFile)
' Flatten the document
pdf.Flatten()
' Save to output directory with "_flattened" suffix
Dim fileName As String = Path.GetFileNameWithoutExtension(pdfFile)
Dim outputPath As String = Path.Combine(outputDir, $"{fileName}_flattened.pdf")
pdf.SaveAs(outputPath)
Console.WriteLine($"Flattened: {fileName}")
Catch ex As Exception
Console.WriteLine($"Error processing {pdfFile}: {ex.Message}")
End Try
Next
End Sub
End Class
有關進階 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 進行扁平化處理,對於安全性、歸檔目的、法律文件或任何需要永久保存文檔的場合(您需要防止進一步修改表格資料)而言,都是不可或缺的。

