How to Start Tutorials Troubleshooting Main Operations Convert PDF Read PDF Edit PDF PDF Report Generator Work with PDF Modules PDF Document PDF Pages Text Image Graph & Path Annotation, Markup & Drawing Redaction Security Digital Signature Forms Watermark Bookmark Link File Attachment File Metadata Printing Work with Other SDKs Barcode read Barcode create OCR Twain

C# PDF Generator Library
How to create PDF file with images in C# ASP.NET, Windows application


C# Demo Code to add an image to adobe pdf file





In the following C# tutorial, you will learn how to create a PDF file programmatically with images embeded in your C# ASP.NET Web application and Windows application.

  • Embed images in created PDF report file
  • Insert scaled images

How to create a PDF with images programmatically using C#

  1. Download XDoc.PDF Generator C# library
  2. Install C# library to create PDF file with images
  3. Step by Step Tutorial












Add an image to the PDF document in C#


In the C# sample code below, you will know how to embed images with original image width and height in the newly created PDF file

  • Use class PDFBuildHandler to create a new PDF file
  • Create an Image object from an existing image file
  • Add the Image object to the PDF document object
  • Save the newly created PDF file



String outputFilePath = Program.RootPath + "\\" + "Sample21.pdf";

String imageFilePath = Program.RootPath + "\\" + "Image001.bmp";

Document document = new Document();
PDFBuildHandler.Create(document, outputFilePath);

//  open document
document.Open();

document.Add(new Paragraph("Sample: add an image."));

//  load an image from the source file.
Image image = new Image(imageFilePath);
document.Add(image);

document.Add(new Paragraph("Image Width: " + image.Width + " pt.; Image Height: " + image.Height + " pt."));

//  close document and save to the output file
document.Close();




Insert an image with the specified size to PDF file using C#


In the C# sample code below, you will know how to embed scaled images in the newly created PDF file

  • Use class PDFBuildHandler to create a new PDF file
  • Create an Image object from an existing image file
  • Scale the image width and height
  • Add the scaled Image object to the PDF document object
  • Save the newly created PDF file



String outputFilePath = Program.RootPath + "\\" + "Sample22.pdf";

String imageFilePath = Program.RootPath + "\\" + "Image001.bmp";

Document document = new Document();
PDFBuildHandler.Create(document, outputFilePath);

//  open document
document.Open();

document.Add(new Paragraph("Scale image size to: width = 500 pt.; height = 50 pt."));

Image image1 = new Image(imageFilePath);
//  scale image width to 500 pt. (72 ppi) 
image1.ScaleAbsoluteWidth(500);
//  scale image height to 50 pt. (72 ppi)
image1.ScaleAbsoluteHeight(50);

document.Add(image1);