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
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.
- Create a PdfDocument object.
- Set the page margins to zero using PdfDocument.PageSettings.SetMargins() method.
- Get the folder where the images are stored.
- Iterate through each image file in the folder, and get the width and height of a specific image.
- Add a new page that has the same width and height as the image to the PDF document using PdfDocument.Pages.Add() method.
- Draw the image on the page using PdfPageBase.Canvas.DrawImage() method.
- 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
Conclusion:
In addition to images to PDF conversion, Spire.PDF for .NET offers many other useful features. For example, you can use Spire.PDF to Create PDF Portfolio, attachment/image extract, 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 and convert PDF to Excel with C#/VB.NET in high quality. With the additional features it offers, it is a complete solution for all your PDF file needs.
Top comments (0)