소스내용.txt
iTextSharp.zip
nuget 에서 iTextSharp 검색해여 "iTextSharp" 를 설치하면 iTextSharp 이 참조됩니다.
참고로 4.1.6 버전까지는 상용제품에 포함하는데 문제는 없습니다.
http://stackoverflow.com/questions/1762842/is-the-itextsharp-dll-free-to-use-and-redistribute-with-my-web-application-proje
Version 5.0.0 and up is licensed under the AGPL (strong copyleft).
Version 4.1.6 and previous are still licensed under the MPL/LGPL (weak copyleft).
이를 설치하기 위에 nuget 의 command 에 실행합니다. 어려우시면 iTextSharp.zip 파일을 다운로드 받으세요.
Install-Package iTextSharp-LGPL
코드 시작~~
using iTextSharp.text;
using iTextSharp.text.pdf;
public ActionResult Sample()
{
string jpgfile = @"c:\users\lucks\documents\visual studio 2015\Projects\WebApplication2\WebApplication2\사이즈조절.PNG";
string pdf = @"c:\users\lucks\documents\visual studio 2015\Projects\WebApplication2\WebApplication2\sample.pdf";
ConvertJPG2PDF(jpgfile, pdf);
return View();
}
void ConvertJPG2PDF(string jpgfile, string pdf)
{
var document = new Document(iTextSharp.text.PageSize.A4, 25, 25, 25, 25);
using (var stream = new FileStream(pdf, FileMode.Create, FileAccess.Write, FileShare.None))
{
PdfWriter.GetInstance(document, stream);
document.Open();
using (var imageStream = new FileStream(jpgfile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
var image = Image.GetInstance(imageStream);
if (image.Height > iTextSharp.text.PageSize.A4.Height - 25)
{
image.ScaleToFit(iTextSharp.text.PageSize.A4.Width - 25, iTextSharp.text.PageSize.A4.Height - 25);
}
else if (image.Width > iTextSharp.text.PageSize.A4.Width - 25)
{
image.ScaleToFit(iTextSharp.text.PageSize.A4.Width - 25, iTextSharp.text.PageSize.A4.Height - 25);
}
image.Alignment = iTextSharp.text.Image.ALIGN_MIDDLE;
document.Add(image);
}
document.Close();
}
}