C# で PDF を編集する方法 — IronPDF 完全チュートリアル
Iron Software は IronPDF ライブラリの PDF 編集機能をわかりやすいメソッドに集約しました。署名の追加、HTML フッターの追加、透かしのスタンプ、注釈の追加など、あらゆる PDF 編集操作に対応しています。IronPDF を使えば、読みやすいコードでプログラムによる PDF 編集が可能で、デバッグも簡単、サポートされている任意の環境やプラットフォームにシームレスにデプロイできます。
IronPDFは、PDF編集に関する豊富な機能を誇ります。 このチュートリアル記事では、主要な機能のいくつかを紹介し、コード例と説明を提供します。
この記事を通じて、C#でPDFを編集するためのIronPDFの使用方法をよく理解できるようになります。
クイックスタート:C# で PDF を数秒で編集する
IronPDFを使用して、C#でPDFドキュメントを手軽に編集できます。 このクイックガイドでは、既存のPDFファイルにテキストスタンプを追加する方法を示します。わずか数行のコードでPDFを変更し、変更を即座に保存することができます。 迅速かつ効率的なPDF編集ソリューションを必要とする開発者に最適です。
目次
- ドキュメント構造の編集
- PDF DOMオブジェクトへのアクセス
- PDFドキュメントの保存とエクスポート
- メモリからのPDFのロード
- メモリへのPDFのエクスポート
- ドキュメントプロパティの編集
- C#でのPDF解析
- テキストと画像の抽出
- テキストと領域の修正
- PDF内のテキストの置換
- PDFデザインの向上
- 注釈の追加と編集
- テキストと画像のスタンプ
- カスタム透かし
- 背景と前景
- テキストとビットマップの描画
- 線と長方形の描画
- テキストとページの回転
- PDFページの変形
ドキュメント構造の編集
PDF DOMオブジェクトへのアクセス
PDFオブジェクトの操作とアクセスは迅速かつ簡単です。 IronPDFは、開発者がWebページのDOMを操作する方法を理解しやすくすることで、DOMオブジェクトとの対話を単純化し、テキストなどのさまざまな要素にプログラムによってアクセスし操作することを可能にします。
:path=/static-assets/pdf/content-code-examples/how-to/access-pdf-dom-object.cs
using IronPdf;
using System.Linq;
// Instantiate Renderer
ChromePdfRenderer renderer = new ChromePdfRenderer();
// Create a PDF from a URL
PdfDocument pdf = renderer.RenderUrlAsPdf("https://ironpdf.com/");
// Access DOM Objects
var objects = pdf.Pages.First().ObjectModel;
Imports IronPdf
Imports System.Linq
' Instantiate Renderer
Private renderer As New ChromePdfRenderer()
' Create a PDF from a URL
Private pdf As PdfDocument = renderer.RenderUrlAsPdf("https://ironpdf.com/")
' Access DOM Objects
Private objects = pdf.Pages.First().ObjectModel
このコードスニペットの詳細な説明と追加機能については、当社の包括的なハウツーガイドを参照してください。
ドキュメントの保存とエクスポート
ドキュメントを保存およびエクスポートするために、 IronPDF、編集したドキュメントをディスクにすばやく保存できるだけでなく、バイナリ データやメモリ ストリームなどの他の形式にエクスポートすることもできます。
このコードスニペットの詳細な説明と追加機能については、当社の包括的なハウツーガイドを参照してください。
メモリからPDFのロード
IronPDFは、既存のC# .NETアプリケーションと完全に統合されています; MemoryStream オブジェクトを介して MemoryStreams から PDF ファイルを読み込み、作成することができます。
:path=/static-assets/pdf/content-code-examples/how-to/pdf-memory-stream-from-stream.cs
using IronPdf;
using System.IO;
// Read PDF file as stream
var fileByte = File.ReadAllBytes("sample.pdf");
// Instantiate PDF object from stream
PdfDocument pdf = new PdfDocument(fileByte);
Imports IronPdf
Imports System.IO
' Read PDF file as stream
Private fileByte = File.ReadAllBytes("sample.pdf")
' Instantiate PDF object from stream
Private pdf As New PdfDocument(fileByte)
このコードスニペットの詳細な説明と追加機能については、当社の包括的なハウツーガイドを参照してください。
メモリへのPDFのエクスポート
同様に、MemoryStream オブジェクトを介して C# .NETの MemoryStream に PDF をエクスポートすることもできます。
:path=/static-assets/pdf/content-code-examples/how-to/pdf-to-memory-stream-to-stream.cs
using IronPdf;
using System.IO;
var renderer = new ChromePdfRenderer();
// Convert the URL into PDF
PdfDocument pdf = renderer.RenderUrlAsPdf("https://ironpdf.com/");
// Export PDF as Stream
MemoryStream pdfAsStream = pdf.Stream;
// Export PDF as Byte Array
byte[] pdfAsByte = pdf.BinaryData;
Imports IronPdf
Imports System.IO
Private renderer = New ChromePdfRenderer()
' Convert the URL into PDF
Private pdf As PdfDocument = renderer.RenderUrlAsPdf("https://ironpdf.com/")
' Export PDF as Stream
Private pdfAsStream As MemoryStream = pdf.Stream
' Export PDF as Byte Array
Private pdfAsByte() As Byte = pdf.BinaryData
このコードスニペットの詳細な説明と追加機能については、当社の包括的なハウツーガイドを参照してください。
ドキュメントテキストの編集
Parse PDFs in C
PDFからテキストを抽出することは、IronPDFを使用すると迅速かつ簡単です。 ExtractAllText メソッドを使用するだけで、すべてのページからテキストを取得でき、ドキュメントのコンテンツに簡単にアクセスして利用できるようになります。 この強力な機能は生産性を向上させ、ワークフローを合理化します!
:path=/static-assets/pdf/content-code-examples/how-to/csharp-parse-pdf-parse-pdf.cs
using IronPdf;
// Select the desired PDF File
PdfDocument pdf = PdfDocument.FromFile("sample.pdf");
// Extract all text from an pdf
string allText = pdf.ExtractAllText();
// Extract all text from page 1
string page1Text = pdf.ExtractTextFromPage(0);
Imports IronPdf
' Select the desired PDF File
Private pdf As PdfDocument = PdfDocument.FromFile("sample.pdf")
' Extract all text from an pdf
Private allText As String = pdf.ExtractAllText()
' Extract all text from page 1
Private page1Text As String = pdf.ExtractTextFromPage(0)
このコードスニペットの詳細な説明と追加機能については、当社の包括的なハウツーガイドを参照してください。
テキストと画像の抽出
IronPDFを使用すると、テキストを抽出することに限らず、PDFからの画像の抽出も非常に簡単です! ExtractAllImages 機能を使用すると、必要なすべてのビジュアルをすばやくキャプチャできます。
:path=/static-assets/pdf/content-code-examples/how-to/extract-text-and-images-extract-image.cs
using IronPdf;
PdfDocument pdf = PdfDocument.FromFile("sample.pdf");
// Extract images
var images = pdf.ExtractAllImages();
for(int i = 0; i < images.Count; i++)
{
// Export the extracted images
images[i].SaveAs($"images/image{i}.png");
}
Imports IronPdf
Private pdf As PdfDocument = PdfDocument.FromFile("sample.pdf")
' Extract images
Private images = pdf.ExtractAllImages()
For i As Integer = 0 To images.Count - 1
' Export the extracted images
images(i).SaveAs($"images/image{i}.png")
Next i
このコードスニペットの詳細な説明と追加機能については、当社の包括的なハウツーガイドを参照してください。
テキストと領域の修正
機密情報を編集して保護する必要があるシナリオでは、強力なツールがすぐに利用できます: RedactTextonAllPages。 この機能を使用すると、特定のキーワードをPDF全体で簡単に見つけて隠すことができます。 これにより、機密情報を保護しながら、ドキュメントを安心して共有することが可能です。
:path=/static-assets/pdf/content-code-examples/how-to/redact-text-redact-text.cs
using IronPdf;
PdfDocument pdf = PdfDocument.FromFile("novel.pdf");
// Redact 'Alaric' phrase from all pages
pdf.RedactTextOnAllPages("Alaric");
pdf.SaveAs("redacted.pdf");
Imports IronPdf
Private pdf As PdfDocument = PdfDocument.FromFile("novel.pdf")
' Redact 'Alaric' phrase from all pages
pdf.RedactTextOnAllPages("Alaric")
pdf.SaveAs("redacted.pdf")
このコードスニペットの詳細な説明と追加機能については、当社の包括的なハウツーガイドを参照してください。
PDF内のテキストの置換
IronPDFを使ってPDF文書を強化するには、ファイル全体のテキストを簡単に置換できます。ReplaceTextonAllPages 関数を利用することで、置換対象の oldText と、その代替となる newText を指定できます。 この方法は、ドキュメント全体の oldText のすべてのインスタンスを効率的に更新し、一貫性のあるプロフェッショナルな外観を保証します。
:path=/static-assets/pdf/content-code-examples/how-to/find-replace-text-all-page.cs
using IronPdf;
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>.NET6</h1>");
string oldText = ".NET6";
string newText = ".NET7";
// Replace text on all pages
pdf.ReplaceTextOnAllPages(oldText, newText);
pdf.SaveAs("replaceText.pdf");
Imports IronPdf
Private renderer As New ChromePdfRenderer()
Private pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>.NET6</h1>")
Private oldText As String = ".NET6"
Private newText As String = ".NET7"
' Replace text on all pages
pdf.ReplaceTextOnAllPages(oldText, newText)
pdf.SaveAs("replaceText.pdf")
このコードスニペットの詳細な説明とその追加機能を探るには、私たちの包括的な ハウツーガイド を参照してください。
PDFデザインを向上させる
注釈の追加と編集
IronPDFはPDF注釈に関する幅広いカスタマイズオプションを提供しており、"付箋"スタイルのコメントをPDFページに直接追加することができます。 TextAnnotation クラスを使用すると、サイズ変更、不透明度、アイコンの選択、編集機能などの高度なオプションを備えた注釈をプログラムで追加できます。
:path=/static-assets/pdf/content-code-examples/how-to/annotation-add-annotation.cs
using IronPdf;
using IronPdf.Annotations;
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Annotation</h1>");
// Create a PDF annotation object on a specified page index
TextAnnotation annotation = new TextAnnotation(0)
{
Title = "This is the title",
Contents = "This is the long 'sticky note' comment content...",
X = 50,
Y = 700,
};
// Add the annotation
pdf.Annotations.Add(annotation);
pdf.SaveAs("annotation.pdf");
Imports IronPdf
Imports IronPdf.Annotations
Dim renderer As New ChromePdfRenderer()
Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Annotation</h1>")
' Create a PDF annotation object on a specified page index
Dim annotation As New TextAnnotation(0) With {
.Title = "This is the title",
.Contents = "This is the long 'sticky note' comment content...",
.X = 50,
.Y = 700
}
' Add the annotation
pdf.Annotations.Add(annotation)
pdf.SaveAs("annotation.pdf")
このコードスニペットの詳細な説明と追加機能については、当社の包括的なハウツーガイドを参照してください。
テキストと画像のスタンプ
IronPDFはテキストと画像をPDFにスタンプするカスタマイズ方法に豊富なオプションを提供しています。 この例では、以下に示すように、TextStamper クラスを使用して、ApplyStamp メソッドで PDF にスタンプを適用します。
:path=/static-assets/pdf/content-code-examples/how-to/stamp-text-image-stamp-text.cs
using IronPdf;
using IronPdf.Editing;
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Example HTML Document!</h1>");
// Create text stamper
TextStamper textStamper = new TextStamper()
{
Text = "Text Stamper!",
FontFamily = "Bungee Spice",
UseGoogleFont = true,
FontSize = 30,
IsBold = true,
IsItalic = true,
VerticalAlignment = VerticalAlignment.Top,
};
// Stamp the text stamper
pdf.ApplyStamp(textStamper);
pdf.SaveAs("stampText.pdf");
Imports IronPdf
Imports IronPdf.Editing
Private renderer As New ChromePdfRenderer()
Private pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Example HTML Document!</h1>")
' Create text stamper
Private textStamper As New TextStamper() With {
.Text = "Text Stamper!",
.FontFamily = "Bungee Spice",
.UseGoogleFont = True,
.FontSize = 30,
.IsBold = True,
.IsItalic = True,
.VerticalAlignment = VerticalAlignment.Top
}
' Stamp the text stamper
pdf.ApplyStamp(textStamper)
pdf.SaveAs("stampText.pdf")
このコードスニペットの詳細な説明と追加機能については、当社の包括的なハウツーガイドを参照してください。
カスタム透かし
また、ApplyWatermark メソッドを使用して、新しくレンダリングされた PDF と既存のドキュメントの両方に透かしをシームレスに統合することもできます。 この機能によりブランド認識が向上し、資料がプロフェッショナルな印象を与えます。
:path=/static-assets/pdf/content-code-examples/how-to/custom-watermark-apply-watermark.cs
using IronPdf;
string watermarkHtml = @"
<img src='https://ironsoftware.com/img/products/ironpdf-logo-text-dotnet.svg'>
<h1>Iron Software</h1>";
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Watermark</h1>");
// Apply watermark
pdf.ApplyWatermark(watermarkHtml);
pdf.SaveAs("watermark.pdf");
Imports IronPdf
Private watermarkHtml As String = "
<img src='https://ironsoftware.com/img/products/ironpdf-logo-text-dotnet.svg'>
<h1>Iron Software</h1>"
Private renderer As New ChromePdfRenderer()
Private pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Watermark</h1>")
' Apply watermark
pdf.ApplyWatermark(watermarkHtml)
pdf.SaveAs("watermark.pdf")
このコードスニペットの詳細な説明と追加機能については、当社の包括的なハウツーガイドを参照してください。
背景と前景
透かしやスタンプに加えて、AddBackgroundPdf を完全に使用して背景を追加し、PDF をカスタマイズすることもできます。
:path=/static-assets/pdf/content-code-examples/how-to/background-foreground-background.cs
using IronPdf;
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Main HTML content</h1>");
// Render background
PdfDocument background = renderer.RenderHtmlAsPdf("<body style='background-color: cyan;'></body>");
// Add background
pdf.AddBackgroundPdf(background);
pdf.SaveAs("addBackground.pdf");
Imports IronPdf
Private renderer As New ChromePdfRenderer()
Private pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Main HTML content</h1>")
' Render background
Private background As PdfDocument = renderer.RenderHtmlAsPdf("<body style='background-color: cyan;'></body>")
' Add background
pdf.AddBackgroundPdf(background)
pdf.SaveAs("addBackground.pdf")
このコードスニペットの詳細な説明と追加機能については、当社の包括的なハウツーガイドを参照してください。
テキストとビットマップの描画
PDFにテキストを描画することは直感的で簡単です; DrawText を使用し、必要なパラメータを指定します。 この例では、Times New Roman フォントを使用して、新しいフレーズ Some text を追加します。
:path=/static-assets/pdf/content-code-examples/how-to/draw-text-and-bitmap-draw-text.cs
using IronPdf;
using IronSoftware.Drawing;
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>testing</h1>");
// Draw text on PDF
pdf.DrawText("Some text", FontTypes.TimesNewRoman.Name, FontSize: 12, PageIndex: 0, X: 100, Y: 100, Color.Black, Rotation: 0);
pdf.SaveAs("drawText.pdf");
Imports IronPdf
Imports IronSoftware.Drawing
Dim renderer As New ChromePdfRenderer()
Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>testing</h1>")
' Draw text on PDF
pdf.DrawText("Some text", FontTypes.TimesNewRoman.Name, FontSize:=12, PageIndex:=0, X:=100, Y:=100, Color.Black, Rotation:=0)
pdf.SaveAs("drawText.pdf")
このコードスニペットの詳細な説明と追加機能については、当社の包括的なハウツーガイドを参照してください。
線と長方形の描画
IronPDFはPDF上に線を描くこともサポートしています。 まず、2 つの PointF クラスを初期化して開始点と終了点を作成し、次に以下に示すように、それらを DrawLine メソッドで適用します。
:path=/static-assets/pdf/content-code-examples/how-to/draw-line-and-rectangle-draw-line.cs
using IronPdf;
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>testing</h1>");
// Configure the required parameters
int pageIndex = 0;
var start = new IronSoftware.Drawing.PointF(200,150);
var end = new IronSoftware.Drawing.PointF(1000,150);
int width = 10;
var color = new IronSoftware.Drawing.Color("#000000");
// Draw line on PDF
pdf.DrawLine(pageIndex, start, end, width, color);
pdf.SaveAs("drawLine.pdf");
Imports IronPdf
Private renderer As New ChromePdfRenderer()
Private pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>testing</h1>")
' Configure the required parameters
Private pageIndex As Integer = 0
Private start = New IronSoftware.Drawing.PointF(200,150)
Private [end] = New IronSoftware.Drawing.PointF(1000,150)
Private width As Integer = 10
Private color = New IronSoftware.Drawing.Color("#000000")
' Draw line on PDF
pdf.DrawLine(pageIndex, start, [end], width, color)
pdf.SaveAs("drawLine.pdf")
このコードスニペットの詳細な説明と追加機能については、当社の包括的なハウツーガイドを参照してください。
テキストとページを回転
PDF ページを回転するには、SetPageRotation メソッドを使用して特定のページを回転します。 以下の例では、ページ1を変更せずに、ページ2から4のみを回転させ、その機能を示しています。
:path=/static-assets/pdf/content-code-examples/how-to/rotating-text-set-page-rotation.cs
using IronPdf;
using IronPdf.Rendering;
using System.Linq;
// Import PDF
PdfDocument pdf = PdfDocument.FromFile("multi-page.pdf");
// Set rotation for a single page
pdf.SetPageRotation(0, PdfPageRotation.Clockwise90);
// Set rotation for multiple pages
pdf.SetPageRotations(Enumerable.Range(1,3), PdfPageRotation.Clockwise270);
// Set rotation for the entire document
pdf.SetAllPageRotations(PdfPageRotation.Clockwise180);
pdf.SaveAs("rotated.pdf");
Imports IronPdf
Imports IronPdf.Rendering
Imports System.Linq
' Import PDF
Private pdf As PdfDocument = PdfDocument.FromFile("multi-page.pdf")
' Set rotation for a single page
pdf.SetPageRotation(0, PdfPageRotation.Clockwise90)
' Set rotation for multiple pages
pdf.SetPageRotations(Enumerable.Range(1,3), PdfPageRotation.Clockwise270)
' Set rotation for the entire document
pdf.SetAllPageRotations(PdfPageRotation.Clockwise180)
pdf.SaveAs("rotated.pdf")
回転角度に関する利用可能なenumのリストについてはこちらを参照してください。
このコードスニペットの詳細な説明と追加機能については、当社の包括的なハウツーガイドを参照してください。
PDFページの変形
回転以外にも、さまざまなパラメータを提供することでPDFページを変形させることができます。 以下の例では、PDF の最初のページを選択し、Transform を使用して、最初のページの内容を右と下に 50 ポイント移動し、元のサイズの 80% に拡大します。
:path=/static-assets/pdf/content-code-examples/how-to/transform-pdf-pages-transform-pdf.cs
using IronPdf;
PdfDocument pdf = PdfDocument.FromFile("basic.pdf");
pdf.Pages[0].Transform(50, 50, 0.8, 0.8);
pdf.SaveAs("transformPage.pdf");
Imports IronPdf
Dim pdf As PdfDocument = PdfDocument.FromFile("basic.pdf")
pdf.Pages(0).Transform(50, 50, 0.8, 0.8)
pdf.SaveAs("transformPage.pdf")
このコードスニペットの詳細な説明と追加機能については、当社の包括的なハウツーガイドを参照してください。
結論
上記の例リストは、IronPDFがPDFを編集する際に即座に動作する主要な機能を持っていることを示しています。
IronPDFやライセンスに関して機能のリクエストをしたり、一般的な質問がある場合は、どうぞサポートチームにご連絡ください。 喜んでお手伝いいたします。
よくある質問
C#でPDF文書を編集するにはどうすればいいですか?
IronPDFは、C#でPDF文書を編集するための包括的なツールを提供します。ページを操作したり、コンテンツを追加または削除したり、AddPageやRemovePageのようなメソッドを使用して効率的にさまざまな変更を加えることができます。
C#を使用してPDFに透かしを追加する手順は何ですか?
C#でPDFに透かしを追加するには、IronPDFのApplyWatermarkメソッドをTextStamperクラスと共に使用し、透かしのテキスト、不透明度、回転をカスタマイズできます。
C#を使用してPDFファイルを圧縮するにはどうすればいいですか?
IronPDFは、埋め込まれた画像を圧縮し、オプションでPDF構造ツリーを削除する単一の呼び出しで実行できるCompressAndSaveAsメソッドを提供しています。PdfDocument.FromFile("input.pdf").CompressAndSaveAs("compressed.pdf", 40) を使用すると、指定したJPEG品質(0~100)で圧縮して保存できます。 ファイルサイズをさらに縮小するには、3番目のパラメータとして removeStructureTree: true を指定します。メモリ内ワークフローの場合は、CompressPdfToBytes または CompressPdfToStream を使用して、ディスクに書き込むことなく、圧縮された出力をバイト配列またはストリームとして取得できます。
C#でPDFにヘッダーとフッターを追加することは可能ですか?
はい、IronPDFでは、HtmlHeaderFooterまたはTextHeaderFooterクラスを使用してPDFにヘッダーとフッターを追加でき、HTMLコンテンツやテキストを定義し、高さなどのプロパティを調整することができます。
複数のPDFを1つの文書に統合するためにはどのメソッドを使用できますか?
複数のPDFを結合するには、それぞれの文書をPdfDocument.FromFileで読み込み、Mergeメソッドを使用して新しいドキュメントとして保存できる単一ファイルに結合します。
C#でPDF内の特定のテキストを置換するにはどうすればいいですか?
IronPDFは、ReplaceTextOnPageメソッドを使用してPDF内の文字列を検索し、プログラム上で置換することができるようにします。
IronPDFはPDFフォームの作成と記入をサポートしていますか?
はい、IronPDFはPDFフォームの作成および既存のフォームフィールドのプログラムでの記入をサポートしており、PDF文書との動的なやりとりを可能にします。
IronPDFを使用してPDFに注釈を追加するにはどうすればいいですか?
IronPDFはPDFに注釈を追加する機能を提供しています。注釈オブジェクトを使用して、テキスト注釈やその他のタイプを作成し、目的のページに配置できます。
C#を使用してPDFにデジタル署名を扱うにはどうすればいいですか?
IronPDFは、X509Certificate2デジタル証明書を使用してPDF文書にデジタル署名を追加することをサポートし、文書の真正性と整合性を確保します。
C#でPDFページの背景を設定することは可能ですか?
はい、IronPDFを使用して、画像や色の背景を適用することで、PDFページに背景を設定し、文書のデザインとプレゼンテーションを強化できます。
IronPDF は .NET 10 と互換性がありますか? また、どのような利点がありますか?
はい。IronPDF は .NET 10 と完全に互換性があり、そのパフォーマンスと言語強化のメリットを活用できます。デスクトップ、Web、MAUI、サービスなど、あらゆる .NET 10 プロジェクトで「そのまま」動作し、配列インターフェースメソッドの非仮想化やスタック割り当ての強化といった改善の恩恵を受けています。

