简体   繁体   中英

itextsharp html to pdf creates no borders

I use iTextSharp (on SharePoint but I hope this does not matter) to convert a HTML document to PDF. So far I was unable to get any borders around elements. How do I do this? Is this not supported?

Things I tried:

  // in c# code
StyleSheet styles = new iTextSharp.text.html.simpleparser.StyleSheet();
styles.LoadStyle("borderCls", "border-style", "solid"); // <td class="borderCls">
styles.LoadStyle("borderCls", "border-width", "1px");
styles.LoadStyle("borderCls", "border-color", "#000");
  //
styles.LoadStyle("borderCls", "border", "solid 1px #000");

  // in html
<td style="border:solid 1px #000">
  //
<td border="1">
  //
<td style="border-style:solid;border-width:1px">

But these did not work. I just can't get iTextSharp to create any borders.

Update : Also is it possible just to define a Border on only one specific side?

use this code.

<table border="1">

sure its woirking. but solid and px is not working in html to pdf.

you can play with table borders and have some examples here: http://demo.itextsupport.com/xmlworker/

XMLWorker CSS and HTMLsupport documentation http://demo.itextsupport.com/xmlworker/itextdoc/index.html

If you want to set only the left border in a table using only css, use the code below:

td.black-left-border {
    border: 0;    /*First set all the borders to 0, then set the desired borders width*/
    border-color: black;
    border-left-width: 1px; /*PDF*/
    border-left-style: solid;
}

you can also set the borders with different colors:

td.black-left-border-blue-bottom-border {
    border: 0;    
    border-left-color: black;
    border-bottom-color: blue; 
    border-left-width: 1px; 
    border-bottom-width: 1px;
    border-left-style: solid;
    border-bottom-style: solid;
}

Maybe the browser won't apply these styles but iTextSharp will. As you can see, you must be very specific in the css properties.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM