Related Tags:
Asp.Net It was developed by Microsoft to allow programmers to build dynamic web sites, web applications and web services. It was first released in January 2002 with version 1.0 of the .NET Framework, and is the successor to Microsoft's Active Server Pages (ASP) technology. Learn More, C# C# (pronounced "C-sharp") is an object-oriented programming language from Microsoft that aims to combine the computing power of C++ with the programming ease of Visual Basic. C# is based on C++ and contains features similar to those of Java. Learn More, ASP.NET MVC The ASP.NET MVC framework is a lightweight, highly testable presentation framework that (as with Web Forms-based applications) is integrated with existing ASP.NET features, such as master pages and membership-based authentication. The MVC framework is defined in the System.Web.Mvc assembly. Learn More, iTextSharp iTextSharp is a free and open source assembly that helps to convert page output or HTML content in a PDF file. Learn More, pdf Portable Document Format (PDF) is an open standard for electronic document exchange maintained by the International Organization for Standardization (ISO). Learn More,

how to dynamically generate pdf file from html in C# using ItextSharp ?

Description:In my project there is a situation where I need to convert my html into pdf file and some one suggested me that there is a library in C# for the purpose called Itextsharp but unable to implement it properly, need a code chunk or method to implement such functionality.

Posted by: | Posted on: Sep 07, 2018

1 answers



I am considering that your html contains inline styling which is most suitable for such dynamic pdf generation then you can easily use the following method for the purpose of converting your HTML into PDF, the method will be returning MemoryStream which can be easily downloaded as file.

public MemoryStream GeneratePdf(string html)
var pdfDoc = new Document(PageSize.A3);
var memoryStream = new MemoryStream();
PdfWriter pdfWriter = PdfWriter.GetInstance(pdfDoc, memoryStream);

pdfWriter.RgbTransparencyBlending = true;

var cssResolver = new StyleAttrCSSResolver();
XMLWorkerFontProvider fontProvider =
new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS);
//Following line will be required in case of lanuguages other than english i.e I have added following for arabic font.
//fontProvider.Register(HttpContext.Server.MapPath("~/Tahoma Regular font.ttf"));
CssAppliers cssAppliers = new CssAppliersImpl(fontProvider);
HtmlPipelineContext htmlContext = new HtmlPipelineContext(cssAppliers);
// Pipelines
PdfWriterPipeline pdf = new PdfWriterPipeline(pdfDoc, pdfWriter);
HtmlPipeline html1 = new HtmlPipeline(htmlContext, pdf);
CssResolverPipeline css = new CssResolverPipeline(cssResolver, html1);
// XML Worker
XMLWorker worker = new XMLWorker(css, true);
XMLParser p = new XMLParser(worker);
p.Parse(new StringReader(html));
pdfWriter.CloseStream = false;
memoryStream.Position = 0;
return memoryStream;

You can simply call the above method by using the following code for example:

MemoryStream memoryStream = GeneratePdf("<div style='font-size:30px;color:#ff0000;'>Document Title</div><br/><p>document description</p>");

Replied by: | Replied on: Sep 10, 2018


Code block Hyperlink bold Quotes block Upload Images