How to Add an Image in Runtime Generated PDF File

Introduction

This article describes how to add an image to a runtime generated PDF file. For generating the PDF file, you need to use a PDF generator library.

Note: I will use the "iTextSharp.dll" as a PDF generator library. You can download it from the following

  • Links

http://sourceforge.net/projects/itextsharp
https://www.nuget.org/packages/iTextSharp/

Or

  •  You can download the attached file of this article.

To explain this article, I will use the following procedure:

  1.   Add a reference of the downloaded "iTextSharp.dll" to the project/website.
  2.   Write some code in the ".cs" file to generate the PDF file with some text for the "button click" event.

The following are the details of the preceding procedure.

Step 1

  • Create a new empty website named "PDF_Generation".



  • Right-click on the website and click on "Add Reference…".



  • Use the following sub steps in the "Reference Manager".
  1. Click on the Browse tab on the left side
  2. Click on the Browse button at the bottom 
  3. Select the "iTextSharp.dll" from the system 
  4. Click on the Add button



    Finally, it will look like:



    Now click on the "OK" button and see the "Solution Explorer" where the "iTextSharp.dll" reference has been added to the "Bin" folder.


Step 2
  • Add a new Page named "GenerateFile.aspx".



  • Add a folder named "Images" and add an Image named "photo.jpg" into it.


  • Add a Button with the following Onclick event (to generate the PDF) on the page.
    1. <asp:Button ID="btnGenerate" runat="server" Text="Generate PDF"    
    2. OnClick="btnGenerate_Click" />


  • Add the 2 namespaces on top of the ".cs" file.
    1. using iTextSharp.text;    
    2. using iTextSharp.text.pdf; 
  • Write the code to generate the PDF file on the click event of the button.
    1. protected void btnGenerate_Click(object sender, EventArgs e)    
    2. {    
    3. Document pdfDoc = new Document(PageSize.A4, 25, 10, 25, 10);    
    4. PdfWriter pdfWriter = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);    
    5. pdfDoc.Open();    
    6. byte[] file;    
    7. file = System.IO.File.ReadAllBytes(Server.MapPath("~/Images/photo.jpg"));//ImagePath    
    8. iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(file);    
    9. jpg.ScaleToFit(550F, 200F);//Set width and height in float    
    10. pdfDoc.Add(jpg);    
    11. pdfWriter.CloseStream = false;    
    12. pdfDoc.Close();    
    13. Response.Buffer = true;    
    14. Response.ContentType = "application/pdf";    
    15. Response.AddHeader("content-disposition""attachment;filename=Image.pdf");    
    16. Response.Cache.SetCacheability(HttpCacheability.NoCache);    
    17. Response.Write(pdfDoc);    
    18. Response.End();    
    19. }


Note: You can provide any name of the generated file like here I have used "filename=Image.pdf".

Step 3
  • Run the page that will be like:


  • Click on the "Generate PDF" button and save the "Image.pdf" file.


Result: Open the PDF file and see the image in the PDF file.



Similar Articles