Streamline Your Workflow: Convert Multiple Images to PDF for Free with C#/VB.NET
Tags: csharp, api, tutorial, programming
Published: 2023-06-21 08:16:08

In today's digital age, it's common to have multiple images stored on your computer or mobile device. Sometimes, you may need to share these images with others in a more organized and accessible format. One way to achieve this is by converting the images into a single PDF file. In this article, we will explore how to convert multiple images into a single PDF for free using C#/VB.NET.

## Programming Environment 
Step 1: Download and Install [Free Spire.PDF for .NET](https://www.e-iceblue.com/Download/download-pdf-for-net-free.html)
Free Spire.PDF for .NET is a free PDF component that allows developers to create, manipulate, and convert PDF documents in .NET applications. You can download the Free Spire.PDF for .NET library from the official website.

Step 2: Declare Required Libraries and Variables
Once you have downloaded the Free Spire.PDF for .NET library, you need to declare it in your project by adding the following namespace:

## Specific Steps:
The following are the detailed steps.

1. Create a PdfDocument object.
2. Set the page margins to zero using PdfDocument.PageSettings.SetMargins() method.
3. Get the folder where the images are stored.
4. Iterate through each image file in the folder, and get the width and height of a specific image.
5. Add a new page that has the same width and height as the image to the PDF document using PdfDocument.Pages.Add() method.
6. Draw the image on the page using PdfPageBase.Canvas.DrawImage() method.
7. Save the document using PdfDocument.SaveToFile() method.

## Full Code:
**C#**
```
using Spire.Pdf;
using Spire.Pdf.Graphics;
using System.Drawing;

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

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

            //Get the folder where the images are stored
            DirectoryInfo folder = new DirectoryInfo(@"C:\Users\Administrator\Desktop\Images");

            //Iterate through the files in the folder
            foreach (FileInfo file in folder.GetFiles())
            {
                //Load a particular image 
                Image image = Image.FromFile(file.FullName);

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

                //Add a page that has 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("CombinaImagesToPdf.pdf");
            doc.Dispose();
        }
    }
}
```
VB.NET
```
Imports Spire.Pdf
Imports Spire.Pdf.Graphics
Imports System.Drawing

Namespace ConvertMultipleImagesIntoPdf
    Friend Class Program
        Private Shared Sub Main(ByVal args As String())
            'Create a PdfDocument object
            Dim doc As PdfDocument = New PdfDocument()

            'Set the page margins to 0
            doc.PageSettings.SetMargins(0)

            'Get the folder where the images are stored
            Dim folder As DirectoryInfo = New DirectoryInfo("C:\Users\Administrator\Desktop\Images")

            'Iterate through the files in the folder
            For Each file In folder.GetFiles()
                'Load a particular image 
                Dim image As Image = Image.FromFile(file.FullName)

                'Get the image width and height
                Dim width As Single = image.PhysicalDimension.Width
                Dim height As Single = image.PhysicalDimension.Height

                'Add a page that has the same size as the image
                Dim page As PdfPageBase = doc.Pages.Add(New SizeF(width, height))

                'Create a PdfImage object based on the image
                Dim pdfImage As PdfImage = PdfImage.FromImage(image)

                'Draw image at (0, 0) of the page
                page.Canvas.DrawImage(pdfImage, 0, 0, pdfImage.Width, pdfImage.Height)
            Next

            'Save to file
            doc.SaveToFile("CombinaImagesToPdf.pdf")
            doc.Dispose()
        End Sub
    End Class
End Namespace
```
## Effective Shot

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3y7alsm7o6aa1qsht1u2.jpg)
## Conclusion:
In addition to images to PDF conversion, [Spire.PDF for .NET](https://www.e-iceblue.com/Tutorials/Spire.PDF/Spire.PDF-Program-Guide/Spire.PDF-Program-Guide-Content.html) offers many other useful features. For example, you can use Spire.PDF to [Create PDF Portfolio](https://www.e-iceblue.com/Tutorials/Spire.PDF/Program-Guide/Document-Operation/Create-a-PDF-Portfolio-in-C.html), [attachment](https://www.e-iceblue.com/Tutorials/Spire.PDF/Spire.PDF-Program-Guide/PDF-Attachment-Remove-Attachments-from-PDF-with-C-VB.NET.html)/[image extract](http://www.e-iceblue.com/Knowledgebase/Spire.PDF/Spire.PDF-Program-Guide/How-to-Extract-Image-From-PDF-in-C.html), PDF merge/split, graph/image drawing and inserting, table creation etc. Besides, Spire.PDF for .NET can be applied to easily converting SVG ,[HTML to PDF](https://www.e-iceblue.com/Tutorials/Spire.PDF/Spire.PDF-Program-Guide/Convert-HTML-to-PDF-Customize-HTML-to-PDF-Conversion-by-Yourself.html) and [convert PDF to Excel](https://www.e-iceblue.com/Tutorials/Spire.PDF/Program-Guide/Conversion/Convert-PDF-to-Excel-in-C-VB.NET.html) with C#/VB.NET in high quality. With the additional features it offers, it is a complete solution for all your PDF file needs.