C#でPDFをフラットにする
IronPDFはC#のPDFドキュメントを1行のコードで平坦化し、インタラクティブなフォームフィールドを静的コンテンツに変換することで、それ以上の変更を防ぎ、ドキュメントの整合性を確保します。
PDF文書には、ラジオボタン、チェックボックス、テキストボックス、リストなどの記入可能なウィジェットを備えたインタラクティブなフォームが含まれることがよくあります。 セキュリティやアーカイブの目的でこれらのドキュメントを編集できないようにするには、PDFファイルをフラットにする必要があります。IronPdfはこの機能をたった1行のコードで提供します。 この能力は、ビジネスアプリケーション、法律文書、または恒久的な文書保存を必要とするシナリオでPDFフォームを扱う場合に不可欠です。
クイックスタート: 1 行で PDF をフラット化する
IronPDFを使用してPDFドキュメントを平坦化し、すべてのインタラクティブ性を削除し、永久に編集不可能なコンテンツを作成します。 このC#ワンライナーは、既存のPDFを読み込み、すべての塗りつぶし可能なウィジェットを削除し、保護されたドキュメントを保存します。
最小限のワークフロー(5ステップ)
- NuGetパッケージマネージャからIronPDFをインストールする。
- 既存のPDFを読み込むか、HTMLから新規作成
- `Flatten`メソッドを呼び出す
- フラット化されたPDFドキュメントを保存する
- フォームフィールドが削除されていることを確認する
C#で PDF ドキュメントをフラットにするにはどうすればよいですか?
IronPDFをインストールすると、1行のコードで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のフラット化メソッドを適用すると、ドキュメントは完全に編集不可能になります。 このコードは、ASP.NETアプリケーションやBlazorサーバーを含む、あらゆる.NETプロジェクトで動作します。
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を使えば、1行のコードで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を平坦化することは、セキュリティ、アーカイブ目的、法的文書、あるいはフォームデータのさらなる変更を防ぐ必要のある永久的な文書保存を必要とするシナリオに不可欠です。

