728x90
반응형
728x170
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
728x90
반응형
그리드형(광고전용)
'C# > WinForm' 카테고리의 다른 글
[C#/WINFORM] 리소스 이미지 구하기 (0) | 2015.01.25 |
---|---|
[C#/WINFORM] 리소스 커서 구하기 (0) | 2015.01.25 |
[C#/WINFORM] 리소스 스트림 구하기 (0) | 2015.01.25 |
[C#/WINFORM] SendKeys 클래스 : Send 정적 메소드를 사용해 TextBox 객체에 키 입력 보내기 (0) | 2015.01.22 |
[C#/WINFORM] 거리 구하기 (0) | 2015.01.01 |
[C#/WINFORM] 투명 비트맵 그리기 (0) | 2014.12.27 |
[C#/WINFORM] 리소스 스트림 구하기 (0) | 2014.12.27 |
[C#/WINFORM] ClickOnce 배포하기 (0) | 2014.12.06 |
[C#/WINFORM] Application 클래스 : ThreadException 이벤트를 사용해 예외 처리하기 (0) | 2014.12.06 |
[C#/WINFORM] Application 클래스 : ExitThread 메소드를 사용해 프로그램 종료하기 (0) | 2014.12.06 |
댓글을 달아 주세요