How to Edit PDFs in C#

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

Iron Softwareは、IronPDFライブラリ内のPDF編集機能をわかりやすいメソッドに単純化しました。 署名を追加したり、HTMLのフッターを追加したり、透かしを押印したり、注釈を追加したりする際に役立ちます。 IronPDFは、読みやすいコード、プログラムによるPDF生成、簡単なデバッグ、そしてサポートされている任意の環境またはプラットフォームへのシームレスなデプロイを可能にするツールです。

IronPDFは、PDF編集に関する豊富な機能を誇ります。 このチュートリアル記事では、主要な機能のいくつかを紹介し、コード例と説明を提供します。

この記事を通じて、C#でPDFを編集するためのIronPDFの使用方法をよく理解できるようになります。

クイックスタート:PDFファイルを数秒で編集

IronPDFを使用して、C#でPDFドキュメントを手軽に編集できます。 このクイックガイドでは、既存のPDFファイルにテキストスタンプを追加する方法を示します。わずか数行のコードでPDFを変更し、変更を即座に保存することができます。 迅速かつ効率的なPDF編集ソリューションを必要とする開発者に最適です。

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronPDF with NuGet Package Manager

    PM > Install-Package IronPdf

  2. Copy and run this code snippet.

    var pdf = IronPdf.PdfDocument.FromFile("example.pdf");
    pdf.ApplyStamp(new IronPdf.Editing.TextStamper("Confidential"));
    pdf.SaveAs("edited_example.pdf");
  3. Deploy to test on your live environment

    Start using IronPDF in your project today with a free trial
    arrow pointer

目次

今日あなたのプロジェクトでIronPDFを無料トライアルで使用開始。

最初のステップ:
green arrow pointer
NuGet 購入の準備ができていませんか?

PM >  Install-Package IronPdf

IronPDFNuGet でチェックしてください。1000万回以上のダウンロードで、C#によるPDF開発を変革しています。 DLL または Windowsインストーラー をダウンロードすることもできます。

ドキュメント構造の編集

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
$vbLabelText   $csharpLabel

このコードスニペットの詳細な説明と追加機能については、当社の包括的なハウツーガイドを参照してください。

ドキュメントの保存とエクスポート

ドキュメントの保存とエクスポートについては、IronPDFを使用すると、編集済みドキュメントを簡単にディスクに保存することもでき、また、バイナリデータやメモリストリームなど他の形式にもエクスポートできます。

このコードスニペットの詳細な説明と追加機能については、当社の包括的なハウツーガイドを参照してください。

メモリからPDFのロード

IronPDFは、既存のC# .NETアプリケーションと完全に統合されています; MemoryStreamオブジェクトを通じてメモリストリームから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)
$vbLabelText   $csharpLabel

このコードスニペットの詳細な説明と追加機能については、当社の包括的なハウツーガイドを参照してください。

メモリへのPDFのエクスポート

同様に、C# .NETを通じてメモリストリームに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
$vbLabelText   $csharpLabel

このコードスニペットの詳細な説明と追加機能については、当社の包括的なハウツーガイドを参照してください。

ドキュメントテキストの編集

C#でのPDF解析

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)
$vbLabelText   $csharpLabel

このコードスニペットの詳細な説明と追加機能については、当社の包括的なハウツーガイドを参照してください。

テキストと画像の抽出

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
$vbLabelText   $csharpLabel

このコードスニペットの詳細な説明と追加機能については、当社の包括的なハウツーガイドを参照してください。

テキストと領域の修正

機密情報を修正して保護する必要がある場合には、強力なツール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")
$vbLabelText   $csharpLabel

このコードスニペットの詳細な説明と追加機能については、当社の包括的なハウツーガイドを参照してください。

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")
$vbLabelText   $csharpLabel

このコードスニペットの詳細な説明と追加機能については、当社の包括的なハウツーガイドを参照してください。

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");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

このコードスニペットの詳細な説明と追加機能については、当社の包括的なハウツーガイドを参照してください。

テキストと画像のスタンプ

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")
$vbLabelText   $csharpLabel

このコードスニペットの詳細な説明と追加機能については、当社の包括的なハウツーガイドを参照してください。

カスタム透かし

新たにレンダリングされたPDFと既存のドキュメントの両方に透かしをシームレスに統合するために、ApplyWatermarkメソッドを利用できます。 この機能によりブランド認識が向上し、資料がプロフェッショナルな印象を与えます。

: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")
$vbLabelText   $csharpLabel

このコードスニペットの詳細な説明と追加機能については、当社の包括的なハウツーガイドを参照してください。

背景と前景

透かしやスタンプに加えて、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")
$vbLabelText   $csharpLabel

このコードスニペットの詳細な説明と追加機能については、当社の包括的なハウツーガイドを参照してください。

テキストとビットマップの描画

PDFにテキストを描画することは直感的で簡単です; DrawTextを使用して必要なパラメータを提供します。 この例では、「Some text」という新しいフレーズをフォントTimes New Romanで追加しています。

: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");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

このコードスニペットの詳細な説明と追加機能については、当社の包括的なハウツーガイドを参照してください。

線と長方形の描画

IronPDFはPDF上に線を描くこともサポートしています。 まず、PointFクラスを2つ初期化することで開始点と終了点を作成し、次に以下に示す方法で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")
$vbLabelText   $csharpLabel

このコードスニペットの詳細な説明と追加機能については、当社の包括的なハウツーガイドを参照してください。

テキストとページを回転

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")
$vbLabelText   $csharpLabel

回転角度に関する利用可能な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");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

このコードスニペットの詳細な説明と追加機能については、当社の包括的なハウツーガイドを参照してください。

結論

上記の例リストは、IronPDFがPDFを編集する際に即座に動作する主要な機能を持っていることを示しています。

IronPDFやライセンスに関して機能のリクエストをしたり、一般的な質問がある場合は、どうぞサポートチームにご連絡ください。 喜んでお手伝いいたします。

よくある質問

C#でPDF文書を編集するにはどうすればいいですか?

IronPDFは、C#でPDF文書を編集するための包括的なツールを提供します。ページを操作したり、コンテンツを追加または削除したり、AddPageRemovePageのようなメソッドを使用して効率的にさまざまな変更を加えることができます。

C#を使用してPDFに透かしを追加する手順は何ですか?

C#でPDFに透かしを追加するには、IronPDFのApplyWatermarkメソッドをTextStamperクラスと共に使用し、透かしのテキスト、不透明度、回転をカスタマイズできます。

C#を使用してPDFファイルを圧縮するにはどうすればいいですか?

IronPDFは、PDF内に埋め込まれた画像のサイズを縮小し、ファイルサイズを効果的に圧縮するCompressImagesメソッドを提供します。

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 プロジェクトで「そのまま」動作し、配列インターフェースメソッドの非仮想化やスタック割り当ての強化といった改善の恩恵を受けています。

Curtis Chau
テクニカルライター

Curtis Chauは、カールトン大学でコンピュータサイエンスの学士号を取得し、Node.js、TypeScript、JavaScript、およびReactに精通したフロントエンド開発を専門としています。直感的で美しいユーザーインターフェースを作成することに情熱を持ち、Curtisは現代のフレームワークを用いた開発や、構造の良い視覚的に魅力的なマニュアルの作成を楽しんでいます。

開発以外にも、CurtisはIoT(Internet of Things)への強い関心を持ち、ハードウェアとソフトウェアの統合方法を模索しています。余暇には、ゲームをしたりDiscordボットを作成したりして、技術に対する愛情と創造性を組み合わせています。

準備はいいですか?
Nuget ダウンロード 16,133,208 | バージョン: 2025.11 ただ今リリースされました