Created
January 19, 2014 04:03
-
-
Save gythialy/8500364 to your computer and use it in GitHub Desktop.
convert jpg to pdf by c#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
tusharuit25
commented
Feb 23, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment