.NETでさまざまな画像形式をPDFに変換する

概要

この記事では、C#を使用してさまざまな画像形式をPDFに変換する方法を説明します。以下のトピックをカバーしています。

次のコードスニペットは、Aspose.PDF.Drawingライブラリでも動作します。

C#画像からPDFへの変換

Aspose.PDF for .NETは、さまざまな形式の画像をPDFファイルに変換することを可能にします。当社のライブラリは、BMP、CGM、DICOM、EMF、JPG、PNG、SVG、CDR、HEIC、TIFFなどの最も一般的な画像形式を変換するためのコードスニペットを示しています。

BMPをPDFに変換

Aspose.PDF for .NETライブラリを使用してBMPファイルをPDFドキュメントに変換します。

BMP画像は拡張子を持つファイルです。BMPは、ビットマップデジタル画像を保存するために使用されるビットマップ画像ファイルを表します。これらの画像はグラフィックアダプタに依存せず、デバイス独立ビットマップ(DIB)ファイル形式とも呼ばれます。 Aspose.PDF for .NET APIを使用してBMPをPDFファイルに変換できます。したがって、BMP画像を変換するための手順は次のとおりです。

BMPをPDFに変換

  1. 新しいDocumentクラスオブジェクトを初期化します。
  2. 入力BMP画像を読み込みます。
  3. 最後に、出力PDFファイルを保存します。

次のコードスニペットは、これらの手順に従い、C#を使用してBMPをPDFに変換する方法を示しています。

// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ConvertBMPtoPDF()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments();

    // Create PDF document
    using (var document = new Aspose.Pdf.Document())
    {
        // Add page
        var page = document.Pages.Add();
        var image = new Aspose.Pdf.Image();
        
        // Load BMP file
        image.File = dataDir + "BMPtoPDF.bmp";
        page.Paragraphs.Add(image);
        
        // Save PDF document
        document.Save(dataDir + "BMPtoPDF_out.pdf");
    }
}

CGMをPDFに変換

CGMは、CAD(コンピュータ支援設計)およびプレゼンテーショングラフィックスアプリケーションで一般的に使用されるコンピュータグラフィックスメタファイル形式のファイル拡張子です。CGMは、プログラムの読み取り速度に最適なバイナリ(最小のファイルサイズを生成し、データ転送を高速化する)またはクリアテキストエンコーディング(ユーザーがテキストエディタでファイルを読み取り、変更できる)など、3つの異なるエンコーディング方法をサポートするベクターグラフィックス形式です。

CGMファイルをPDF形式に変換するための次のコードスニペットを確認してください。

CGMをPDFに変換

  1. CgmLoadOptionsクラスのインスタンスを作成します。
  2. ソースファイル名とオプションを指定してDocumentクラスのインスタンスを作成します。
  3. 希望のファイル名でドキュメントを保存します。
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ConvertCGMtoPDF()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments();

    var option = new Aspose.Pdf.CgmLoadOptions();

    // Open PDF document
    using (var document = new Aspose.Pdf.Document(dataDir + "CGMtoPDF.cgm", option))
    {
        // Save PDF document
        document.Save(dataDir + "CGMtoPDF_out.pdf");
    }
}

DICOMをPDFに変換

DICOM形式は、デジタル医療画像と検査された患者の文書の作成、保存、送信、視覚化のための医療業界の標準です。

Aspose.PDF for .NETは、DICOMおよびSVG画像を変換できますが、技術的な理由からPDFに追加する画像のタイプを指定する必要があります。

DICOMをPDFに変換

  1. Imageクラスのオブジェクトを作成します。
  2. 画像をページの段落コレクションに追加します。
  3. FileTypeプロパティを指定します。
  4. ファイルのパスまたはソースを指定します。
    • 画像がハードドライブの場所にある場合は、Image.Fileプロパティを使用してパスを指定します。
    • 画像がMemoryStreamに配置されている場合は、画像を保持するオブジェクトをImage.ImageStreamプロパティに渡します。

