■ 이미지를 PDF 파일로 저장하는 방법을 보여준다.
▶ 예제 코드 (C#)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
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 |