C# PDFをTIFFに変換

<p><strong>今回はSpire.PDFという無料で使いやすいライブラリを利用して、PDFをTIFF、そしそして逆にTIFFをPDFに変換する方法を紹介します。</strong></p>
<h4><strong> </strong><strong>下準備</strong></h4>
<p><strong>1.E-iceblueの公式サイトから<a href="https://www.e-iceblue.com/Download/download-pdf-for-net-free.html">Free Spire.PDF</a>無料版をダウンロードしてください。</strong></p>
<p><img src="https://cdn-ak.f.st-hatena.com/images/fotolife/l/lendoris/20210602/20210602155356.png" alt="f:id:lendoris:20210602155356p:plain" title="" class="hatena-fotolife" itemprop="image" /></p>
<p><strong>2.Visual Studioを起動して新規プロジェクトを作成してから、インストールされたファイルにあった相応しいSpire. PDF.dllを参照に追加してください。</strong></p>
<p><strong>（Net 4.0を例としたら、デフォルトパスは“Bin→NET4.0→PDF.dll”というようです。）</strong></p>
<p><img src="https://cdn-ak.f.st-hatena.com/images/fotolife/l/lendoris/20210602/20210602155409.png" alt="f:id:lendoris:20210602155409p:plain" title="" class="hatena-fotolife" itemprop="image" /></p>
<h4><strong>PDFをTIFFに変換</strong></h4>
```C#
using Spire.Pdf;
using System;
using System.Drawing;
using System.Drawing.Imaging;
class Program
{
    static void Main(string[] args)
    {
        //PDFファイルをロードします。
        PdfDocument document = new PdfDocument();
        document.LoadFromFile("sample.pdf");

        //.tiffで保存します。
        JoinTiffImages(SaveAsImage(document), "result.tiff", EncoderValue.CompressionLZW);
    }
    //SaveAsImage()
    private static Image[] SaveAsImage(PdfDocument document)
    {
        Image[] images = new Image[document.Pages.Count];
        for (int i = 0; i < document.Pages.Count; i++)
        {
            images[i] = document.SaveAsImage(i);
        }
        return images;
    }
    //GetEncoderInfo()メソッドを定義します
    private static ImageCodecInfo GetEncoderInfo(string mimeType)
    {
        ImageCodecInfo[] encoders = ImageCodecInfo.GetImageEncoders();
        for (int j = 0; j < encoders.Length; j++)
        {
            if (encoders[j].MimeType == mimeType)
                return encoders[j];
        }
        throw new Exception(mimeType + " mime type not found in ImageCodecInfo");
    }
    //JoinTiffImages()メソッドを定義します
    public static void JoinTiffImages(Image[] images, string outFile, EncoderValue compressEncoder)
    {
        Encoder enc = Encoder.SaveFlag;
        EncoderParameters ep = new EncoderParameters(2);
        ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.MultiFrame);
        ep.Param[1] = new EncoderParameter(Encoder.Compression, (long)compressEncoder);
        Image pages = images[0];
        int frame = 0;
        ImageCodecInfo info = GetEncoderInfo("image/tiff");
        foreach (Image img in images)
        {
            if (frame == 0)
            {
                pages = img;
                pages.Save(outFile, info, ep);
            }

            else
            {
                ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.FrameDimensionPage);

                pages.SaveAdd(img, ep);
            }
            if (frame == images.Length - 1)
            {
                ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.Flush);
                pages.SaveAdd(ep);
            }
            frame++;
        }
    }
}
```
<h4><strong>実行結果</strong></h4>
<p><img src="https://cdn-ak.f.st-hatena.com/images/fotolife/l/lendoris/20210602/20210602160120.png" alt="f:id:lendoris:20210602160120p:plain" title="" class="hatena-fotolife" itemprop="image" /></p>
<h4><strong>TIFFをPDFに変換</strong></h4>
```C#
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using Spire.Pdf;
using Spire.Pdf.Graphics;

namespace SplitTiff
{
    class Program
    {
        static void Main(string[] args)
        {
            using (PdfDocument pdfDoc = new PdfDocument())
            {
                //tiffをロードします。
                Image tiffImage = Image.FromFile("test.tiff");
                Image[] imgs = SplitImages(tiffImage);

                for (int i = 0; i < imgs.Length; i++)
                {
                    PdfImage pdfImg = PdfImage.FromImage(imgs[i]);
                    PdfUnitConvertor uinit = new PdfUnitConvertor();
                    SizeF pageSize = uinit.ConvertFromPixels(imgs[i].Size, PdfGraphicsUnit.Point);
                    PdfPageBase page = pdfDoc.Pages.Add(pageSize, new PdfMargins(0f));
                    float x = (page.Canvas.ClientSize.Width - pageSize.Width) / 2;

                    //PDFで画像を描きます。
                    page.Canvas.DrawImage(pdfImg, x, 0, pageSize.Width,pageSize.Height);
                }

                //Pdfで保存します。
                pdfDoc.SaveToFile("result.pdf");
                System.Diagnostics.Process.Start("result.pdf");

            }
        }
        public static Image[] SplitImages(Image tiffImage)
        {
            int frameCount = tiffImage.GetFrameCount(FrameDimension.Page);
            Image[] frames = new Image[frameCount];
            Guid guid = tiffImage.FrameDimensionsList[0];
            for (int i = 0; i < frameCount; i++)
            {
                FrameDimension dimension = new FrameDimension(guid);

                tiffImage.SelectActiveFrame(dimension, i);
                using (MemoryStream buffer = new MemoryStream())
                {
                    tiffImage.Save(buffer, ImageFormat.Tiff);
                    buffer.Position = 0;
                    frames[i] = Image.FromStream(buffer);
                }
            }

            return frames;
        }

    }
}
```
<h4><strong>実行結果</strong></h4>
<p><img src="https://cdn-ak.f.st-hatena.com/images/fotolife/l/lendoris/20210602/20210602160208.png" alt="f:id:lendoris:20210602160208p:plain" title="" class="hatena-fotolife" itemprop="image" /></p>
<p> </p>