Add Image Into Runtime Generated PDF From a Database

Content: I will explain how to add an image into a runtime generated PDF from a database functionality with the help of Visual Studio Ultimate 2015 Preview using Windows Form Application but you can also do this into the Website.

Briefly, I will do something like:

  1. Create a table for stored images in binary format on the basis of roll numbers.

  2. Get the image from the database on the basis of a specific roll number.

  3. Generate the runtime PDF using ITextSharp.

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

    You can find it in the attached file in the "bin\Debug" folder of this article.

  4. Add the image into the PDF.

To learn more about Point 1, Click here.

I am assuming I have a table named "TestImage" that stores the data like:

result

Now I will start the article from Point 2.

Step 1

Create a Windows Forms application named "WindowsFormsApplication1".

WindowsFormsApplication1

Right-click on "References" in the Solution Explorer and click on "Add Reference".

References

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.

Add button

Finally it will look like:

iTextSharp

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

Solution Explorer

Step 2

  1. Add the following 2 namespaces to the top of the ".cs" file:
    1. using System.Data.SqlClient; 
  2. Create a method in ".cs" file that will return the image in the format of a byte array.
    1. private byte[] GetImage(string Roll_no)  
    2. {  
    3.     string sConn = ConfigurationManager.ConnectionStrings["con"].ToString();  
    4.     SqlConnection objConn = new SqlConnection(sConn);  
    5.     objConn.Open();  
    6.     string sTSQL = "select img from TestImage where Roll_no='" + Roll_no + "'";  
    7.     SqlCommand objCmd = new SqlCommand(sTSQL, objConn);  
    8.     objCmd.CommandType = CommandType.Text;  
    9.     object result = objCmd.ExecuteScalar();  
    10.     objConn.Close();  
    11.     return (byte[])result;  

output

Note:
  1. For "ConfigurationManager"  you need to add the reference of System.Configuration.

    ConfigurationManager

  2. For "Connection String"  you need to add the app.config file and add the connection string into it.

    Connection String
    1. <connectionStrings>  
    2.    <add name="con" providerName="System.Data.sqlclient"  
    3. connectionString="data source=;database=;user id=;password=;" /> </connectionStrings> 
    Form1

Step 3

  1. Add a button in default form named "Form1" and make some changes like set the text to "Generate PDF" and increase the width and height of the button.

    Generate PDF

  2. Double-click on the button to generate an Onclick event (to generate the PDF) on the Form.

  3. Add the following 2 namespaces to the top of the ".cs" file:
    1. using iTextSharp.text;  
    2. using iTextSharp.text.pdf;  
    3. using System.IO;
  4. Write the code to call the method of the image for getting and generating the PDF file on the click event of the button:
    1. private void button1_Click(object sender, EventArgs e)  
    2. {  
    3.     try  
    4.     {  
    5.         Document document = new Document();  
    6.         PdfWriter.GetInstance(document, new FileStream("E:/Rahul/A.pdf", FileMode.Create));  
    7.         document.Open();  
    8.         iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(GetImage("2000001"));  
    9.         img.ScaleToFit(105F, 120F);  
    10.         document.Add(img);  
    11.         document.Close();  
    12.     }  
    13.     catch (Exception ex)  
    14.     {  
    15.   
    16.     }  

image getting

Note:

  1. You can provide any name for the generated file and any text that you want to print in the PDF file. For example here I provided "A.pdf" and the text as "This is test PDF" and path also.

  2. You can also change the height and width of the image using the "ScaleToFit()" method.

Step 4:

  1. Run the page that will be like:

    Run the page

  2. Click on the "Generate PDF" button.

Result

  1. Follow the specified path and open the folder, you will see the PDF file.

    Folder

  2. Open the PDF file and see your image in the PDF file.

    image in the PDF file