Hi I have succesfully used a HTMLWorker to convert a gridview using asp.NET / C#.
(1) I have applied some limited style to the resulting table but cannot see how to apply tablestyle for instance grid lines or apply other formatting style such as a large column width for example for a particular column.
(2) I would actually like to put this text onto a pre-existing template which contains a logo etc. I've used PDF Stamper before for this but cannot see how I can use both PDFStamper and HTMLWorker at once. HTMLWorker needs a Document which implements iDocListener … but that doesnt seem compatible with usign a PDFStamper. I guess what I am looking for is a way to create a PDFStamper, write title etc, then add the parsed HTML from the grid. The other problem is that the parsed content doesnt interact with the other stuff on the page. For instance below I add a title chunk to the page. Rather than starting below it, the parsed HTML writes over the top. How do I place / interact the parsed HTML content with the rest of what is on the PDF document ?
Thanks in advance
Rob
Here';s the code I have already
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 30f, 0f);
HTMLWorker htmlWorker = new HTMLWorker(pdfDoc);
StyleSheet styles = new StyleSheet();
styles.LoadTagStyle("th", "size", "12px");
styles.LoadTagStyle("th", "face", "helvetica");
styles.LoadTagStyle("span", "size", "10px");
styles.LoadTagStyle("span", "face", "helvetica");
styles.LoadTagStyle("td", "size", "10px");
styles.LoadTagStyle("td", "face", "helvetica");
htmlWorker.SetStyleSheet(styles);
PdfWriter.GetInstance(pdfDoc, HttpContext.Current.Response.OutputStream);
pdfDoc.Open();
//Title - but this gets obsured by data, doesnt move it down
Font font = new Font(Font.FontFamily.HELVETICA, 14, Font.BOLD);
Chunk chunk = new Chunk(title, font);
pdfDoc.Add(chunk);
//Body
htmlWorker.Parse(sr);
Best Answer
Let me first give you a couple of links to look over when you get a chance:
These answers go deeper into what's going on and I recommend reading them when you get a chance. Specifically the second one will show you why you need to use
pt
instead ofpx
.To answer your first question let me show you a different way to use the
HTMLWorker
class. This class has a static method on it calledParseToList
that will convert HTML to aList<IElement>
. The objects in that list are all iTextSharp specific versions of your HTML. Normally you would do aforeach
on those and just add them to a document but you can modify them before adding which is what you want to do. Below is code that takes a static string and does that:Hopefully you can see that once you get access to the raw
PdfPTable
you can tweak it as necessary.To answer your second question, if you want to use the normal
Paragraph
andChunk
objects with aPdfStamper
then you need to use aPdfContentByte
object. You can get this from your stamper in one of two ways, either by asking for one that sits "above" existing content,stamper.GetOverContent(int)
or one that sits "below" existing content,stamper.GetUnderContent(int)
. Both versions take a single parameter saying what page to work with. Once you have aPdfContentByte
you can create aColumnText
object bound to it and use this object'sAddElement()
method to add your normal elements. Before doing this (and this answers your third question), you'll want to create at least one "column". When I do this I generally create one that essentially covers the entire page. (This part might sound weird but we're essentially make a single row, single column table cell to add our objects to.)Below is a full working C# 2010 WinForms app targeting iTextSharp 5.1.1.0 that shows off everything above. First it creates a generic PDF on the desktop. Then it creates a second document based off of the first, adds a paragraph and then some HTML. See the comments in the code for any questions.