I have created a paragraph in using itext pdf library in java. I have to add border to paragraph, not to the whole document. How to do it ?
-
Check out this http://itextpdf.com/sandbox/events/PageBorder. – Deepika Rajani May 05 '15 at 13:02
-
@user3271518 ...didnt find anything yet – Amit Das May 05 '15 at 13:21
-
@DeepikaRajani it is for whole document not for the paragraph – Amit Das May 05 '15 at 13:41
-
Yes I know. I thought May be that can help. – Deepika Rajani May 05 '15 at 17:05
2 Answers
Please take a look at the BorderForParagraph example. It shows how to add a border for a paragraph like this:
There is no method that allows you to create a border for a Paragraph
, but you can create a PdfPageEvent
implementation that allows you to draw a rectangle based on the start and end position of the Paragraph
:
class ParagraphBorder extends PdfPageEventHelper {
public boolean active = false;
public void setActive(boolean active) {
this.active = active;
}
public float offset = 5;
public float startPosition;
@Override
public void onParagraph(PdfWriter writer, Document document, float paragraphPosition) {
this.startPosition = paragraphPosition;
}
@Override
public void onParagraphEnd(PdfWriter writer, Document document, float paragraphPosition) {
if (active) {
PdfContentByte cb = writer.getDirectContentUnder();
cb.rectangle(document.left(), paragraphPosition - offset,
document.right() - document.left(), startPosition - paragraphPosition);
cb.stroke();
}
}
}
As you can see, I introduced a boolean
parameter named active
. By default, I've set this parameter to false
. I also create an offset
(change this value to fine-tune the result) and a startPosition
parameter.
Each time iText starts rendering a Paragraph
object, the startPosition
value is updated. Each time iText ends rendering a Paragraph
, a rectangle is drawn if active
is true
(otherwise nothing happens).
We use this event like this:
public void createPdf(String dest) throws IOException, DocumentException {
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
ParagraphBorder border = new ParagraphBorder();
writer.setPageEvent(border);
document.open();
document.add(new Paragraph("Hello,"));
document.add(new Paragraph("In this document, we'll add several paragraphs that will trigger page events. As long as the event isn't activated, nothing special happens, but let's make the event active and see what happens:"));
border.setActive(true);
document.add(new Paragraph("This paragraph now has a border. Isn't that fantastic? By changing the event, we can even provide a background color, change the line width of the border and many other things. Now let's deactivate the event."));
border.setActive(false);
document.add(new Paragraph("This paragraph no longer has a border."));
document.close();
}
As you can see, we declare the event to the PdfWriter
using the setPageEvent()
method. We activate the event like this:
border.setActive(true);
and we deactivate it like this:
border.setActive(false);
This is only a proof of concept! You will need to implement the onStartPage()
and onEndPage()
method if you want this to work for paragraphs that span more than one page. That's shown in BorderForParagraph2:
The onStartPage()
and onEndPage()
implementation is a no-brainer:
class ParagraphBorder extends PdfPageEventHelper {
public boolean active = false;
public void setActive(boolean active) {
this.active = active;
}
public float offset = 5;
public float startPosition;
@Override
public void onStartPage(PdfWriter writer, Document document) {
startPosition = document.top();
}
@Override
public void onParagraph(PdfWriter writer, Document document, float paragraphPosition) {
this.startPosition = paragraphPosition;
}
@Override
public void onEndPage(PdfWriter writer, Document document) {
if (active) {
PdfContentByte cb = writer.getDirectContentUnder();
cb.rectangle(document.left(), document.bottom() - offset,
document.right() - document.left(), startPosition - document.bottom());
cb.stroke();
}
}
@Override
public void onParagraphEnd(PdfWriter writer, Document document, float paragraphPosition) {
if (active) {
PdfContentByte cb = writer.getDirectContentUnder();
cb.rectangle(document.left(), paragraphPosition - offset,
document.right() - document.left(), startPosition - paragraphPosition);
cb.stroke();
}
}
}

- 75,994
- 9
- 109
- 165
-
1Depending on the exact requirements, the rectangles drawn for paragraphs crossing page breaks should be open at the bottom (on first page) or the top (on the second page) or even at both top and bottom for paragraphs spanning more than two pages on the inner pages. But that is a good exercise for the reader... ;) – mkl May 05 '15 at 14:59
-
Your code is working fine if the paragraph in on one page but I has issues if the paragraph starts at one page and ends on the other than It has issues.It adds the part of the paragraph on first page inside the borders and the part on other page is borderless. – mominapk Mar 17 '16 at 06:51
-
@mominapk And that's exactly what [the second example](http://developers.itextpdf.com/examples/page-events/page-events-paragraphs#962-borderforparagraph2.java) is about. Didn't you read the full answer? Don't you see that what you say is contradicted in the second screen shot? – Bruno Lowagie Mar 17 '16 at 13:33
-
When i followed the second example then borders are everywhere i am unable to remove the borders i.e. border.setActive(false) not working. Actually i have added the titles as paragraphs and the other description in the sections n chapter and instead of paragraphs written your code in section methods like onSectionEnd() etc but getting the borders around the title paragraphs too. Please help me with this stuck on it for 4 days now. – mominapk Mar 18 '16 at 05:15
-
It works for me. Maybe you should hire a specialist. I think hiring a specialist for 1 day might be about as expensive as 4 days of your time. Or you could try asking a decent question. Maybe that would work too. – Bruno Lowagie Mar 18 '16 at 05:56
-
@kegsproduction You must be doing something wrong. Did you publish your projects as AGPL software on Github or are you a paying customer? – Bruno Lowagie Sep 25 '17 at 12:21
Try this:
public static void main(String[] args) {
Document document = new Document();
// step 2
PdfWriter writer=PdfWriter.getInstance(document, new FileOutputStream("somepath"));
document.setPageSize(PageSize.LETTER);
document.setMargins(36, 72, 108, 180);
document.setMarginMirroring(false);
// step 3
document.open();
// step 4
Rectangle rect= new Rectangle(36,108);
rect.enableBorderSide(1);
rect.enableBorderSide(2);
rect.enableBorderSide(4);
rect.enableBorderSide(8);
rect.setBorder(2);
rect.setBorderColor(BaseColor.BLACK);
rect.setBorderWidth(2);
document.add(rect);
}

- 832
- 7
- 20

- 146
- 10
-
Basically you need to rely on below methods rect.setBorder(Rectangle.BOX); rect.setBorderWidth(2); – Pankaj Dubey May 05 '15 at 13:10
-
in this you are adding this rect to document, but i want to add border to paragraph like - Paragraph par = new Paragraph(); par.add(rect); – Amit Das May 05 '15 at 13:19
-
par.setBorder(someparam) & par.setBorderWidth(someWidth) will work on paragraph's object as well, Please try. – Pankaj Dubey May 05 '15 at 13:21
-
1There is no method available like - setBorder and setBorderWidth in Paragraph Class. – Amit Das May 05 '15 at 13:22
-