Thursday, April 2, 2015

Convert Multiple Images to PDF using iTextSharp?

Why only two images allowed, cause failure with third ?Oh I used another way before, a third party tool, download and add reference to project then use the following code.There is no limitation of image amount. Ref this article.



// Create a pdf document with a section and page added.
PdfDocument doc = new PdfDocument();
PdfSection section = doc.Sections.Add();
PdfPageBase page = doc.Pages.Add();
//Load a tiff image from system
PdfImage image = PdfImage.FromFile(@"D:\images\bear.tif");
//Set image display location and size in PDF
float widthFitRate = image.PhysicalDimension.Width / page.Canvas.ClientSize.Width;
float heightFitRate = image.PhysicalDimension.Height / page.Canvas.ClientSize.Height;
float fitRate = Math.Max(widthFitRate, heightFitRate);
float fitWidth = image.PhysicalDimension.Width / fitRate;
float fitHeight = image.PhysicalDimension.Height / fitRate;
page.Canvas.DrawImage(image, 30, 30, fitWidth, fitHeight);
//save and launch the file
doc.SaveToFile("image to pdf.pdf");
doc.Close();
System.Diagnostics.Process.Start("image to pdf.pdf");


No comments:

Post a Comment