How to add border to paragraph in itext pdf library in java?
Please see the BorderForParagraph example . It shows you how to add a paragraph border as follows:
There is no way that allows you to create a border for Paragraph
, but you can create an implementation PdfPageEvent
that allows you to draw a rectangle based on the start and end position 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 entered a parameter boolean
named active
. By default, I have set this option to false
. I also create offset
(change this value to fine tune the result) and a parameter startPosition
.
Every time iText starts rendering an object Paragraph
, the value is startPosition
updated. Every time iText finishes rendering Paragraph
, the rectangle is drawn, if active
any true
(otherwise nothing happens).
We use this event as follows:
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 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 deactivate the event."));
border.setActive(false);
document.add(new Paragraph("This paragraph no longer has a border."));
document.close();
}
As you can see, we are declaring the event PdfWriter
using the method setPageEvent()
. We will trigger the event as follows:
border.setActive(true);
and we deactivate it like this:
border.setActive(false);
This is just a proof of concept ! You will need to implement methods onStartPage()
and onEndPage()
if you want this to work for paragraphs that span more than one page. This is shown in BorderForParagraph2 :
Implementation onStartPage()
and onEndPage()
does not require much effort:
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();
}
}
}
source to share
Try the following:
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);
document.add(rect);
}
source to share