画像をPDFに変換する方法

Chaknith related to 画像をPDFに変換する方法
チャクニット・ビン
2023年9月21日
更新済み 2024年12月10日
共有:
This article was translated from English: Does it need improvement?
Translated
View the article in English

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

IronPDFを使用すると、ユニークな画像の配置と動作を持つPDFに単一または複数の画像を変換できます。 これらの動作には、ページに合わせる、ページの中央に配置する、ページをトリミングすることが含まれます。 さらに、IronPDFを使用してテキストおよびHTMLヘッダーやフッターを追加したり、IronPDFで透かしを適用したり、カスタムページサイズを設定したり、背景および前景のオーバーレイを含めたりできます。

IronPDFを始める

今日から無料トライアルでIronPDFをあなたのプロジェクトで使い始めましょう。

最初のステップ:
green arrow pointer



画像をPDFに変換する例

ImageToPdf スタティックメソッドを ImageToPdfConverter クラス内で使用して、画像を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");

PDFを出力


画像をPDFに変換する例

複数の画像をPDFドキュメントに変換するには、前の例で示したように、単一のファイルパスではなく、ファイルパスを含む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");

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");

画像動作の比較

Place the image at the top-left of the page
Place the image at the top-right of the page
Place the image at the center of the page
Fit the image to the page while maintaining the aspect ratio
Place the image at the bottom-left of the page
Place the image at the bottom-right of the page
画像をページに合わせて引き伸ばす
画像に合わせてページをトリミングする

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

様々な種類の画像をPDFドキュメントに変換するImageToPdf静的メソッドの裏側の鍵は、画像を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");

PDFを出力

PDFドキュメントを画像に変換またはラスタライズしたい場合は、PDFを画像にラスタライズする方法に関するガイドをご参照ください。

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