Articles in this section
Category / Section

How to create PDF page based on image size using C# and VB.NET?

2 mins read

Syncfusion Essential PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can create a PDF page based on image size using C# and VB.NET.

Steps to create PDF page based on image size programmatically:

  1. Create a new C# console application project. Create a console application project in WinForms PDF
  2. Install the Syncfusion.Pdf.WinForms NuGet package as reference to your .NET Framework application from NuGet.org. WinForms PDF installation
  3. Include the following namespaces in Program.cs file.

C#

using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
using System.Drawing;

 

VB.NET

Imports Syncfusion.Pdf
Imports Syncfusion.Pdf.Graphics
Imports System.Drawing

 

  1. Use the following code snippet to create a PDF page based on the image size.

C#

//Create a new PDF document
PdfDocument document = new PdfDocument();
//Load the image from the disk
PdfBitmap image = new PdfBitmap("Syncfusion.png");
//Add the first section to the PDF document
PdfSection section = document.Sections.Add();
//Initialize unit converter
PdfUnitConvertor converter = new PdfUnitConvertor();
//Convert image size from pixel to points
SizeF size = converter.ConvertFromPixels(image.PhysicalDimension, PdfGraphicsUnit.Pixel);
//Set section size based on the image size
section.PageSettings.Size = size;
// Set section orientation based on image size (by default Portrait) 
if (image.Width > image.Height)
    section.PageSettings.Orientation = PdfPageOrientation.Landscape;
//Set margin for section
section.PageSettings.Margins.All = 0;
//Add a page to the section
PdfPage page = section.Pages.Add();
//Draw image
page.Graphics.DrawImage(image, 0, 0);
//Save the document
document.Save("Sample.pdf");
//Closes the document 
document.Close(true);
//This will open the PDF file so, the result will be seen in default PDF Viewer 
Process.Start("Sample.pdf");

 

VB.NET

'Create a new PDF document
Dim document As New PdfDocument()
'Load the image from the disk
Dim image As New PdfBitmap("Syncfusion.png")
'Add the first section to the PDF document
Dim section As PdfSection = document.Sections.Add()
'Initialize unit converter
Dim converter As New PdfUnitConvertor()
'Convert image size from pixel to points
Dim size As SizeF = converter.ConvertFromPixels(image.PhysicalDimension,PdfGraphicsUnit.Pixel)
'Set section size based on the image size
section.PageSettings.Size = size
' Set section orientation based on image size (by default Portrait) 
If image.Width > image.Height Then
    section.PageSettings.Orientation = PdfPageOrientation.Landscape
End If 
'Set margin for section
section.PageSettings.Margins.All = 0
'Add a page to the section
Dim page As PdfPage = section.Pages.Add()
'Draw image
page.Graphics.DrawImage(image, 0, 0)
'Save the document
document.Save("Sample.pdf")
'Closes the document
document.Close(True)
'This will open the PDF file so, the result will be seen in default PDF Viewer 
Process.Start("Sample.pdf")

 

A complete working sample can be downloaded from PDFSample.zip.

By executing the program, you will get the PDF document as follows. Output document screenshot i WinForms PDF

Take a moment to peruse the documentation, where you can find options like inserting images in an existing PDF, vector images, image masking, replacing image in an existing PDF document, image pagination, add margin to PDF, rotating a PDF page with code examples.

Refer here to explore the rich set of Syncfusion Essential PDF features.

Note:

Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments
Please  to leave a comment
Access denied
Access denied