Draw an image into PDF and save it as image at the same size

This article mainly introduces a detail method to draw an image into pdf document and make sure the pdf page size is same with image size. This method would show you how to draw an image into PDF file and save the PDF as image file with the same size of image via a .NET PDF component in C#, VB.NET.

Using Spire.PDF, you can not only draw image into PDF but also save the PDF as an image file. In my solution, I used the size of image as the PDF size via PdfUnitConvertor.ConvertFromPixels() function to get the image size in point. You can view output pdf file and image file as below pictures

Output PDF file:

Draw image in pdf with same size

Output image file:

Draw image in pdf with same size

The main steps of the method

Draw an image into PDF

Step 1: Create a new PDF document.

PdfDocument doc = new PdfDocument();

Step 2: Use PdfImage.FromImage() function to load pdf image.

PdfImage pdfimage = PdfImage.FromImage(image); 

Step 3: Use PdfUnitConvertor.ConvertFromPixels() function to get the image size in point and create a new page with image size.

PdfUnitConvertor uinit = new PdfUnitConvertor();
SizeF pageSize = uinit.ConvertFromPixels(image.Size, PdfGraphicsUnit.Point);
PdfPageBase page = doc.Pages.Add(pageSize, new PdfMargins(0f));

Step 4: Draw the PDF image into the PDF page.

page.Canvas.DrawImage(pdfimage, new PointF(0, 0));

Step 5: Save to a PDF file.

doc.SaveToFile("sample.pdf");

Save the PDF as image file

Step 1: Load the PDF file.

doc.LoadFromFile("sample.pdf");

Step 2: Use the doc.SaveAsImage( ) function to save the PDF as image.

Image img = doc.SaveAsImage(0);

Step 3: Save to an image file.

img.Save("sample.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

Download and install Spire.Pdf for .NET and use below code to experience this solution to draw an image into PDF and save it as image file with original size.

The full code:

[C#]
using Spire.Pdf;
using Spire.Pdf.Graphics;
using System.Drawing;
namespace ConvertPdfToImage
{
    class Program
    {
        static void Main(string[] args)
        {  
          
            PdfDocument doc = new PdfDocument();

            Image image = Image.FromFile(@"sample.jpg");
            PdfImage pdfimage = PdfImage.FromImage(image); 

            PdfUnitConvertor uinit = new PdfUnitConvertor();
            SizeF pageSize = uinit.ConvertFromPixels(image.Size, PdfGraphicsUnit.Point);
            PdfPageBase page = doc.Pages.Add(pageSize, new PdfMargins(0f));

            page.Canvas.DrawImage(pdfimage, new PointF(0, 0));
            doc.SaveToFile("sample.pdf");

            doc.LoadFromFile("sample.pdf");
            Image img = doc.SaveAsImage(0);
            img.Save("sample.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
        }
    }
}
[VB.NET]
Imports Spire.Pdf
Imports Spire.Pdf.Graphics
Imports System.Drawing
Module Module1
    Sub Main()
       
        Dim doc As New PdfDocument()

        Dim image As Image = image.FromFile("sample.jpg")
        Dim pdfimage As PdfImage = pdfimage.FromImage(image)

        Dim uinit As New PdfUnitConvertor()
        Dim pageSize As SizeF = uinit.ConvertFromPixels(image.Size, PdfGraphicsUnit.Point)
        Dim page As PdfPageBase = doc.Pages.Add(pageSize, New PdfMargins(0.0F))

        page.Canvas.DrawImage(pdfimage, New PointF(0, 0))

        doc.SaveToFile("sample.pdf")

        doc.LoadFromFile("sample.pdf")
        Dim img As Image = doc.SaveAsImage(0)
        img.Save("sample.jpg", System.Drawing.Imaging.ImageFormat.Jpeg)
    End Sub
End Module