> Singhal, Ramneek (Exchange) wrote:
>> A newbie question. How to draw a horizontal line (same as <HR> in html)
>> in PDF using iText.
> Document document = new Document(PageSize.A4, 36, 36, 36, 36);
> PdfWriter writer = PdfWriter.getInstance(document, out);
> PdfContentByte cb = writer.getDirectContent();
> cb.setLineWidth(2.0f); // Make a bit thicker than 1.0 default
> cb.setGrayStroke(0.95f); // 1 = black, 0 = white
> float x = 72f;
> float 7 = 72f;
> cb.moveTo(x, y);
> cb.lineTo(x + 72f*6, y);
That's a good example.
A follow-up question could be: how do I know the value of y?
There are different possibilities.
PdfWriter has a method getVerticalPosition,
but I like to use page events to draw lines,
for instance using onParagraph or onGenericTag.
> On a related note if you know how to do a relative lineTo in iText I
> would like to know, maybe PDF doesn't allow it ?
See table 4.9 on page 196-197 of the PDF Reference manual
for a list with the 'path construction operators'.
There is no relative lineTo...