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:
- Create a table for stored images in binary format on the basis of roll numbers.
- Get the image from the database on the basis of a specific roll number.
- 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.
- 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:
Now I will start the article from Point 2.
Step 1
Create a Windows Forms application named "WindowsFormsApplication1".
Right-click on "References" in the Solution Explorer and click on "Add Reference".
Use the following sub-steps in the "Reference Manager".
- Click on the Browse tab on the left side.
- Click on the Browse button at the bottom.
- Select the "iTextSharp.dll" from the system.
- 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.
Step 2
- Add the following 2 namespaces to the top of the ".cs" file:
- using System.Data.SqlClient;
- Create a method in ".cs" file that will return the image in the format of a byte array.
- private byte[] GetImage(string Roll_no)
- {
- string sConn = ConfigurationManager.ConnectionStrings["con"].ToString();
- SqlConnection objConn = new SqlConnection(sConn);
- objConn.Open();
- string sTSQL = "select img from TestImage where Roll_no='" + Roll_no + "'";
- SqlCommand objCmd = new SqlCommand(sTSQL, objConn);
- objCmd.CommandType = CommandType.Text;
- object result = objCmd.ExecuteScalar();
- objConn.Close();
- return (byte[])result;
- }
Note:
- For "ConfigurationManager" you need to add the reference of System.Configuration.
- For "Connection String" you need to add the app.config file and add the connection string into it.
- <connectionStrings>
- <add name="con" providerName="System.Data.sqlclient"
- connectionString="data source=;database=;user id=;password=;" /> </connectionStrings>
Step 3
- 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.
- Double-click on the button to generate an Onclick event (to generate the PDF) on the Form.
- Add the following 2 namespaces to the top of the ".cs" file:
- using iTextSharp.text;
- using iTextSharp.text.pdf;
- using System.IO;
- Write the code to call the method of the image for getting and generating the PDF file on the click event of the button:
- private void button1_Click(object sender, EventArgs e)
- {
- try
- {
- Document document = new Document();
- PdfWriter.GetInstance(document, new FileStream("E:/Rahul/A.pdf", FileMode.Create));
- document.Open();
- iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(GetImage("2000001"));
- img.ScaleToFit(105F, 120F);
- document.Add(img);
- document.Close();
- }
- catch (Exception ex)
- {
-
- }
- }
Note:
- 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.
- You can also change the height and width of the image using the "ScaleToFit()" method.
Step 4:
- Run the page that will be like:
- Click on the "Generate PDF" button.
Result
- Follow the specified path and open the folder, you will see the PDF file.
- Open the PDF file and see your image in the PDF file.