画像をPDFに変換する方法

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

によって チャクニット・ビン

画像をPDFに変換することは、複数の画像ファイルを結合する便利なプロセスです。 (JPG、PNG、TIFFなど) 単一のPDFドキュメントに。 これはデジタルポートフォリオ、プレゼンテーション、またはレポートを作成するためによく行われます。これにより、画像のコレクションをより整理された普遍的に読みやすい形式で共有および保存しやすくなります。

IronPDFを使用すると、単一または複数の画像を固有のPDFに変換することができます。 画像の配置と動作. これらの動作には、ページに合わせる、ページの中央に配置する、ページをトリミングすることが含まれます。 Additionally, you can add (追加で、以下を追加することができます) テキストとHTMLヘッダーおよびフッター, 透かしを適用する、カスタムページサイズを設定し、背景および前景のオーバーレイを含めることができます。


PDF 用 C# NuGet ライブラリ

でインストール NuGet

Install-Package IronPdf
または
Java PDF JAR(ジャバPDF JAR)

ダウンロード DLL (ディーエルエル)

DLLをダウンロード

プロジェクトに手動でインストールする

PDF 用 C# NuGet ライブラリ

でインストール NuGet

Install-Package IronPdf
または
Java PDF JAR(ジャバPDF JAR)

ダウンロード DLL (ディーエルエル)

DLLをダウンロード

プロジェクトに手動でインストールする

今日からプロジェクトでIronPDFを使い始めましょう。無料のトライアルをお試しください。

最初のステップ:
green arrow pointer

チェックアウト IronPDF オン Nuget 迅速なインストールと展開のために。8百万以上のダウンロード数により、PDFをC#で変革しています。

PDF 用 C# NuGet ライブラリ nuget.org/packages/IronPdf/
Install-Package IronPdf

インストールを検討してください IronPDF DLL 直接。ダウンロードして、プロジェクトまたはGACの形式で手動でインストールしてください。 IronPdf.zip

プロジェクトに手動でインストールする

DLLをダウンロード

画像をPDFに変換する例

ImageToPdfConverterクラス内のImageToPdf静的メソッドを使用して、画像をPDFドキュメントに変換します。 このメソッドはイメージのファイルパスだけを必要とし、デフォルトの画像配置と動作でPDFドキュメントに変換します。 対応している画像形式には、.bmp、.jpeg、.jpg、.gif、.png、.svg、.tif、.tiff、.webp、.apng、.avif、.cur、.dib、.ico、.jfif、.jif、.jpe、.pjp、.pjpegが含まれます。

サンプル画像

画像サンプル

コード

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

string imagePath = "meetOurTeam.jpg";

// Convert an image to a PDF
PdfDocument pdf = ImageToPdfConverter.ImageToPdf(imagePath);

// Export the PDF
pdf.SaveAs("imageToPdf.pdf");
Imports IronPdf

Private imagePath As String = "meetOurTeam.jpg"

' Convert an image to a PDF
Private pdf As PdfDocument = ImageToPdfConverter.ImageToPdf(imagePath)

' Export the PDF
pdf.SaveAs("imageToPdf.pdf")
VB   C#

PDFを出力


画像をPDFに変換する例

複数の画像をPDFドキュメントに変換するには、以前の例で示したように、1つのファイルパスの代わりにファイルパスを含むIEnumerableオブジェクトを提供する必要があります。 これは再びデフォルトの画像配置と動作でPDFドキュメントを生成します。

:path=/static-assets/pdf/content-code-examples/how-to/image-to-pdf-convert-multiple-images.cs
using IronPdf;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

// Retrieve all JPG and JPEG image paths in the 'images' folder.
IEnumerable<String> imagePaths = Directory.EnumerateFiles("images").Where(f => f.EndsWith(".jpg") || f.EndsWith(".jpeg"));

// Convert images to a PDF
PdfDocument pdf = ImageToPdfConverter.ImageToPdf(imagePaths);

// Export the PDF
pdf.SaveAs("imagesToPdf.pdf");
Imports IronPdf
Imports System
Imports System.Collections.Generic
Imports System.IO
Imports System.Linq

' Retrieve all JPG and JPEG image paths in the 'images' folder.
Private imagePaths As IEnumerable(Of String) = Directory.EnumerateFiles("images").Where(Function(f) f.EndsWith(".jpg") OrElse f.EndsWith(".jpeg"))

' Convert images to a PDF
Private pdf As PdfDocument = ImageToPdfConverter.ImageToPdf(imagePaths)

' Export the PDF
pdf.SaveAs("imagesToPdf.pdf")
VB   C#

PDFを出力


画像の配置と動作

使いやすさのために、役に立つ画像配置や動作オプションを幅広く提供しています。 例えば、画像をページの中央に配置したり、アスペクト比を維持しながらページサイズに合わせたりすることができます。 使用可能なすべての画像配置と動作は以下の通りです。

  • TopLeftCornerOfPage: イメージがページの左上隅に配置されます。
  • TopRightCornerOfPage: 画像はページの右上隅に配置されます。
  • CenteredOnPage: 画像がページの中央に配置される。
  • FitToPageAndMaintainAspectRatio: イメージは元のアスペクト比を保ちながらページに適合します。
  • BottomLeftCornerOfPage: 画像がページの左下隅に配置されます。
  • BottomRightCornerOfPage: 画像はページの右下に配置されます。
  • FitToPage: 画像がページに合わせて調整されます。
  • CropPage: ページが画像に合わせて調整されます。
:path=/static-assets/pdf/content-code-examples/how-to/image-to-pdf-convert-one-image-image-behavior.cs
using IronPdf;
using IronPdf.Imaging;

string imagePath = "meetOurTeam.jpg";

// Convert an image to a PDF with image behavior of centered on page
PdfDocument pdf = ImageToPdfConverter.ImageToPdf(imagePath, ImageBehavior.CenteredOnPage);

// Export the PDF
pdf.SaveAs("imageToPdf.pdf");
Imports IronPdf
Imports IronPdf.Imaging

Private imagePath As String = "meetOurTeam.jpg"

' Convert an image to a PDF with image behavior of centered on page
Private pdf As PdfDocument = ImageToPdfConverter.ImageToPdf(imagePath, ImageBehavior.CenteredOnPage)

' Export the PDF
pdf.SaveAs("imageToPdf.pdf")
VB   C#

画像動作の比較

画像をページの左上に配置する
ページの右上に画像を配置します
ページの中央に画像を配置してください
アスペクト比を維持しながら画像をページに合わせる
画像をページの左下に配置してください。
画像をページの右下に配置する
画像をページに合わせて引き伸ばす
画像に合わせてページをトリミングする

レンダリングオプションを適用

ImageToPdf静的メソッドの内部で、様々な種類の画像をPDFドキュメントに変換するキーは、画像をHTMLのimgタグとしてインポートし、次にそのHTMLをPDFに変換することです。 これは、ImageToPdf メソッドの第3パラメーターとして ChromePdfRenderOptions オブジェクトを渡して直接レンダリングプロセスをカスタマイズできる理由でもあります。

:path=/static-assets/pdf/content-code-examples/how-to/image-to-pdf-convert-one-image-rendering-options.cs
using IronPdf;

string imagePath = "meetOurTeam.jpg";

ChromePdfRenderOptions options = new ChromePdfRenderOptions()
{
    HtmlHeader = new HtmlHeaderFooter()
    {
        HtmlFragment = "<h1 style='color: #2a95d5;'>Content Header</h1>",
        DrawDividerLine = true,
    },
};

// Convert an image to a PDF with custom header
PdfDocument pdf = ImageToPdfConverter.ImageToPdf(imagePath, options: options);

// Export the PDF
pdf.SaveAs("imageToPdfWithHeader.pdf");
Imports IronPdf

Private imagePath As String = "meetOurTeam.jpg"

Private options As New ChromePdfRenderOptions() With {
	.HtmlHeader = New HtmlHeaderFooter() With {
		.HtmlFragment = "<h1 style='color: #2a95d5;'>Content Header</h1>",
		.DrawDividerLine = True
	}
}

' Convert an image to a PDF with custom header
Private pdf As PdfDocument = ImageToPdfConverter.ImageToPdf(imagePath, options:= options)

' Export the PDF
pdf.SaveAs("imageToPdfWithHeader.pdf")
VB   C#

PDFを出力

PDFドキュメントを画像に変換またはラスタライズしたい場合は、以下を参照してください PDF を画像にラスタライズする方法 記事

チャクニット・ビン

ソフトウェアエンジニア

チャクニットは開発者のシャーロック・ホームズです。彼がソフトウェアエンジニアリングの将来性に気付いたのは、楽しみでコーディングチャレンジをしていたときでした。彼のフォーカスはIronXLとIronBarcodeにありますが、すべての製品でお客様を助けることに誇りを持っています。チャクニットは顧客と直接話すことで得た知識を活用して、製品自体のさらなる改善に貢献しています。彼の逸話的なフィードバックは、単なるJiraチケットを超えて、製品開発、ドキュメントおよびマーケティングをサポートし、顧客の全体的な体験を向上させます。オフィスにいないときは、機械学習やコーディングについて学んだり、ハイキングを楽しんだりしています。