■ 이미지를 PDF 파일로 저장하기

----------------------------------------------------------------------------------------------------

using System;

using System.Drawing;

using System.Windows.Forms;

 

using PdfSharp.Drawing;

using PdfSharp.Pdf;

 

#region 이미지를 PDF 파일로 저장하기 - SavePDFFile(sourceImage, targetPDFFilePath)

 

/// <summary>

/// 이미지를 PDF 파일로 저장하기

/// </summary>

/// <param name="sourceImage">소스 이미지</param>

/// <param name="targetPDFFilePath">타겟 PDF 파일 경로</param>

/// <returns>처리 결과</returns>

public void SavePDFFile(Image sourceImage, string targetPDFFilePath)

{

    PdfDocument pdfDocument = null;

 

    try

    {

        pdfDocument = new PdfDocument();

 

        double sourceImageWidth  = (sourceImage.Width  / 96d) * 72d;

        double sourceImageHeight = (sourceImage.Height / 96d) * 72d;

 

        int pageHeight = (int)((842d / 72d) * 96d);

        int pageCount  = (int)Math.Ceiling(sourceImageHeight / 842d);

 

        for(int i = 0; i < pageCount; i++)

        {

            Bitmap   pageBitmap   = new Bitmap(sourceImage.Width, pageHeight);

            Graphics pageGraphics = Graphics.FromImage(pageBitmap);

 

            pageGraphics.DrawImage

            (

                sourceImage,

                new Rectangle(0, 0, sourceImage.Width, pageHeight),

                new Rectangle(0, i * pageHeight, sourceImage.Width, pageHeight),

                GraphicsUnit.Pixel

            );

 

            PdfPage pdfPage = new PdfPage();

 

            pdfPage.Width = XUnit.FromPoint(sourceImageWidth);

 

            pdfDocument.AddPage(pdfPage);

 

            XImage    xImage    = XImage.FromGdiPlusImage(pageBitmap);

            XGraphics xGraphics = XGraphics.FromPdfPage(pdfPage);

 

            xGraphics.DrawImage(xImage, 0, 0);

        }

 

        pdfDocument.Save(targetPDFFilePath);

    }

    finally

    {

        if(pdfDocument != null)

        {

            pdfDocument.Clone();

        }

    }

}

 

#endregion

----------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker

댓글을 달아 주세요