PDF control Spire.PDF for .NET [conversion] tutorial: convert image to PDF

Spire.Doc  is a class library specialized in manipulating Word documents. It helps developers create, edit, convert and print Microsoft Word documents easily, quickly and efficiently without installing Microsoft Word. With nearly 10 years of professional development experience, the Spire series of office document development tools focus on creating, editing, converting and printing Word/PDF/Excel and other file formats, compact and convenient. 

The E-iceblue  function class library Spire series document processing components are all developed by local Chinese teams, do not rely on third-party software, are not restricted by technology or laws and regulations of other countries, and are also compatible with domestic operating systems such as Zhongke Fangde, Winning Kirin, etc. Compatible with domestic document processing software WPS (such as .wps/.et/.dps and other formats

Download Spire.PDF for.netDownload    Spire.PDF for java

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

Spire.PDF does not provide an easy way to convert images to PDF. But you can create a new PDF document and draw an image at a specified location on a specific page. Depending on whether to generate a PDF with the same page size as the image, this topic can be divided into the following two subtopics.

Install Spire.PDF for.NET

First, you need to add the DLL file included in the Spire.PDF for.NET package as a reference in your .NET project. The DLL file can be downloaded from this link or installed via NuGet.

PM> Install-Package Spire.PDF
Add image to specified location in PDF

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

  • Create a PdfDocument object.
  • Use the PdfDocument.PageSettings.SetMargins() method to set the page margins.
  • Add pages using the PdfDocument.Pages.Add() method
  • Use the Image.FromFile() method to load an image and get the width and height of the image.
  • If the image width is larger than the page (content area) width, resize the image to fit the page width.
  • Create a PdfImage object based on a scaled image or an original image.
  • Use the PdfPageBase.Canvas.DrawImage() method to draw the PdfImage object at the first page (0, 0).
  • Use the PdfDocument.SaveToFile() method to save the document as a PDF file.

【C#】

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");
}
}
}

【VB.NET】

Imports System.Drawing
Imports Spire.Pdf
Imports Spire.Pdf.Graphics

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

'Set the margins
doc.PageSettings.SetMargins(20)

'Add a page
Dim page As PdfPageBase = doc.Pages.Add()

'Load an image
Dim image As Image = Image.FromFile("C:\Users\Administrator\Desktop\announcement.jpg")

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

'Declare a PdfImage variable
Dim pdfImage As PdfImage

'If the image width is larger than page width
If width > page.Canvas.ClientSize.Width Then
'Resize the image to make it to fit to the page width
Dim widthFitRate As single = width / page.Canvas.ClientSize.Width
Dim size As Size = New Size(CType((width / widthFitRate),(Integer)(height / widthFitRate), Integer))
Dim scaledImage As Bitmap = 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)
End If

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

'Save to file
doc.SaveToFile("AddImage.pdf")
End Sub
End Class
End Namespace

Convert image to PDF with same width and height

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

  • Create a PdfDocument object.
  • Use the PdfDocument.PageSettings.SetMargins() method to set the page margins to zero.
  • Use the Image.FromFile() method to load an image and get the width and height of the image.
  • Use the PdfDocument.Pages.Add() method to add pages to the PDF based on the size of the image.
  • Create a PdfImage object from an image.
  • Use the PdfPageBase.Canvas.DrawImage() method to draw a PdfImage object on the first page from coordinates (0, 0).
  • Use the PdfDocument.SaveToFile() method to save the document as a PDF file.

【C#】

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");
}
}
}

【VB.NET】

Imports System.Drawing
Imports Spire.Pdf
Imports Spire.Pdf.Graphics

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

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

'Load an image
Dim image As Image = Image.FromFile("C:\Users\Administrator\Desktop\announcement.jpg")

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

'Add a page of 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)

'Save to file
doc.SaveToFile("ConvertPdfWithSameSize.pdf")
End Sub
End Class
End Namespace

The above is how to convert images to PDF. If you have other questions, you can continue to browse this series of articles to get related tutorials.~

Guess you like

Origin blog.csdn.net/m0_67129275/article/details/131410625