Convert Image to PDF in C#, VB.NET

When it comes to the conversion of image to PDF, people are commonly bothered by various problems such as: hardly load images to PDF files from streams or files on disk directly; some special image formats are neither supported by your tools nor clearly shown when they are converted to PDF. However, all these are not conversion problems at all for Spire.PDF users.

Spire.PDF, as a PDF component allows its users to not only to convert images of commonly used formats to PDF document such as jpg, bmp, png, but also to convert gif, tif and ico images to PDF, which can be the most special feature during the conversion task of image to PDF. Besides, Spire.PDF supports to load images from stream and files on disk. These distinct characters, not have the slightest doubt, provides great convenience for developers.

Below shows the effect of converting image to PDF via Spire.PDF through a screenshot.

Image to PDF

Apart from helping you accomplish your image to PDF conversion task in a quick and fast way via C#, VB.NET, Spire.PDF also can Extract text and images from PDF files. That is to say, you never worry about where your images located, you can load them to PDF as well as extract from PDF. Please Download Spire.PDF before you start your work. Below steps are for you to convert your image to PDF with clear effect. The screenshot is displayed after the procedure.

Step1: Use C#/VB.NET to create a PDF document

In this step, you need to create a new PDF file first, then, add a section in the newly built PDF, at last, add a page in the section that you just added.

[C#]
// Create a pdf document with a section and page added.
 PdfDocument doc = new PdfDocument();
 PdfSection section = doc.Sections.Add();
 PdfPageBase page = doc.Pages.Add();

[VB.NET]
 ' Create a pdf document with a section and page added.
 Dim doc As New PdfDocument()
 Dim section As PdfSection = doc.Sections.Add()
 Dim page As PdfPageBase = doc.Pages.Add()

Step2: Load an image to PDF and set image location

First, load an image of any format among jpg, bmp, png, gif, tif and ico from system. A tiff image is loaded and its location is being set in this method.

[C#]
//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);
[VB.NET]
'Load a tif image from system
 Dim image As PdfImage = PdfImage.FromFile("D:\images\bear.tif")
 'Set image display location and size in PDF
 Dim widthFitRate As Single = image.PhysicalDimension.Width / page.Canvas.ClientSize.Width
 Dim heightFitRate As Single = image.PhysicalDimension.Height / page.Canvas.ClientSize.Height
 Dim fitRate As Single = Math.Max(widthFitRate, heightFitRate)
 Dim fitWidth As Single = image.PhysicalDimension.Width / fitRate
 Dim fitHeight As Single = image.PhysicalDimension.Height / fitRate
 page.Canvas.DrawImage(image, 30, 30, fitWidth, fitHeight)

Step3: Save the image to PDF file and launch the file

[C#]
 //save and launch the file
 doc.SaveToFile("image to pdf.pdf");
 doc.Close();
 System.Diagnostics.Process.Start("image to pdf.pdf");
[VB.NET]
 'save and launch the file
 doc.SaveToFile("image to pdf.pdf")
 doc.Close()
 System.Diagnostics.Process.Start("image to pdf.pdf")