Content
I will explain how to design a runtime generated PDF via HTML code functionality using Visual Studio Ultimate 2015 Preview.
Sometimes, when you write code to generate a runtime PDF, you need a complex structure to be added to the PDF and you might think "Can't I just use the HTML code in it to create the tables or add some text or do some formatting?" Yes, you can, using the
HtmlWorker class.
Note: CSS aka style will not work.
In brief, I will generate the runtime PDF using
ITextSharp.
Note: I will use the "iTextSharp.dll" as a PDF generator library. You can download it from:
Links:
Sourceforge and
NuGet.
You can find it on the attached file in the "bin" folder of this article but the best way is to add the
ItextSharp as in Step B.
Step 1
Create a new website named "Website1".
Step 2
Add the ItextSharp library from the NuGet package using the following substeps:
- Click on Tools
- NuGet Package Manager
- Manage NuGet Packages for Solution
Then a popup window will open like:
Type the "ItextSharp" into the Search TextBox. Then you will get the "ItextSharp" library in the left pane. To add it to the website just click on the Install button of the right pane.
After the installation, you see the "Right" sign in the left pane that indicates that the library has been installed.
You can also check it from the Solution Explorer.
Step 3
- Add an HTML page named "MyPage.html' and draw a table with some rows and columns or create a design that you want to add to the runtime generated PDF.
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <title></title>
- </head>
- <body>
- <div align="center"><h1><b> This is Header</b></h1></div>
- <table border="1" width="100%" height="100%">
- <tr>
- <td>First Name: </td>
- <td>Rahul </td>
- </tr>
- <tr>
- <td>Last Name: </td>
- <td>Bansal </td>
- </tr>
- </table>
- </body>
- </html>
- The page will look like:
- Convert all the double quotes (") of the HTML page into apostrophes (single quotes, in other words ').
- <!DOCTYPE html>
- <html xmlns='http://www.w3.org/1999/xhtml'>
- <head>
- <title></title>
- </head>
- <body>
- <div align='center'><h1><b> This is Header</b></h1></div>
- <table border='1' width='100%' height='100%'>
- <tr>
- <td>First Name: </td>
- <td>Rahul </td>
- </tr>
- <tr>
- <td>Last Name: </td>
- <td>Bansal </td>
- </tr>
- </table>
- </body>
- </html>
Step 4
- Add a button to default page named "Default.aspx" and change the text to be "Generate PDF".
- 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;
- using iTextSharp.text.html.simpleparser;
- Write the code to generate the PDF file on the click event of the button and copy all the code of the HTML page into the "strHTML" variable.
- protected void Button1_Click(object sender, EventArgs e)
- {
- Document pdfDoc = new Document();
- PdfWriter pdfWriter = PdfWriter.GetInstance(pdfDoc, HttpContext.Current.Response.OutputStream);
- pdfDoc.Open();
- string strHTML = @"<!DOCTYPE html>
- <html xmlns='http://www.w3.org/1999/xhtml'>
- <head>
- <title></title>
- </head>
- <body>
- <div align = 'center'><h1><b>This is Header</b></h1></div>
- <table border = '1' width = '100%' height = '100%'>
- <tr>
- <td>First Name: </td>
- <td>Rahul </td>
- </tr>
- <tr>
- <td>Last Name: </td>
- <td>Bansal </td>
- </tr>
- </table>
- </body>
- </html>";
- HTMLWorker htmlWorker = new HTMLWorker(pdfDoc);
- htmlWorker.Parse(new StringReader(strHTML));
- pdfWriter.CloseStream = false;
- pdfDoc.Close();
- Response.Buffer = true;
- Response.ContentType = "application/pdf";
- Response.AddHeader("content-disposition", "attachment;filename=Test.pdf");
- Response.Cache.SetCacheability(HttpCacheability.NoCache);
- Response.Write(pdfDoc);
- Response.Flush();
- Response.End();
- }
As in the following image:
Note:
You can provide any name of the generated file. For example here I provided "test.pdf".
Step 5
- Run the page that will be like:
- Click on the "Generate PDF" button, it will pop-up a window like:
- Open this PDF and see the output.
In this article, we studied How to Design Runtime Generated PDF Via HTML.