1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

C#でPDFをJPG/ PNG画像に変換する

Posted at

PDFファイルと比較して、画像ファイルは、特にソーシャルメディアプラットフォーム上で共有しやすく、より幅広いデバイスと互換性があります。これらの要因により、PDFから画像への変換は、今日のデジタル時代においてニーズが高まっています。
C#でPDFをJPGやPNGのような画像形式に変換する信頼性の高い効率的な方法をお探しなら、Spire.PDF for .NETライブラリが正しい選択です。この記事では、この.NET PDFライブラリを使用してタスクを達成する方法を詳しく説明します。

PDFを画像に変換する.NETライブラリ

Spire.PDFライブラリは、C#、VB.NET、ASP.NET、.NET CoreでPDFファイルの作成、読み込み、書き込み、変換、印刷を含む幅広いPDF処理機能をサポートしています。
以下のリンクからダウンロードするか、Nuget経由でインストールしてください。

PDFから画像への変換手順

  1. 必要な名前空間をインポートする。
  2. PDFファイルを読み込みます。
  3. PDFページを画像に変換し、PdfDocument.SaveAsImage (int pageIndex, PdfImageType type, int dpiX, int dpiY) メソッドを使用して画像のDpiを設定します。
  4. 画像を任意の画像形式(PNG、JPG、BMP、EMFなど)に保存します。

PDFをJPG、PNG画像に変換するC#コード

using Spire.Pdf;
using Spire.Pdf.Graphics;
using System;
using System.Drawing;
using System.Drawing.Imaging;

namespace PDFtoImage
{
    class Program
    {
        static void Main(string[] args)
        {
            //サンプルPDF文書を読み込む
            PdfDocument pdf = new PdfDocument();
            pdf.LoadFromFile("新聞.pdf");

            //PDFの各ページをループする
            for (int i = 0; i < pdf.Pages.Count; i++)
            {
                //すべてのページを画像に変換し、画像のDpiを設定する。
                Image image = pdf.SaveAsImage(i, PdfImageType.Bitmap, 500, 500);

                //画像をPNG形式で指定フォルダに保存 
                string file = string.Format("Image\\ToImage-{0}.png", i);
                image.Save(file, ImageFormat.Png);

            }
        }
    }
}

上記のコード例は、PDFファイル内の全ページを繰り返し処理し、各ページを指定されたファイルパスに個別の画像に変換します。

4ページのPDFファイルを変換した出力結果:
PDFtoImage.png


:round_pushpin: Spire.PDF for .NETは、PDFからJPGやPNGへの変換に加えて、PDFからWordPDFからHTMLHtmlからPDFPDFからExcelPDFからPowerPointPDFからSVGなどへの変換もサポートしています。

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?