C#/VB.NET: Convert Images to PDF

Images are a device-friendly file format that can be easily shared across a variety of devices. However, in some circumstances, a more professional format such as PDF is needed to replace images. In this article, you will learn how to convert an image to PDF in C# and VB.NET using Spire.PDF for .NET.

Spire.PDF does not provide a straightforward method to convert images to PDF. But you could create a new PDF document and draw images at the specified locations of a certain page. Depending on whether to generate PDF in a page size equal to that of the image, this topic can be divided into the following two subtopics.

Install Spire.PDF for .NET

To begin with, you need to add the DLL files included in the Spire.PDF for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.PDF

Add an Image to PDF at a Specified location

The following are the steps to add an image as a part of a new PDF document using Spire.PDF for .NET.

  • Create a PdfDocument object.
  • Set the page margins using PdfDocument.PageSettings.SetMargins() method.
  • Add a page using PdfDocument.Pages.Add() method
  • Load an image using Image.FromFile() method, and get the image width and height.
  • If the image width is larger than the page (the content area) width, resize the image to make it to fit to the page width.
  • Create a PdfImage object based on the scaled image or the original image.
  • Draw PdfImage object on the first page at (0, 0) using PdfPageBase.Canvas.DrawImage() method.
  • Save the document to a PDF file using PdfDocument.SaveToFile() method.
  • C#
  • VB.NET
using System.Drawing;
using Spire.Pdf;
using Spire.Pdf.Graphics;

namespace AddImageToPdf
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument object
            PdfDocument doc = new PdfDocument();

            //Set the margins
            doc.PageSettings.SetMargins(20);

            //Add a page
            PdfPageBase page = doc.Pages.Add();

            //Load an image 
            Image image = Image.FromFile(@"C:\Users\Administrator\Desktop\announcement.jpg");

            //Get the image width and height
            float width = image.PhysicalDimension.Width;
            float height = image.PhysicalDimension.Height;
       
            //Declare a PdfImage variable
            PdfImage pdfImage;

            //If the image width is larger than page width
            if (width > page.Canvas.ClientSize.Width)
            {
                //Resize the image to make it to fit to the page width
                float widthFitRate = width / page.Canvas.ClientSize.Width;
                Size size = new Size((int)(width / widthFitRate), (int)(height / widthFitRate));
                Bitmap scaledImage = new Bitmap(image, size);

                //Load the scaled image to the PdfImage object
                pdfImage = PdfImage.FromImage(scaledImage);
            } else
            {
                //Load the original image to the PdfImage object
                pdfImage = PdfImage.FromImage(image);
            }

            //Draw image at (0, 0)
            page.Canvas.DrawImage(pdfImage, 0, 0, pdfImage.Width, pdfImage.Height);

            //Save to file
            doc.SaveToFile("AddImage.pdf");
        }
    }
}

C#/VB.NET: Convert Images to PDF

Convert an Image to PDF with the Same Width and Height

The following are the steps to convert an image to PDF with the same page width and height as the image using Spire.PDF for .NET.

  • Create a PdfDocument object.
  • Set the page margins to zero using PdfDocument.PageSettings.SetMargins() method.
  • Load an image using Image.FromFile() method, and get the image width and height.
  • Add a page to PDF based on the size of the image using PdfDocument.Pages.Add() method.
  • Create a PdfImage object based on the image.
  • Draw PdfImage object on the first page from the coordinate (0, 0) using PdfPageBase.Canvas.DrawImage() method.
  • Save the document to a PDF file using PdfDocument.SaveToFile() method.
  • C#
  • VB.NET
using System.Drawing;
using Spire.Pdf;
using Spire.Pdf.Graphics;

namespace ConvertImageToPdfWithSameSize
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument object
            PdfDocument doc = new PdfDocument();

            //Set the margins to 0
            doc.PageSettings.SetMargins(0);

            //Load an image 
            Image image = Image.FromFile(@"C:\Users\Administrator\Desktop\announcement.jpg");

            //Get the image width and height
            float width = image.PhysicalDimension.Width;
            float height = image.PhysicalDimension.Height;

            //Add a page of the same size as the image
            PdfPageBase page = doc.Pages.Add(new SizeF(width, height));

            //Create a PdfImage object based on the image
            PdfImage pdfImage = PdfImage.FromImage(image);
 
            //Draw image at (0, 0) of the page
            page.Canvas.DrawImage(pdfImage, 0, 0, pdfImage.Width, pdfImage.Height);

            //Save to file
            doc.SaveToFile("ConvertPdfWithSameSize.pdf");
        }
    }
}

C#/VB.NET: Convert Images to PDF

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.