Introduction
This article explains how we can create a multi-page pdf using Ajax and HttpHandler using iTextSharp. A few days back, I had gone through the requirement where I had to create a user resume using iTextsharp. Hence, I will explain how to create multi-page pdf; i.e., according to the data of a user the pdf page will increase automatically. Before we start, we should know what is iTestsharp, chunk, phrase, paragraph and document; which are provided by Itextsharp.
This article explains how we can create a multi-page pdf using Ajax and HttpHandler using iTextSharp. A few days back, I had gone through the requirement where I had to create a user resume using iTextsharp. Hence, I will explain how to create multi-page pdf; i.e., according to the data of a user the pdf page will increase automatically. Before we start, we should know what is iTestsharp, chunk, phrase, paragraph and document; which are provided by Itextsharp.
What is iTextSharp?
iTextsharp is a PDF library which helps us to create and maintain our document in the portable document format (PDF). To use ItextSharp, you need to add the reference of itextsharp dll.
iTextsharp is a PDF library which helps us to create and maintain our document in the portable document format (PDF). To use ItextSharp, you need to add the reference of itextsharp dll.
Chunk
Chunk is the smallest text. If we want to show some text over the pdf, we can use Chunk. We can bind the data on the chunk; either static or dynamic from the database.
Chunk is the smallest text. If we want to show some text over the pdf, we can use Chunk. We can bind the data on the chunk; either static or dynamic from the database.
Example
Chunk chunk = new Chunk("Education:"); Here I bind Static Education field on the Chunk.
Chunk chunk1 = new Chunk(["Degree"].ToString()); here I bind user degree dynamically from the database.
Hence, chunk is nothing but <asp:label>, which is used to show text.
Phrase
Phrase is the collection of chunks or we can say phrase is the container where we can add multiple chunks and show on pdf.
Phrase is the collection of chunks or we can say phrase is the container where we can add multiple chunks and show on pdf.
Example,
- Phrase pExample = new Phrase();
- pExample.Add(chunk);
- pExample.Add(new Chunk(Environment.NewLine));
- pExample.Add(chunk1);
You can see above that I created one phrase “pExample” and added multiple chunks on that phrase. If we want a one line space between two chunks, we use Environment.NewLine.
Paragraph
Now, paragraph is the most important feature provided by iTextSharp, which holds the sequence of chunks and phrases.
- Paragraph para = new Paragraph();
- para.Add(pExample);
Document
Document is the sequence of the collection of chunks, phrases and paragraphs. We write the document on Pdf and data is shown accordingly.
Document is the sequence of the collection of chunks, phrases and paragraphs. We write the document on Pdf and data is shown accordingly.
- Document doc = new Document();
- doc.Add(para);
This article consists of three main steps,
- Add the reference of itextsharp.
- Using jQuery call to the Handler to creating the pdf.
- Set Fonts, style of pdf text using iTestsharp.
1. Add the reference of itextsharp
If we want to use the feature of iTextSharp, we have to add the reference. Hence, I explain how we can achieve that.
Step 1
Right click the reference and select add reference option, shown in the image below:

Step 2
Once you click the add reference option, select the dll from the location, where you saved the dll.
Once you click the add reference option, select the dll from the location, where you saved the dll.

Step 3
When you successfully add the iTextSharp reference, you can use the namespace which is required to create the pdf.
When you successfully add the iTextSharp reference, you can use the namespace which is required to create the pdf.
- using iTextSharp.text;
- using iTextSharp.text.pdf;
- using iTextSharp.text.pdf.draw;
2. Using jQuery call to the handler for creating the pdf.
When you are done with iTextSharp dll reference, create the HTML page and on the Click event of the button, call the handler. I explain these points in different steps.
Step 1
Create your HTML with the input type button and assign it a useful ID, because on the click of that button, we call the specific method in handler page.
Create your HTML with the input type button and assign it a useful ID, because on the click of that button, we call the specific method in handler page.
- <html>
- <head>
- <body>
- <div>
- <div>
- <input type="submit" id="btnDownloadResume" value="Download">
- </div>
- </div>
- </body>
- <script type="text/javascript">
- $("#btnDownloadResume").click(function()
- {
- window.location.href = "handlername?method=getpdf";
- })
- </script>
- </head>
- </html>


Narayan pandeyPosted Jun 13, 2016, 10:58 AM
Thank you.
RakeshPosted Jun 9, 2016, 11:57 AM
Good one
Vignesh ManiPosted Jun 9, 2016, 4:56 AM
Nice one
Debasis SahaPosted Jun 9, 2016, 1:33 AM
Nice share..
Nimit JoshiPosted Jun 9, 2016, 12:57 AM
Welcome to C# Corner Community. Nice Start Narayan. Looking forward to see some more nice articles from you. All the best. Cheers!!
Chandra ShekherPosted Jun 9, 2016, 12:36 AM
Good article
Atul RawatPosted Jun 9, 2016, 12:34 AM
nice article
Santhakumar MunuswamyPosted Jun 9, 2016, 12:05 AM
Thank you for good article
Bhavik PatelPosted Jun 8, 2016, 10:22 PM
nice
Sonu ChaudharyPosted Jun 8, 2016, 2:53 PM
good one ..