Convert String Html To Pdf Itextsharp Download For Mac

Posted on  by admin

This article explains how to export content in a div to a PDF using iTextSharp. What is ITextSharp iTextSharp is a free and open source assembly that helps to convert page output or HTML content in A PDF file. #region convert HTML To PDF static public void HTMLToPdf(string Contents, string FilePath, string strValue) { Document document = new Document(PageSize.A4, 10f, 10f, 10f, 0f); //PDF for compliance overdue report.

  1. Html To Pdf Itextsharp

Oct 08, 2012  Hi All, I have a string that contains HTML and I am trying to write this out to a PDF document (as part of a larger document). I'm using iTextSharp and everything is working as expected but this single HTML string is outputting the actual HTML into the PDF which is not what I.

Re: iTextSharp and converting a HTML formatted string to PDF


Oct 08, 2012 09:26 AMkaushikmahetaLINK

add Itextsharp.dll

.aspx page

<b>Order ID:</b>
<asp:TextBox runat='server' Columns='10'></asp:TextBox>
<asp:RequiredFieldValidator runat='server'
ControlToValidate='txtOrderID' ErrorMessage='[Required]' SetFocusOnError='True'></asp:RequiredFieldValidator>
<b>Total Price:</b>
$<asp:TextBox Columns='10' runat='server'></asp:TextBox>
<asp:RequiredFieldValidator runat='server'
ControlToValidate='txtTotalPrice' Display='Dynamic' ErrorMessage='[Required]'
<asp:CompareValidator runat='server'
ControlToValidate='txtTotalPrice' Display='Dynamic' ErrorMessage='[Invalid]'
Operator='GreaterThan' SetFocusOnError='True' ValueToCompare='0'></asp:CompareValidator>
<b>What Items Were Purchased?</b>
<asp:CheckBoxList runat='server'>
<asp:ListItem Value='Widget 450FX 4'>Four widgets</asp:ListItem>
<asp:ListItem Value='Thingamajiggs 780JP 3'>Three Thingamajiggs</asp:ListItem>
<asp:ListItem Value='Whatchacallit 89001 1'>One Whatchacallit</asp:ListItem>
<asp:ListItem Value='Thingamabops A0902X 7'>Seven Thingamabops</asp:ListItem>
<asp:Button runat='server' Text='Create Receipt'

.aspx.cs page

Html To Pdf Itextsharp

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html.simpleparser;
public partial class ConvertHTMLtoPDF : System.Web.UI.Page
protected void btnCreatePDF_Click(object sender, EventArgs e)
// Create a Document object
var document = new Document(PageSize.A4, 50, 50, 25, 25);
// Create a new PdfWrite object, writing the output to a MemoryStream
var output = new MemoryStream();
var writer = PdfWriter.GetInstance(document, output);
// Open the Document for writing
// Read in the contents of the Receipt.htm HTML template file
string contents = File.ReadAllText(Server.MapPath('~/HTMLTemplate/Receipt.htm'));
// Replace the placeholders with the user-specified text
contents = contents.Replace('[ORDERID]', txtOrderID.Text);
contents = contents.Replace('[TOTALPRICE]', Convert.ToDecimal(txtTotalPrice.Text).ToString('c'));
contents = contents.Replace('[ORDERDATE]', DateTime.Now.ToShortDateString());
var itemsTable = @'<table><tr><thfont-weight: bold'>Item #</th><thfont-weight: bold'>Item Name</th><thfont-weight: bold'>Qty</th></tr>';
foreach (System.Web.UI.WebControls.ListItem item in cblItemsPurchased.Items)
if (item.Selected)
// Each CheckBoxList item has a value of ITEMNAME ITEM# QTY, so we split on and pull these values out...
var pieces = item.Value.Split(' '.ToCharArray());
itemsTable += string.Format('<tr><td>{0}</td><td>{1}</td><td>{2}</td></tr>',
pieces[1], pieces[0], pieces[2]);
itemsTable += '</table>';
contents = contents.Replace('[ITEMS]', itemsTable);
var parsedHtmlElements = HTMLWorker.ParseToList(new StringReader(contents), null);
foreach (var htmlElement in parsedHtmlElements)
document.Add(htmlElement as IElement);
// You can add additional elements to the document. Let's add an image in the upper right corner
var logo = iTextSharp.text.Image.GetInstance(Server.MapPath('~/Images/4guysfromrolla.gif'));
logo.SetAbsolutePosition(440, 800);
Response.ContentType = 'application/pdf';
Response.AddHeader('Content-Disposition', string.Format('attachment;filename=Receipt-{0}.pdf', txtOrderID.Text));