ITextSharp is a free library to create PDF documents using C#.net. HTML to PDF using iTextSharp Library In ASP.NET. Using i. Text. Sharp to generate pdf file in asp. Introduction. One of the common requirement of web applications is to provide users a way to download some contents. In case of a report, almost all of the reporting tools have the export to pdf kind of function available. Using a template to programmatically create PDFs with C# and iTextSharp. C# 24; Code 21; iTextSharp 1. IText is a PDF library that allows you to CREATE. 1 of 5 2 of 5 3 of 5 4 of 5 5 of 5. But sometime, it is not a report that needs to be generated in pdf. But it could be simple web page or content of a page or sometimes a plain text needs to be generated in pdf format. There are many third party tools are available for this task. We can import their API's in Asp. Most obvious because i wanted it for free.
I am just trying to include my learning here in the series of i. Text. Sharp tutorials. Article Body. I intend to cover other features of i. Text. Sharp component. Focus of this article would be on simply creating a pdf file with different sources of text. To start with, download i. Text. Sharp dll from official page here i. Tex. Sharp is a c# version of original Java library i. Text. To demonstrate the use, i have created a sample web application in c# and added reference to i. Text. Sharp. dll. I wanted to test using some other site's html page as a source to convert into pdf also, i would user entered string to convert into pdf. After creating couple of textboxes for input, I want to write code on button click event. Before that, lets add few references in our code. System. IO. using i. Text. Sharp. using i. Text. Sharp. text. Text. Sharp. text. Text. Sharp. text. Text. Sharp. text. In button click event, create object of Document and PDFWriter. Document itext. Doc = new Document(). Pdf. Writer pdf. Doc = Pdf. Writer. Get. Instance(itext. Doc,Response. Output. Stream); At the end generated PDF file is going to be outputted to outputstream. The following code is just for reading other web page using Web. Client and converting response into string reader (or reading text file into stringreader). You may want ignore this code if you are aware about how to do it. System. Net. Web. Client web. Client=new System. Net. Web. Client(). Stream response. Data = web. Client. Open. Read(. The parsed elements will be returned into List of IElement. We will then need to loop through all elements and add them individually using Document object we just created. Below lines of code will do just that. List< IElement> elements =HTMLWorker. Parse. To. List(sr,null). Doc. Open(). //htmlwrite. Parse(sr). foreach (IElement el in elements). I already have another simple asp. Visual studio instance. When viewed in browser, the page looks like this. From my sample application when I call this page using Web. Client and generate the PDF file, it opens up as. Our basic task is completed. You might want to add little more stuff to generated PDF. Like adding header/footer, images, watermark etc. Most of these functionality available in IText. Sharp. We will look at them in following articles. Please find complete code for this test below. I would like to hear comments/crtics : )System. Net. Web. Client web. Client=new System. Net. Web. Client(). Stream response. Data = web. Client. Open. Read(. If you prefer VB. Net, Please use online VB. Net < -> C# converter here.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
December 2016
Categories |