<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>hamang.net &#187; Pdf</title>
	<atom:link href="http://hamang.net/tag/pdf/feed/" rel="self" type="application/rss+xml" />
	<link>http://hamang.net</link>
	<description>- the everyday life</description>
	<lastBuildDate>Wed, 10 Feb 2010 09:52:49 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Html to Pdf in .NET</title>
		<link>http://hamang.net/2008/08/14/html-to-pdf-in-net/</link>
		<comments>http://hamang.net/2008/08/14/html-to-pdf-in-net/#comments</comments>
		<pubDate>Thu, 14 Aug 2008 14:47:41 +0000</pubDate>
		<dc:creator>Knut Hamang</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Pdf]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[I searched around for some OpenSource projects converting HTML to PDF, and stumbled upon a great library called iTextSharp . It&#039;s actually a Java library ported to .NET (typically isn&#039;t it?), and it has some really nice features. There are some good commercial products out there doing the same, but in my opinion this is [...]]]></description>
			<content:encoded><![CDATA[<p><a href="/wp-content/uploads/2008/09/reader_icon_special.jpg"><img class="size-medium wp-image-31 alignleft" title="PDF" src="/wp-content/uploads/2008/09/reader_icon_special.jpg" alt="" width="125" height="104" /></a>I searched around for some OpenSource projects <strong>converting HTML to PDF</strong>, and stumbled upon a great library called <a href="http://sourceforge.net/projects/itextsharp/?abmode=1" target="_blank">iTextSharp</a> . It&#039;s actually a Java library ported to .NET (typically isn&#039;t it?), and it has some really nice features. There are some good commercial products out there doing the same, but in my opinion this is core functionality, so if one can get it for free and even get the source, nothing is better than that!</p>
<p>Regarding PDF creation, iTextSharps main function is <strong>generating PDF from scratch</strong>, not converting them from HTML. The library has a really understandable API for developers that are not familiar with the PDF specification, me beeing one of them.</p>
<p>Playing around with the API and reading some news lists and forum posts, I finally managed to get a working sample on how to export a GridView to PDF. This is a pretty simple sample, but you can play around with the API to add more formatting and do your stuff. Actually <strong>you can export anything in the XHTML</strong>, providing the markup is legal. However, <strong>not all html tags are supported</strong>. The intention of the author was not to make a HTML2PDF converter, but more like create PDF&#039;s from HTML if the markup supports the engine. So you will probably not be able to convert dynamic content you do not have control over, but it&#039;s excellent for creating reports etc. Supported tags are: &#034;ol ul li a pre font span br p div body table td th tr i b u sub sup em strong s strike h1 h2 h3 h4 h5 h6 img&#034;</p>
<p>So how does it work, then? Well, first you need to get the latest version of iTextSharp. Just place the dll to your bin folder in your Web Application Project (you&#039;re not using Web Projects, are you  <img src='http://hamang.net/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' />  ) and add a reference to it. Then create a new page, add a <strong>PlaceHolder</strong> to it, The placeholder will be the section of the HTML that you will export to PDF. You can add some more controls to the placeholder if you need. Inside the placeholder add a GridView and bind it to your datasource.</p>
<p>Add a ASP:Button to the page. This will trigger the export. The code when the button is clicked is doing all the exporting stuff:</p>
<p><!-- {\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;\red43\green145\blue175;\red0\green128\blue0;\red163\green21\blue21;}??\fs20 \cf1 protected\cf0  \cf1 void\cf0  ButtonCreatePdf_Click(\cf1 object\cf0  sender, \cf4 EventArgs\cf0  e)\par ??        \{\par ??            \cf5 //Set content type in response stream\par ??\cf0             Response.ContentType = \cf6 "application/pdf"\cf0 ;\par ??            Response.AddHeader(\cf6 "content-disposition"\cf0 , \cf6 "attachment;filename=FileName.pdf"\cf0 );\par ??            Response.Cache.SetCacheability(\cf4 HttpCacheability\cf0 .NoCache);\par ??\par ??            \cf5 //Render PlaceHolder to temporary stream \par ??\cf0             System.IO.\cf4 StringWriter\cf0  stringWrite = \cf1 new\cf0  \cf4 StringWriter\cf0 ();\par ??            System.Web.UI.\cf4 HtmlTextWriter\cf0  htmlWrite = \cf1 new\cf0  \cf4 HtmlTextWriter\cf0 (stringWrite);\par ??            PlaceholderPdf.RenderControl(htmlWrite);\par ??            \cf4 StringReader\cf0  reader = \cf1 new\cf0  \cf4 StringReader\cf0 (stringWrite.ToString());\par ??\par ??            \cf5 //Create PDF document \par ??\cf0             \cf4 Document\cf0  doc = \cf1 new\cf0  \cf4 Document\cf0 (\cf4 PageSize\cf0 .A4);\par ??            \cf4 HTMLWorker\cf0  parser = \cf1 new\cf0  \cf4 HTMLWorker\cf0 (doc);\par ??            \cf4 PdfWriter\cf0 .GetInstance(doc, Response.OutputStream);\par ??            doc.Open();\par ??            \par ??            \cf1 try\par ??\cf0             \{\par ??                \cf5 //Create a footer that will display page number\par ??\cf0                 \cf4 HeaderFooter\cf0  footer = \cf1 new\cf0  \cf4 HeaderFooter\cf0 (\cf1 new\cf0  \cf4 Phrase\cf0 (\cf6 "This is page: "\cf0 ), \cf1 true\cf0 ) \par ??                                           \{ Border = \cf4 Rectangle\cf0 .NO_BORDER \};\par ??                doc.Footer = footer;\par ??                \par ??                \cf5 //Parse Html\par ??\cf0                 parser.Parse(reader);\par ??            \}\par ??            \cf1 catch\cf0  (\cf4 Exception\cf0  ex)\par ??            \{\par ??                \cf5 //Display parser errors in PDF. \par ??\cf0                 \cf5 //Parser errors will also be wisible in Debug.Output window in VS\par ??\cf0                 \cf4 Paragraph\cf0  paragraph = \cf1 new\cf0  \cf4 Paragraph\cf0 (\cf6 "Error! "\cf0  + ex.Message);\par ??                paragraph.SetAlignment(\cf6 "center"\cf0 );\par ??                \cf4 Chunk\cf0  text = paragraph.Chunks[0] \cf1 as\cf0  \cf4 Chunk\cf0 ;\par ??                \cf1 if\cf0  (text != \cf1 null\cf0 )\par ??                \{\par ??                    text.Font.Color = \cf4 Color\cf0 .RED;\par ??                \}\par ??                doc.Add(paragraph);\par ??            \}\par ??            \cf1 finally\par ??\cf0             \{\par ??                doc.Close();\par ??            \}\par ??        \}} --></p>
<div style="font-size: 10pt; background: white; color: black; font-family: Courier New; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial">
<p style="margin: 0px"><span style="color: #0000ff;"> protected</span> <span style="color: #0000ff;">void</span> ButtonCreatePdf_Click(<span style="color: #0000ff;">object</span> sender, <span style="color: #2b91af;">EventArgs</span> e)</p>
<p style="margin: 0px">{</p>
<p style="margin: 0px; padding-left: 30px;"><span style="color: #008000;">//Set content type in response stream</span></p>
<p style="margin: 0px; padding-left: 30px;">Response.ContentType = <span style="color: #a31515;">&#034;application/pdf&#034;</span>;</p>
<p style="margin: 0px; padding-left: 30px;">Response.AddHeader(<span style="color: #a31515;">&#034;content-disposition&#034;</span>, <span style="color: #a31515;">&#034;attachment;filename=FileName.pdf&#034;</span>);</p>
<p style="margin: 0px; padding-left: 30px;">Response.Cache.SetCacheability(<span style="color: #2b91af;">HttpCacheability</span>.NoCache);</p>
<p style="margin: 0px; padding-left: 30px;">
<p style="margin: 0px; padding-left: 30px;"><span style="color: #008000;">//Render PlaceHolder to temporary stream </span></p>
<p style="margin: 0px; padding-left: 30px;">System.IO.<span style="color: #2b91af;">StringWriter</span> stringWrite = <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">StringWriter</span>();</p>
<p style="margin: 0px; padding-left: 30px;">System.Web.UI.<span style="color: #2b91af;">HtmlTextWriter</span> htmlWrite = <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">HtmlTextWriter</span>(stringWrite);</p>
<p style="margin: 0px; padding-left: 30px;">PlaceholderPdf.RenderControl(htmlWrite);</p>
<p style="margin: 0px; padding-left: 30px;"><span style="color: #2b91af;">StringReader</span> reader = <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">StringReader</span>(stringWrite.ToString());</p>
<p style="margin: 0px; padding-left: 30px;">
<p style="margin: 0px; padding-left: 30px;"><span style="color: #008000;">//Create PDF document </span></p>
<p style="margin: 0px; padding-left: 30px;"><span style="color: #2b91af;">Document</span> doc = <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">Document</span>(<span style="color: #2b91af;">PageSize</span>.A4);</p>
<p style="margin: 0px; padding-left: 30px;"><span style="color: #2b91af;">HTMLWorker</span> parser = <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">HTMLWorker</span>(doc);</p>
<p style="margin: 0px; padding-left: 30px;"><span style="color: #2b91af;">PdfWriter</span>.GetInstance(doc, Response.OutputStream);</p>
<p style="margin: 0px; padding-left: 30px;">doc.Open();</p>
<p style="margin: 0px; padding-left: 30px;">
<p style="margin: 0px; padding-left: 30px;"><span style="color: #0000ff;">try</span></p>
<p style="margin: 0px; padding-left: 30px;">{</p>
<p style="margin: 0px; padding-left: 60px;"><span style="color: #008000;">//Create a footer that will display page number</span></p>
<p style="margin: 0px; padding-left: 60px;"><span style="color: #2b91af;">HeaderFooter</span> footer = <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">HeaderFooter</span>(<span style="color: #0000ff;">new</span> <span style="color: #2b91af;">Phrase</span>(<span style="color: #a31515;">&#034;This is page: &#034;</span>), <span style="color: #0000ff;">true</span>)</p>
<p style="margin: 0px; padding-left: 60px;">{ Border = <span style="color: #2b91af;">Rectangle</span>.NO_BORDER };</p>
<p style="margin: 0px; padding-left: 60px;">doc.Footer = footer;</p>
<p style="margin: 0px; padding-left: 60px;">
<p style="margin: 0px; padding-left: 60px;"><span style="color: #008000;">//Parse Html</span></p>
<p style="margin: 0px; padding-left: 60px;">parser.Parse(reader);</p>
<p style="margin: 0px; padding-left: 30px;">}</p>
<p style="margin: 0px; padding-left: 30px;"><span style="color: #0000ff;">catch</span> (<span style="color: #2b91af;">Exception</span> ex)</p>
<p style="margin: 0px; padding-left: 30px;">{</p>
<p style="margin: 0px; padding-left: 60px;"><span style="color: #008000;">//Display parser errors in PDF. </span></p>
<p style="margin: 0px; padding-left: 60px;"><span style="color: #008000;">//Parser errors will also be wisible in Debug.Output window in VS</span></p>
<p style="margin: 0px; padding-left: 60px;"><span style="color: #2b91af;">Paragraph</span> paragraph = <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">Paragraph</span>(<span style="color: #a31515;">&#034;Error! &#034;</span> + ex.Message);</p>
<p style="margin: 0px; padding-left: 60px;">paragraph.SetAlignment(<span style="color: #a31515;">&#034;center&#034;</span>);</p>
<p style="margin: 0px; padding-left: 60px;"><span style="color: #2b91af;">Chunk</span> text = paragraph.Chunks[0] <span style="color: #0000ff;">as</span> <span style="color: #2b91af;">Chunk</span>;</p>
<p style="margin: 0px; padding-left: 60px;"><span style="color: #0000ff;">if</span> (text != <span style="color: #0000ff;">null</span>)</p>
<p style="margin: 0px; padding-left: 60px;">{</p>
<p style="margin: 0px; padding-left: 90px;">text.Font.Color = <span style="color: #2b91af;">Color</span>.RED;</p>
<p style="margin: 0px; padding-left: 60px;">}</p>
<p style="margin: 0px; padding-left: 60px;">doc.Add(paragraph);</p>
<p style="margin: 0px; padding-left: 30px;">}</p>
<p style="margin: 0px; padding-left: 30px;"><span style="color: #0000ff;">finally</span></p>
<p style="margin: 0px; padding-left: 30px;">{</p>
<p style="margin: 0px; padding-left: 60px;">doc.Close();</p>
<p style="margin: 0px; padding-left: 30px;">}</p>
<p style="margin: 0px">}</p>
<p style="margin: 0px">
</div>
<p>Almost there. Clicking the button will result in an exception:</p>
<p class="error">Control &#039;GridView1&#039; of type &#039;GridView&#039; must be placed inside a form tag with runat=server.</p>
<p>This is because we are trying to render the PlaceHolder control in a stream and not in a WebForm. A neat .NET security feature to prevent Injection attacks. This exception is simply ignored by adding the following code to the page:</p>
<div style="font-family: Courier New; font-size: 10pt; color: black; background: white;">
<p style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">override</span> <span style="color: blue;">void</span> VerifyRenderingInServerForm(<span style="color: #2b91af;">Control</span> control)</p>
<p style="margin: 0px;">{</p>
<p style="margin: 0px;">
<p style="margin: 0px;">}</p>
</div>
<p>Now, clicking the button again results in another Exception:</p>
<p class="error">RegisterForEventValidation can only be called during Render();</p>
<p>Again, a security feature of .NET. Ignore this excpetion by setting EnableEventValidation=&#034;False&#034; in the Page header. <strong>If you are concerned about the security</strong> of the page, please check official documentation of the features that has been disabled for the page.</p>
<p>Now, clicking the button should result in a PDF document. If not, debug the application and check <strong>Debut Output</strong> window for exceptions. Probably the XHTML in the placeholder is not valid.</p>
<p>Note that I am using <strong>HTMLWorker </strong>class instead of the HtmlParser class in the iTextSharp library. According the the author, the HTMLParser is not supported. I tried both, and the HTMLWorker swallows a lot more HTML markup than the HtmlParser.</p>
<p>You probably also want to <strong>clean the HTML</strong> before parsing it with the HTMLWorker. Typically you want to remove javascript postbacks, anchors etc. from the GridView. This can be achieved with the following code:</p>
<p><!--<br />
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red0\green0\blue255;\red43\green145\blue175;\red163\green21\blue21;}??\fs20             \cf3 string\cf0  html = stringWrite.ToString();\par ??            html = \cf4 Regex\cf0 .Replace(html, \cf5 "&lt;/?(a|A).*?&gt;"\cf0 , \cf5 ""\cf0 );\par ??            \cf4 StringReader\cf0  reader = \cf3 new\cf0  \cf4 StringReader\cf0 (html);}<br />
--></p>
<div style="font-size: 10pt; background: white; color: black; font-family: Courier New; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial">
<p style="margin: 0px"><span style="color: #0000ff;">string</span> html = stringWrite.ToString();</p>
<p style="margin: 0px">html = <span style="color: #2b91af;">Regex</span>.Replace(html, <span style="color: #a31515;">&#034;&lt;/?(a|A).*?&gt;&#034;</span>, <span style="color: #a31515;">&#034;&#034;</span>);</p>
<p style="margin: 0px"><span style="color: #2b91af;">StringReader</span> reader = <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">StringReader</span>(html);</p>
</div>
<p>You can also <strong>extend the HTMLWorker class </strong>making it more specialized for your purpose. For instance it would be great to be able to define pagebreaks in the final PDF document. Simply create a new class inherited from HTMLWorker.</p>
<p><!--<br />
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;\red43\green145\blue175;\red163\green21\blue21;}??\fs20 \cf1 public\cf0  \cf1 class\cf0  \cf4 HTMLWorkerExtended\cf0  : \cf4 HTMLWorker\par ??\cf0     \{\par ??        \cf1 public\cf0  HTMLWorkerExtended(\cf4 IDocListener\cf0  document) : \cf1 base\cf0 (document)\par ??        \{\}\par ??\par ??        \cf1 public\cf0  \cf1 override\cf0  \cf1 void\cf0  StartElement(\cf4 String\cf0  tag, \cf4 Hashtable\cf0  h)\par ??        \{\par ??            \cf1 if\cf0  (tag.Equals(\cf5 "newpage"\cf0 ))\par ??                document.Add(\cf4 Chunk\cf0 .NEXTPAGE);\par ??            \cf1 else\par ??\cf0                 \cf1 base\cf0 .StartElement(tag, h);\par ??        \}\par ??    \}}<br />
--></p>
<div style="font-size: 10pt; background: white; color: black; font-family: Courier New; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial">
<p style="margin: 0px"><span style="color: #0000ff;"> public</span> <span style="color: #0000ff;">class</span> <span style="color: #2b91af;">HTMLWorkerExtended</span> : <span style="color: #2b91af;">HTMLWorker</span></p>
<p style="margin: 0px">{</p>
<p style="margin: 0px; padding-left: 30px;"><span style="color: #0000ff;">public</span> HTMLWorkerExtended(<span style="color: #2b91af;">IDocListener</span> document) : <span style="color: #0000ff;">base</span>(document)</p>
<p style="margin: 0px; padding-left: 30px;">{}</p>
<p style="margin: 0px; padding-left: 30px;">
<p style="margin: 0px; padding-left: 30px;"><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">override</span> <span style="color: #0000ff;">void</span> StartElement(<span style="color: #2b91af;">String</span> tag, <span style="color: #2b91af;">Hashtable</span> h)</p>
<p style="margin: 0px; padding-left: 30px;">{</p>
<p style="margin: 0px; padding-left: 60px;"><span style="color: #0000ff;">if</span> (tag.Equals(<span style="color: #a31515;">&#034;newpage&#034;</span>))</p>
<p style="margin: 0px; padding-left: 90px;">document.Add(<span style="color: #2b91af;">Chunk</span>.NEXTPAGE);</p>
<p style="margin: 0px; padding-left: 60px;"><span style="color: #0000ff;">else</span></p>
<p style="margin: 0px; padding-left: 90px;"><span style="color: #0000ff;">base</span>.StartElement(tag, h);</p>
<p style="margin: 0px; padding-left: 30px;">}</p>
<p style="margin: 0px">}</p>
</div>
<p>Now, simply replace the HTMLWorker with the extended version and add a <strong>&lt;newpage /&gt;</strong> element to the HTML in the placeholder where you want a new page.</p>
<p><strong>There are some css styles not supported by default</strong>. For instance it is not possible to set the background-color style for an image or tablecell/-row. The only solution for adding more style support is changing the iTextSharp source. It&#039;s pretty simple, however. Open <strong>\text\html\simpleparser\FactoryProperties.cs</strong>. In the <strong>InsertStyle</strong> method add the following code to the foreach loop:</p>
<p><!--<br />
{\rtf1\ansi\ansicpg\lang1024\noproof1252\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;\red43\green145\blue175;\red163\green21\blue21;}??\fs20 \cf1 else\cf0  \cf1 if\cf0  (key.Equals(\cf4 Markup\cf0 .CSS_KEY_BGCOLOR))\par ??                \{\par ??                    \cf4 Color\cf0  c = \cf4 Markup\cf0 .DecodeColor(prop[key]);\par ??                    \cf1 if\cf0  (c != \cf1 null\cf0 )\par ??                    \{\par ??                        \cf1 int\cf0  hh = c.ToArgb() &amp; 0xffffff;\par ??                        \cf4 String\cf0  hs = \cf5 "#"\cf0  + hh.ToString(\cf5 "X06"\cf0 , \cf4 NumberFormatInfo\cf0 .InvariantInfo);\par ??                        h[\cf5 "bgcolor"\cf0 ] = hs;\par ??                    \}\par ??                \}  }<br />
--></p>
<div style="font-size: 10pt; background: white; color: black; font-family: Courier New">
<p style="margin: 0px"><span style="color: #0000ff;"> else</span> <span style="color: #0000ff;">if</span> (key.Equals(<span style="color: #2b91af;">Markup</span>.CSS_KEY_BGCOLOR))</p>
<p style="margin: 0px"><span style="color: #0000ff;"> </span>{</p>
<p style="margin: 0px; padding-left: 30px;"><span style="color: #2b91af;"><span style="color: #000000;"> </span><span style="color: #0000ff;"> </span>Color</span> c = <span style="color: #2b91af;">Markup</span>.DecodeColor(prop[key]);</p>
<p style="margin: 0px; padding-left: 30px;"><span style="color: #0000ff;"> if</span> (c != <span style="color: #0000ff;">null</span>)</p>
<p style="margin: 0px; padding-left: 30px;"><span style="color: #0000ff;"> </span>{</p>
<p style="margin: 0px; padding-left: 60px;"><span style="color: #0000ff;"> int</span> hh = c.ToArgb() &amp; 0xffffff;</p>
<p style="margin: 0px; padding-left: 60px;"><span style="color: #0000ff;"> </span><span style="color: #2b91af;">String</span> hs = <span style="color: #a31515;">&#034;#&#034;</span> + hh.ToString(<span style="color: #a31515;">&#034;X06&#034;</span>, <span style="color: #2b91af;">NumberFormatInfo</span>.InvariantInfo);</p>
<p style="margin: 0px; padding-left: 60px;"><span style="color: #0000ff;"> </span>h[<span style="color: #a31515;">"bgcolor"</span>] = hs;</p>
<p style="margin: 0px; padding-left: 30px;"><span style="color: #0000ff;"> </span>}</p>
<p style="margin: 0px"><span style="color: #0000ff;"> </span>}</p>
</div>
<p><strong>Update: </strong>Another example for adding border color to a table:</p>
<p>First, set the border style for the table, ie. style=&#034;border-color: #ff0000;&#034;</p>
<p>Then again, you need to apply the style to <strong>FactoryProperties.cs</strong> file as in the example above.</p>
<p><!-- {\rtf1\ansi\ansicpg\lang1024\noproof1252\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red0\green0\blue255;\red43\green145\blue175;\red163\green21\blue21;}??\fs20                 \cf3 else\cf0  \cf3 if\cf0  (key.Equals(\cf4 Markup\cf0 .CSS_KEY_BORDERCOLOR))\par ??                \{\par ??                    \cf4 Color\cf0  c = \cf4 Markup\cf0 .DecodeColor(prop[key]);\par ??                    \cf3 if\cf0  (c != \cf3 null\cf0 )\par ??                    \{\par ??                        \cf3 int\cf0  hh = c.ToArgb() &amp; 0xffffff;\par ??                        \cf4 String\cf0  hs = \cf5 "#"\cf0  + hh.ToString(\cf5 "X06"\cf0 , \cf4 NumberFormatInfo\cf0 .InvariantInfo);\par ??                        h[\cf5 "border-color"\cf0 ] = hs;\par ??                    \}\par ??                \}  } --></p>
<div style="background: white none repeat scroll 0% 0%; font-family: Courier New; font-size: 10pt; color: black;">
<pre style="margin: 0px;"><span style="color: blue;">else</span> <span style="color: blue;">if</span> (key.Equals(<span style="color: #2b91af;">Markup</span>.CSS_KEY_BORDERCOLOR))</pre>
<pre style="margin: 0px;">{</pre>
<pre style="margin: 0px;">    <span style="color: #2b91af;">Color</span> c = <span style="color: #2b91af;">Markup</span>.DecodeColor(prop[key]);</pre>
<pre style="margin: 0px;">    <span style="color: blue;">if</span> (c != <span style="color: blue;">null</span>)</pre>
<pre style="margin: 0px;">    {</pre>
<pre style="margin: 0px;">        <span style="color: blue;">int</span> hh = c.ToArgb() &amp; 0xffffff;</pre>
<pre style="margin: 0px;">        <span style="color: #2b91af;">String</span> hs = <span style="color: #a31515;">"#"</span> + hh.ToString(<span style="color: #a31515;">"X06"</span>, <span style="color: #2b91af;">NumberFormatInfo</span>.InvariantInfo);</pre>
<pre style="margin: 0px;">        h[<span style="color: #a31515;">"border-color"</span>] = hs;</pre>
<pre style="margin: 0px;">    }</pre>
<pre style="margin: 0px;">}</pre>
</div>
<p>In addition you need to alter the output of the table as there is no default &#034;bordercolor&#034; style property in the IncTable class.</p>
<p>Open <strong>IncTable.cs</strong> and change the following code in the <strong>BuildTable </strong>method:<br />
Existing code:</p>
<p><!-- {\rtf1\ansi\ansicpg\lang1024\noproof1252\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red0\green0\blue255;\red43\green145\blue175;}??\fs20             \cf3 for\cf0  (\cf3 int\cf0  row = 0; row &lt; rows.Count; ++row) \{\par ??                \cf4 ArrayList\cf0  col = (\cf4 ArrayList\cf0 )rows[row];\par ??                \cf3 for\cf0  (\cf3 int\cf0  k = 0; k &lt; col.Count; ++k) \{\par ??                    table.AddCell((\cf4 PdfPCell\cf0 )col[k]);\par ??                \}\par ??            \}} --></p>
<div style="background: white none repeat scroll 0% 0%; font-family: Courier New; font-size: 10pt; color: black; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">
<pre style="margin: 0px;"><span style="color: blue;">for</span> (<span style="color: blue;">int</span> row = 0; row &lt; rows.Count; ++row) {</pre>
<pre style="margin: 0px;">    <span style="color: #2b91af;">ArrayList</span> col = (<span style="color: #2b91af;">ArrayList</span>)rows[row];</pre>
<pre style="margin: 0px;">    <span style="color: blue;">for</span> (<span style="color: blue;">int</span> k = 0; k &lt; col.Count; ++k) {</pre>
<pre style="margin: 0px;">        table.AddCell((<span style="color: #2b91af;">PdfPCell</span>)col[k]);</pre>
<pre style="margin: 0px;">    }</pre>
<pre style="margin: 0px;">}</pre>
</div>
<p>Replace with:</p>
<p><!-- {\rtf1\ansi\ansicpg\lang1024\noproof1252\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red43\green145\blue175;\red163\green21\blue21;\red0\green0\blue255;}??\fs20             \cf3 String\cf0  bordercolor = (\cf3 String\cf0 )props[\cf4 "bordercolor"\cf0 ];\par ??            \cf5 for\cf0  (\cf5 int\cf0  row = 0; row &lt; rows.Count; ++row)\par ??            \{\par ??                \cf3 ArrayList\cf0  col = (\cf3 ArrayList\cf0 )rows[row];\par ??                \cf5 for\cf0  (\cf5 int\cf0  k = 0; k &lt; col.Count; ++k)\par ??                \{\par ??                    \cf3 PdfPCell\cf0  cell = (\cf3 PdfPCell\cf0 )col[k];\par ??                    cell.BorderColor = \cf3 Markup\cf0 .DecodeColor(bordercolor);\par ??                    table.AddCell(cell);\par ??                \}\par ??            \}\par ??} --></p>
<div style="background: white none repeat scroll 0% 0%; font-family: Courier New; font-size: 10pt; color: black;">
<pre style="margin: 0px;"><span style="color: #2b91af;">String</span> bordercolor = (<span style="color: #2b91af;">String</span>)props[<span style="color: #a31515;">"border-color"</span>];</pre>
<pre style="margin: 0px;"><span style="color: blue;">for</span> (<span style="color: blue;">int</span> row = 0; row &lt; rows.Count; ++row)</pre>
<pre style="margin: 0px;">{</pre>
<pre style="margin: 0px;">    <span style="color: #2b91af;">ArrayList</span> col = (<span style="color: #2b91af;">ArrayList</span>)rows[row];</pre>
<pre style="margin: 0px;">    <span style="color: blue;">for</span> (<span style="color: blue;">int</span> k = 0; k &lt; col.Count; ++k)</pre>
<pre style="margin: 0px;">    {</pre>
<pre style="margin: 0px;">        <span style="color: #2b91af;">PdfPCell</span> cell = (<span style="color: #2b91af;">PdfPCell</span>)col[k];</pre>
<pre style="margin: 0px;">        cell.BorderColor = <span style="color: #2b91af;">Markup</span>.DecodeColor(bordercolor);</pre>
<pre style="margin: 0px;">        table.AddCell(cell);</pre>
<pre style="margin: 0px;">    }</pre>
<pre style="margin: 0px;">}</pre>
</div>
<p>This will change the border color on the cell in the table. Hint: It could be wise to check if the cell already has a border color before overwriting it with the table border color.</p>
<p>Recompile and add the new DLL to your project.</p>
<p>Happy Coding!</p>
]]></content:encoded>
			<wfw:commentRss>http://hamang.net/2008/08/14/html-to-pdf-in-net/feed/</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
	</channel>
</rss>