次のコードスニペットは、Aspose.PDFを使用してDICOMファイルをPDF形式に変換する方法を示しています。DICOM画像を読み込み、PDFファイルのページに画像を配置し、出力をPDFとして保存する必要があります。

// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ConvertDICOMtoPDF()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments();

    // Create PDF document 
    using (var document = new Aspose.Pdf.Document())
    {
        // Add page
        var page = document.Pages.Add();
        
        var image = new Aspose.Pdf.Image
        {
            FileType = ImageFileType.Dicom,
            File = dataDir + "DICOMtoPDF.dcm"
        };
        page.Paragraphs.Add(image);

        // Save PDF document
        document.Save(dataDir + "DICOMtoPDF_out.pdf");
    }
}

EMFをPDFに変換

EMFは、グラフィカル画像をデバイスに依存せずに保存します。EMFのメタファイルは、保存された画像を任意の出力デバイスで解析後にレンダリングできる可変長レコードで構成されています。さらに、以下の手順を使用してEMFをPDF画像に変換できます。

EMFをPDFに変換

  1. まず、Documentクラスオブジェクトを初期化します。
  2. EMF画像ファイルを読み込みます。
  3. 読み込んだEMF画像をページに追加します。
  4. PDFドキュメントを保存します。

さらに、次のコードスニペットは、C#を使用してEMFをPDFに変換する方法を示しています。

// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ConvertEMFtoPDF()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments();

    // Create PDF document 
    using (var document = new Aspose.Pdf.Document())
    {
        // Add page
        var page = document.Pages.Add();
        var image = new Aspose.Pdf.Image();
        // Load EMF file
        image.File = dataDir + "EMFtoPDF.emf";

        // Specify page dimension properties
        page.PageInfo.Margin.Bottom = 0;
        page.PageInfo.Margin.Top = 0;
        page.PageInfo.Margin.Left = 0;
        page.PageInfo.Margin.Right = 0;
        page.PageInfo.Width = image.BitmapSize.Width;
        page.PageInfo.Height = image.BitmapSize.Height;

        page.Paragraphs.Add(image);

        // Save PDF document
        document.Save(dataDir + "EMFtoPDF_out.pdf");
    }
}

GIFをPDFに変換

Aspose.PDF for .NETライブラリを使用してGIFファイルをPDFドキュメントに変換します。

GIFは、256色以下の形式で圧縮データを品質を損なうことなく保存できます。ハードウェアに依存しないGIF形式は、1987年(GIF87a)にCompuServeによってビットマップ画像をネットワーク経由で送信するために開発されました。 Aspose.PDF for .NET APIを使用してGIFをPDFファイルに変換できます。したがって、GIF画像を変換するための手順は次のとおりです。

GIFをPDFに変換

  1. 新しいDocumentクラスオブジェクトを初期化します。
  2. 入力GIF画像を読み込みます。
  3. 最後に、出力PDFファイルを保存します。

次のコードスニペットは、これらの手順に従い、C#を使用してBMPをPDFに変換する方法を示しています。

// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ConvertGIFtoPDF()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments();

    // Create PDF document
    using (var document = new Aspose.Pdf.Document())
    {
        // Add page
        var page = document.Pages.Add();
        var image = new Aspose.Pdf.Image();
        
        // Load sample GIF image file
        image.File = dataDir + "GIFtoPDF.gif";
        page.Paragraphs.Add(image);

        // Save PDF document
        document.Save(dataDir + "GIFtoPDF_out.pdf");
    }
}

JPGをPDFに変換

JPGをPDFに変換する方法を考える必要はありません。なぜなら、Aspose.PDF for .NETライブラリが最適な解決策を提供するからです。

Aspose.PDF for .NETを使用してJPG画像をPDFに非常に簡単に変換できます。手順は次のとおりです。

JPGをPDFに変換

  1. Documentクラスのオブジェクトを初期化します。
  2. PDFドキュメントに新しいページを追加します。
  3. JPG画像を読み込み、段落に追加します。
  4. 出力PDFを保存します。

以下のコードスニペットは、JPG画像をPDFに変換する方法を示しています。

// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ConvertJPGtoPDF()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments();

    // Create PDF document 
    using (var document = new Aspose.Pdf.Document())
    {
        // Add page
        var page = document.Pages.Add();
        var image = new Aspose.Pdf.Image();
        // Load input JPG file
        image.File = dataDir + "JPGtoPDF.jpg";
        
        // Add image on a page
        page.Paragraphs.Add(image);
        
        // Save PDF document
        document.Save(dataDir + "JPGtoPDF_out.pdf");
    }
}

次に、ページの高さと幅が同じ画像をPDFに変換する方法を示します。画像の寸法を取得し、それに応じてPDFドキュメントのページ寸法を設定します。

  1. 入力画像ファイルを読み込みます。
  2. ページの高さ、幅、およびマージンを設定します。
  3. 出力PDFファイルを保存します。

以下のコードスニペットは、C#を使用して同じページの高さと幅を持つ画像をPDFに変換する方法を示しています。

// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ConvertJPGtoPDF()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments();

    // Create PDF document
    using (var document = new Aspose.Pdf.Document())
    {
        // Add page
        var page = document.Pages.Add();
        var image = new Aspose.Pdf.Image();
        // Load JPEG file
        image.File = dataDir + "JPGtoPDF.jpg";
        
        // Read Height of input image
        page.PageInfo.Height = image.BitmapSize.Height;
        // Read Width of input image
        page.PageInfo.Width = image.BitmapSize.Width;
        page.PageInfo.Margin.Bottom = 0;
        page.PageInfo.Margin.Top = 0;
        page.PageInfo.Margin.Right = 0;
        page.PageInfo.Margin.Left = 0;
        page.Paragraphs.Add(image);
        
        // Save PDF document
        document.Save(dataDir + "JPGtoPDF_out.pdf");
    }
}

PNGをPDFに変換

Aspose.PDF for .NETは、PNG画像をPDF形式に変換する機能をサポートしています。次のコードスニペットを確認して、タスクを実現してください。

PNGは、ロスレス圧縮を使用するラスタ画像ファイル形式の一種であり、ユーザーの間で人気があります。

以下の手順を使用してPNGをPDF画像に変換できます。

PNGをPDFに変換

  1. 入力PNG画像を読み込みます。
  2. 高さと幅の値を読み取ります。
  3. 新しいDocumentオブジェクトを作成し、ページを追加します。
  4. ページの寸法を設定します。
  5. 出力ファイルを保存します。

さらに、以下のコードスニペットは、C#を使用して.NETアプリケーションでPNGをPDFに変換する方法を示しています。

// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ConvertPNGtoPDF()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments();

    // Create PDF document
    using (var document = new Aspose.Pdf.Document())
    {
        // Add page
        var page = document.Pages.Add();
        var image = new Aspose.Pdf.Image();
        // Load PNG file
        image.File = dataDir + "PNGtoPDF.png";
        
        // Read Height of input image
        page.PageInfo.Height = image.BitmapSize.Height;
        // Read Width of input image
        page.PageInfo.Width = image.BitmapSize.Width;
        page.PageInfo.Margin.Bottom = 0;
        page.PageInfo.Margin.Top = 0;
        page.PageInfo.Margin.Right = 0;
        page.PageInfo.Margin.Left = 0;
        page.Paragraphs.Add(image);
        
        // Save PDF document
        document.Save(dataDir + "PNGtoPDF_out.pdf");
    }
}

SVGをPDFに変換

Aspose.PDF for .NETは、SVG画像をPDF形式に変換する方法と、ソースSVGファイルの寸法を取得する方法を説明します。

スケーラブルベクターグラフィックス(SVG)は、静的および動的(インタラクティブまたはアニメーション)2次元ベクターグラフィックス用のXMLベースのファイル形式の仕様のファミリーです。SVG仕様は、1999年からWorld Wide Web Consortium(W3C)によって開発されているオープンスタンダードです。

