Convert Images to PDF in C# and VB.NET

If you want to share images across multiple devices and applications, conversion to PDF is the perfect solution. For example, you can maintain the quality of your images in a single format without making any alterations that would interfere with your project.

With PDF .Net, you can easily convert images in formats such as JPG, GIF, TIFF, and PNG to PDF documents programmatically using C# and VB.NET.

These are all the image formats that PDF Focus .Net supports for PDF conversion:

  • BMP
  • GIF
  • JPEG
  • PNG
  • TIFF
  • WMP

The following example shows how to convert a PNG image to a PDF document.

Complete code

using System;
using SautinSoft.Pdf;
using System.IO;

class Program
{
	static void Main()
	{
		// This property is necessary only for licensed version.
		//SautinSoft.Pdf.Serial = "XXXXXXXXXXX";
		// Create new document.
		using (var document = new PdfDocument())
		{
			// Add new page.
			var page = document.Pages.Add();

			// Add image from PNG file.
			var image = PdfImage.Load("stockholm.png");
			page.Content.DrawImage(image, new PdfPoint(0, 0));

			// Set page size.
			page.SetMediaBox(image.Width, image.Height);

			// Save as PDF file.
			document.Save("convert-png-image.pdf");
		}
	}
}

            

Download.

In the example below you can see how to convert multiple JPG images into a single PDF document.

using System;
using SautinSoft.Pdf;
using System.IO;

class Program
{
	static void Main()
	{
		// This property is necessary only for licensed version.
		//SautinSoft.Pdf.Serial = "XXXXXXXXXXX";
		string[] jpgs = { "penguin.jpg", "jellyfish.jpg", "dolphin.jpg", "lion.jpg", "deer.jpg" };
		// Create new document.
		using (var document = new PdfDocument())
		{
			// For each image add new page with margins.
			foreach (var jpg in jpgs)
			{
				var page = document.Pages.Add();
				double margins = 20;

				// Load image from JPG file.
				var image = PdfImage.Load(jpg);

				// Set page size.
				page.SetMediaBox(image.Width + 2 * margins, image.Height + 2 * margins);

				// Draw backgroud color.
				var backgroud = page.Content.Elements.AddPath();
				backgroud.AddRectangle(new PdfPoint(0, 0), page.Size);
				backgroud.Format.Fill.IsApplied = true;
				backgroud.Format.Fill.Color = PdfColor.FromRgb(1, 0, 1);

				// Draw image.
				page.Content.DrawImage(image, new PdfPoint(margins, margins));
			}
			// Save as PDF file.
			document.Save("converted-jpg-images.pdf");
		}
	}
}

            

Download.


Advanced Options On Image to PDF Conversions

With PDF .Net, you can control the results when converting an image to PDF. This includes changing the rotation, scale (width and height), and even adding margins to the images that will be available in PDF documents.

The following example shows how to convert a PNG image into multiple images of different sizes in a single PDF document.

using System;
using SautinSoft.Pdf;
using System.IO;

class Program
{
	static void Main()
	{
		// This property is necessary only for licensed version.
		//SautinSoft.Pdf.Serial = "XXXXXXXXXXX";

		// Create new document.
		using (var document = new PdfDocument())
		{
			// Load image from PNG file.
			var image = PdfImage.Load("stockholm.png");

			double width = image.Width;
			double height = image.Height;
			double ratio = width / height;

			// Add image four times, each time with 20% smaller size.
			for (int i = 0; i < 4; i++)
			{
				width *= 0.8;
				height = width / ratio;

				var page = document.Pages.Add();
				page.Content.DrawImage(image, new PdfPoint(0, 0), new PdfSize(width, height));
				page.SetMediaBox(width, height);
			}

			document.Save("convert-scaled-images.pdf");
		}
	}
}

            

Download.


If you need a new code example or have a question: email us at support@sautinsoft.com or ask at Online Chat (right-bottom corner of this page) or use the Form below:



Questions and suggestions from you are always welcome!

We are developing .Net components since 2002. We know PDF, DOCX, RTF, HTML, XLSX and Images formats. If you need any assistance with creating, modifying or converting documents in various formats, we can help you. We will write any code example for you absolutely free.