
Convert Html File To Pdf Using Itextsharp Css
How can i convert a html file with style into a pdf file. I have tested 2 issues (ITextSharp & Aspose) but i dont have excellent rendering. If someone know another tools i will be grateful. Thanks Try this plugin which enables you to HTML page with rich elements, such as HTTPS, CSS3, HTML5, JavaScript, to PDF in C#. Sample code is available here. Subtitle indonesia tokyo species indowebster movies. How to convert web page to pdf April 07, 2009. To simply and fast create pdf file use.rdlc file in VStudio. There is a WYSWIG that help you do it:) Reply Delete. I Use iTextSharp.dll for Convert ASPX page in PDF Formate That is Done Successfully But My CSS is Not working in PDF File.
I wanted to add a way to optionally render the PDF in landscape mode, so this was what I ultimately came up with.The ReturnValue class was simply a helper class that looks like this:
We also had another method to physically create the PDF file in case you didn't want just the bytes array directly, for example:
It's important to remember that in order for this to work, you must have valid well-formed HTML; otherwise you can certainly expect for iTextSharp to throw an error. But if you have control over the HTML that you need to convert, this solution is great, and produces very nice PDF files.
It's worth noting that in our case we didn't need to pass the CSS in separately using the overloaded ParseXHtml constructor, ParseXHtml(PdfWriter writer, Document doc, Stream inp, Stream inCssFile), because we were including our CSS styles in our HTML data string instead, which for our solution was a bit cleaner.
Matt Pavey is a Microsoft Certified software developer who specializes in ASP.Net, VB.Net, C#, AJAX, LINQ, XML, XSL, Web Services, SQL, jQuery, and more. Follow on Twitter @matthewpavey
Re: iTextSharp and converting a HTML formatted string to PDF
Oct 08, 2012 09:26 AMkaushikmahetaLINK
add Itextsharp.dll
.aspx page
<p>
<b>Order ID:</b>
<asp:TextBox runat='server' Columns='10'></asp:TextBox>
<asp:RequiredFieldValidator runat='server'
ControlToValidate='txtOrderID' ErrorMessage='[Required]' SetFocusOnError='True'></asp:RequiredFieldValidator>
</p>
<p>
<b>Total Price:</b>
$<asp:TextBox Columns='10' runat='server'></asp:TextBox>
<asp:RequiredFieldValidator runat='server'
ControlToValidate='txtTotalPrice' Display='Dynamic' ErrorMessage='[Required]'
SetFocusOnError='True'></asp:RequiredFieldValidator>
<asp:CompareValidator runat='server'
ControlToValidate='txtTotalPrice' Display='Dynamic' ErrorMessage='[Invalid]'
Operator='GreaterThan' SetFocusOnError='True' ValueToCompare='0'></asp:CompareValidator>
</p>
<p>
<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:CheckBoxList>
</p>
<p>
<asp:Button runat='server' Text='Create Receipt'
/>
</p>
No doubt she got an energetic spirit of delivering. Koffee toast download. The young female artiste proved to give his fans a piece of exclusive music. Your African Showbiz stationary Hub. Kindly Enjoy and Share to rate with a comment.Stream Rapture EP here.
.aspx.cs page
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
document.Open();
// 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);
document.Add(logo);
document.Close();
Response.ContentType = 'application/pdf';
Response.AddHeader('Content-Disposition', string.Format('attachment;filename=Receipt-{0}.pdf', txtOrderID.Text));
Response.BinaryWrite(output.ToArray());
}
}