Narendra
Narendra

Reputation: 3117

How to show horizontal line in iTextSharp

I am creating a pdf and need to put a horizontal line in the page. Can anyone tell how to do that?

I have a xml file which has my html tag(<table>....</table>). And the whole content of xml file is parsed to a string which is used to create the pdf. Now some tags are not supported. One of them is <hr>. So is there any other tag which I can use in the xml file so that this will draw a line when the pdf is created using xml data.

Below is an example of xml xontent

<table>
   <tr>
     <td>
       <span>
           This is working properly.
       </span>
     </td>
   <tr>
</table>

<table>
   <tr>
     <td>
       <span>
           <hr>
           This is not working properly.
       </span>
     </td>
   <tr>
</table>

Please let me know if any more information is needed.

Thanks in advance.

Upvotes: 8

Views: 8462

Answers (3)

Peter
Peter

Reputation: 9712

The following creates a full width black line a few pixels thick, I'm using HTMLWorker.Parse:

<table>
   <tr>
    <td>
       <span>
           This is working properly.
       </span>
    </td>
   <tr>
</table>

<table>
   <tr>
    <td>
       <span>
       <table border="1" cellpadding="0" cellspacing="0"><tr><td>&nbsp;</td></tr></table>

           This is working properly now too!
       </span>
    </td>
   <tr>
</table>

Upvotes: 4

Saksham
Saksham

Reputation: 9390

I hope this helps you out

PdfPTable table = new PdfPTable(1);                //Create a new table with one column
PdfPCell cellLeft = new PdfPCell();                //Create an empty cell
StyleSheet style = new StyleSheet();               //Declare a stylesheet
style.LoadTagStyle("h1", "border-bottom", "red");          //Create styles for your html tags which you think will be there in PDFText
 List<IElement> objects = HTMLWorker.ParseToList(new StringReader(PDFText),style);  //This transforms your HTML to a list of PDF compatible objects
for (int k = 0; k < objects.Count; ++k)
{
     cellLeft.AddElement((IElement)objects[k]);    //Add these objects to cell one by one
}
table.AddCell(cellLeft);

Upvotes: 0

Peter
Peter

Reputation: 27944

You can draw lines from begining postion (moveto), LineTo and then stroke (commit the line):

...
PdfContentByte cb = writer.DirectContent;
....
cb.MoveTo(doc.PageSize.Width / 2, doc.PageSize.Height / 2);     
cb.LineTo(doc.PageSize.Width / 2, doc.PageSize.Height);     
cb.Stroke();
...

Upvotes: 0

Related Questions