Articles in this section
Category / Section

How to insert different formats of images in a PDF document

5 mins read

The Syncfusion® .NET PDF library used to create, read, and edit PDF documents. Using this library, you can insert different formats of images into a PDF document in ASP.NET Core platform.

Steps to insert different formats of images into a PDF document programmatically

  1. Create a new C# ASP.NET Core Web App MVC project.
    Web application.png
  2. In configuration window, name your project and click Next.
    Name the project.png
  3. Install the Syncfusion.Pdf.Imaging.Net.Core package as reference to your ASP.NET Core applications from NuGet.org.
    NuGet Package.png
  4. A default controller with name HomeController.cs gets added on creation of ASP.NET Core project. Include the following namespaces in that HomeController.cs file.
using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
using Syncfusion.Drawing;
  1. A default action method named Index will be present in HomeController.cs. Right click on Index method and select Go To View where you will be directed to its associated view page index.cshtml.
  2. Add a new button in Index.cshtml as shows below.
@{
    Html.BeginForm("ImagesToPdfSample", "Home", FormMethod.Get);
    {
        <div>
            <input type="submit" value="Create Document" style="width:150px;height:27px" />
        </div>
    }
    Html.EndForm();
}
  1. Add a new method DifferentFormatsImagetoPDF in HomeController.cs and include the below code example to insert different formats of images in a PDF document in ASP.NET Core.
//Create a new PDF document
PdfDocument doc = new PdfDocument();

//Retrieve all files from the /Data/ directory
string[] files = Directory.GetFiles(_webHostEnvironment.WebRootPath + "/Data/");

foreach (var file in files)
{
    //Get the file extension to determine the image type
    string extension = Path.GetExtension(file);
    
    //Open the image file from disk
    FileStream imageStream = new FileStream(file, FileMode.Open, FileAccess.Read);
    
    //Declare a PdfImage variable to store the image
    PdfImage image;
    
    //Check if the file is a supported image type (JPG, PNG, or JPEG)
    if (extension == ".jpg" || extension == ".png" || extension == ".jpeg")
    {
        //Load the image as a bitmap if the extension is .jpg, .png, or .jpeg
        image = new PdfBitmap(imageStream);
    }
    else
    {
        //If the image is a TIFF file, load it as a TIFF image
        image = new PdfTiffImage(imageStream);
    }

    //Add a new section to the PDF document
    PdfSection section = doc.Sections.Add();
    
    //Set the page margins to 0 (no margin)
    section.PageSettings.Margins.All = 0;
    
    //Set the page size to match the image dimensions (width and height of the image)
    section.PageSettings.Size = new SizeF(image.Width, image.Height);
    
    //Add a new page to the section
    PdfPage page = section.Pages.Add();
    
    //Create graphics for the new page to allow drawing
    PdfGraphics graphics = page.Graphics;
    
    //Draw the image on the page at position (0, 0) with its original dimensions
    graphics.DrawImage(image, new PointF(0, 0), new SizeF(image.Width, image.Height));
}

//Create a new memory stream to save the PDF document
MemoryStream stream = new MemoryStream();

//Save the PDF document to the memory stream
doc.Save(stream);

//Set the position of the memory stream to 0 to start reading from the beginning
stream.Position = 0;

//Create a FileStreamResult to allow downloading the PDF document as a file
FileStreamResult fileStreamResult = new FileStreamResult(stream, "application/pdf");

//Set the name of the PDF file to be downloaded
fileStreamResult.FileDownloadName = "Sample.pdf";

//Return the FileStreamResult to the browser for download
return fileStreamResult;

A complete working sample to insert different formats of images in a PDF document can be download from GitHub.

By executing the program, you will get the PDF document as follows.

Output.png

Take a moment to peruse the documentation. You can find the other options like inserting an image in a new and existing PDF document, inserting vector image, replacing images in an existing PDF document, Image pagination, applying transparency, and rotation to the image, convert multi-page TIFF to PDF.

Conclusion
I hope you enjoyed learning about how to insert different formats of images in a PDF document.

You can refer to our ASP.NET Core PDF feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our ASP.NET Core PDF example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!

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