How can I add a underline of header of pdf by using itext ColumnText? I set the width of line 100%, but it doesn't work

panjiefeng :
//set the width to 90%, but the generated pdf shows 100%. Please look at the picture below
LineSeparator underLine = new LineSeparator(1, 90f, null, Element.ALIGN_CENTER, 0);
Phrase phrase1 = new Phrase();
phrase1.add(underLine);
ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_CENTER, phrase1, 0, pageSize.getHeight() - 75, 0);

whatever I set 90% or 100% for the width, it shows 100%.Please look at the picture below

the screenshot of generated pdf

cgrim :

When you want to make line on absolute position it's better to draw it directly:

PdfContentByte canvas = writer.getDirectContent();
canvas.setColorStroke(BaseColor.BLACK);
canvas.moveTo(36, pageSize.getHeight() - 75);
canvas.lineTo(pageSize.getWidth() - 36, pageSize.getHeight() - 75);
canvas.closePathStroke();

In your example the problem is in using of LineSeparator in a Phrase with combination of ColumnText.showTextAligned() method which is used to position text on absolute position. When using relative positioning document.add(underLine) then the width works.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=107926&siteId=1