SVG画像とその動作はXMLテキストファイルで定義されています。これは、検索、インデックス作成、スクリプト化、必要に応じて圧縮できることを意味します。XMLファイルとして、SVG画像は任意のテキストエディタで作成および編集できますが、Inkscapeなどの描画プログラムを使用して作成する方が便利です。

SVGファイルをPDFに変換するには、LoadOptionsオブジェクトを初期化するために使用されるSvgLoadOptionsというクラスを使用します。後で、このオブジェクトはDocumentオブジェクトの初期化中に引数として渡され、PDFレンダリングエンジンがソースドキュメントの入力形式を決定するのに役立ちます。

SVGをPDFに変換

  1. SvgLoadOptionsクラスのインスタンスを作成します。
  2. ソースファイル名とオプションを指定してDocumentクラスのインスタンスを作成します。
  3. 希望のファイル名でドキュメントを保存します。

次のコードスニペットは、Aspose.PDF for .NETを使用してSVGファイルをPDF形式に変換するプロセスを示しています。

// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ConvertSVGtoPDF()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments();

    var option = new Aspose.Pdf.SvgLoadOptions();
    // Open SVG file 
    using (var document = new Aspose.Pdf.Document(dataDir + "SVGtoPDF.svg", option))
    {
        // Save PDF document
        document.Save(dataDir + "SVGtoPDF_out.pdf");
    }
}

SVGの寸法を取得

ソースSVGファイルの寸法を取得することも可能です。この情報は、SVGが出力PDFのページ全体をカバーする場合に役立ちます。SvgLoadOptionクラスのAdjustPageSizeプロパティがこの要件を満たします。このプロパティのデフォルト値はfalseです。値がtrueに設定されている場合、出力PDFはソースSVGと同じサイズ(寸法)になります。

次のコードスニペットは、ソースSVGファイルの寸法を取得し、PDFファイルを生成するプロセスを示しています。

// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ConvertSVGtoPDF()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion();

    var loadopt = new Aspose.Pdf.SvgLoadOptions();
    loadopt.AdjustPageSize = true;
    // Open SVG file
    using (var document = new Aspose.Pdf.Document(dataDir + "SVGtoPDF.svg", loadopt))
    {
        document.Pages[1].PageInfo.Margin.Top = 0;
        document.Pages[1].PageInfo.Margin.Left = 0;
        document.Pages[1].PageInfo.Margin.Bottom = 0;
        document.Pages[1].PageInfo.Margin.Right = 0;

        // Save PDF document
        document.Save(dataDir + "SVGtoPDF_out.pdf");
    }
    
}

SVGサポート機能

SVGタグ

サンプル使用

circle

< circle id="r2" cx="10" cy="10" r="10" stroke="blue" stroke-width="2"> 

defs

<defs> 
<rect id="r1" width="15" height="15" stroke="blue" stroke-width="2" /> 
<circle id="r2" cx="10" cy="10" r="10" stroke="blue" stroke-width="2"/> 
<circle id="r3" cx="10" cy="10" r="10" stroke="blue" stroke-width="3"/> 
</defs> 
<use x="25" y="40" xlink:href="#r1" fill="red"/> 
<use x="35" y="15" xlink:href="#r2" fill="green"/> 
<use x="58" y="50" xlink:href="#r3" fill="blue"/>

tref

<defs> 
    <text id="ReferencedText"> 
      参照された文字データ 
    </text> 
</defs> 
<text x="10" y="100" font-size="15" fill="red" > 
    <tref xlink:href="#ReferencedText"/> 
</text>

use

<defs> 
    <text id="Text" x="400" y="200" 
          font-family="Verdana" font-size="100" text-anchor="middle" > 
      マスクされたテキスト 
    </text> 
<use xlink:href="#Text" fill="blue"  />

ellipse 

<ellipse cx="2.5" cy="1.5" rx="2" ry="1" fill="red" />

<g fill="none" stroke="dimgray" stroke-width="1.5" > 
                <line x1="-7" y1="-7" x2="-3" y2="-3"/> 
                <line x1="7" y1="7" x2="3" y2="3"/> 
                <line x1="-7" y1="7" x2="-3" y2="3"/> 
                <line x1="7" y1="-7" x2="3" y2="-3"/> 
</g> 

image

<image id="ShadedRelief" x="24" y="4" width="64" height="82" xlink:href="relief.jpg" /> 

line

<line style="stroke:#eea;stroke-width:8" x1="10" y1="30" x2="260" y2="100"/> 

path

<path style="fill:#daa;fill-rule:evenodd;stroke:red" d="M 230,150 C 290,30 10,255 110,140 z "/> 

style

<path style="fill:#daa;fill-rule:evenodd;stroke:red" d="M 230,150 C 290,30 10,255 110,140 z "/>

polygon

<polygon style="stroke:#24a;stroke-width:1.5;fill:#eefefe" points="10,10 180,10 10,250 10,10" />

polyline

<polyline fill="none" stroke="dimgray" stroke-width="1" points="-3,-6 3,-6 3,1 5,1 0,7 -5,1 -3,1 -3,-5"/>

rect 

<rect x="0" y="0" width="400" height="600" stroke="none" fill="aliceblue" />

svg

<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="10cm" height="5cm" >

text

<text font-family="sans-serif" fill="dimgray" font-size="22px" font-weight="bold" x="58" y="30" pointer-events="none">マップタイトル</text>

font

<text x="10" y="100" font-size="15" fill="red" > 
    サンプルテキスト 
</text>

tspan

<tspan dy="25" x="25">6つのインクカラー入力値。ここに表示されます</tspan>

TIFFをPDFに変換

Aspose.PDFファイル形式は、単一フレームまたはマルチフレームTIFF画像をサポートしています。つまり、.NETアプリケーションでTIFF画像をPDFに変換できます。

TIFFまたはTIF、タグ付き画像ファイル形式は、このファイル形式標準に準拠したさまざまなデバイスで使用されるラスタ画像を表します。TIFF画像は、異なる画像を持つ複数のフレームを含むことができます。Aspose.PDFファイル形式もサポートされており、単一フレームまたはマルチフレームTIFF画像を扱うことができます。

他のラスタファイル形式のグラフィックスと同様に、TIFFをPDFに変換できます。

TIFFをPDFに変換

  1. 新しいDocumentクラスオブジェクトを作成し、ページを追加します。
  2. 入力TIFF画像を読み込みます。
  3. PDFドキュメントを保存します。
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ConvertTIFFtoPDF()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments();

    // Create PDF document
    using (var document = new Aspose.Pdf.Document())
    {
        document.Pages.Add();
        var image = new Aspose.Pdf.Image();
        
        // Load sample Tiff image file
        image.File = dataDir + "TIFFtoPDF.tiff";
        document.Pages[1].Paragraphs.Add(image);
        
        // Save PDF document
        document.Save(dataDir + "TIFFtoPDF_out.pdf");
    }
}

マルチページTIFF画像をマルチページPDFドキュメントに変換し、幅やアスペクト比などのパラメータを制御する必要がある場合は、次の手順に従ってください。

  1. Documentクラスのインスタンスを作成します。
  2. 入力TIFF画像を読み込みます。
  3. フレームのFrameDimensionを取得します。
  4. 各フレームのために新しいページを追加します。
  5. 最後に、画像をPDFページに保存します。

次のコードスニペットは、マルチページまたはマルチフレームTIFF画像をC#でPDFに変換する方法を示しています。

// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ConvertTIFFtoPDF()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments();

    // Create PDF document
    using (var document = new Aspose.Pdf.Document())
    {
        using (var bitmap = new System.Drawing.Bitmap(File.OpenRead(dataDir + "TIFFtoPDF.tif")))
        {
            // Convert multi page or multi frame TIFF to PDF
            var dimension = new FrameDimension(bitmap.FrameDimensionsList[0]);
            var frameCount = bitmap.GetFrameCount(dimension);

            // Iterate through each frame
            for (int frameIdx = 0; frameIdx <= frameCount - 1; frameIdx++)
            {
                var page = document.Pages.Add();

                bitmap.SelectActiveFrame(dimension, frameIdx);

                using (var currentImage = new MemoryStream())
                {
                    bitmap.Save(currentImage, ImageFormat.Tiff);

                    var imageht = new Aspose.Pdf.Image
                    {
                        ImageStream = currentImage,
                        //Apply some other options
                        //ImageScale = 0.5
                    };
                    page.Paragraphs.Add(imageht);
                }
            }
        }

        // Save PDF document
        document.Save(dataDir + "TIFFtoPDF_out.pdf");
    }
}

CDRをPDFに変換

CDRは、Corel Corporationによって開発されたファイル形式で、主にベクターグラフィック画像や図面に使用されます。CDRファイル形式は、ほとんどの画像編集プログラムによって認識されます。CDR形式は、Corel Drawアプリケーションのデフォルト形式です。

CDRファイルをPDF形式に変換するための次のコードスニペットを確認してください。

CDRをPDFに変換

  1. CdrLoadOptionsクラスのインスタンスを作成します。
  2. ソースファイル名とオプションを指定してDocumentクラスのインスタンスを作成します。
  3. 希望のファイル名でドキュメントを保存します。
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ConvertCDRtoPDF()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments();

    // Open CDR file
    using (var document = new Aspose.Pdf.Document(dataDir + "CDRtoPDF.cdr", new CdrLoadOptions()))
    {
        // Save PDF document
        document.Save(dataDir + "CDRtoPDF_out.pdf");
    }
}

DJVUをPDFに変換

DjVuは、LizardTechによって開発された圧縮画像形式です。このファイル形式は、特にテキスト、画像、インデックス付きカラー画像、線画の組み合わせを含むさまざまな種類のスキャンされた文書を保存するために設計されました。

DJVUファイルをPDF形式に変換するための次のコードスニペットを確認してください。

DJVUをPDFに変換

  1. DjvuLoadOptionsクラスのインスタンスを作成します。
  2. ソースファイル名とオプションを指定してDocumentクラスのインスタンスを作成します。
  3. 希望のファイル名でドキュメントを保存します。
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ConvertDJVUtoPDF()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments();
    
    // Open DJVU file
    using (var document = new Aspose.Pdf.Document(dataDir + "CDRtoPDF.djvu", new DjvuLoadOptions()))
    {
        // Save PDF document
        document.Save(dataDir + "CDRtoPDF_out.pdf");
    }
}

HEICをPDFに変換

HEICをPDFに変換

HEICファイルは、複数の画像を単一のファイルにコレクションとして保存できる高効率コンテナ画像ファイル形式です。 HEIC画像を読み込むには、https://www.nuget.org/packages/FileFormat.Heic/ nugetパッケージへの参照を追加する必要があります。 Aspose.PDFを使用してHEIC画像をPDFに変換します。

// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ConvertHEICtoPDF()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments();

    // Open HEIC file
    using (var fs = new FileStream(dataDir + "HEICtoPDF.heic", FileMode.Open))
    {
        var image = FileFormat.Heic.Decoder.HeicImage.Load(fs);
        var pixels = image.GetByteArray(PixelFormat.Rgb24);
        var width = (int)image.Width;
        var height = (int)image.Height;

        using (var document = new Aspose.Pdf.Document())
        {
            var page = document.Pages.Add();
            var asposeImage = new Aspose.Pdf.Image();
            asposeImage.BitmapInfo = new Aspose.Pdf.BitmapInfo(pixels, width, height, Aspose.Pdf.BitmapInfo.PixelFormat.Rgb24);
            page.PageInfo.Height = height;
            page.PageInfo.Width = width;
            page.PageInfo.Margin.Bottom = 0;
            page.PageInfo.Margin.Top = 0;
            page.PageInfo.Margin.Right = 0;
            page.PageInfo.Margin.Left = 0;

            page.Paragraphs.Add(asposeImage);

            // Save PDF document
            document.Save(dataDir + "HEICtoPDF_out.pdf");
        }
    }
}