3

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 ?

Amit Das
  • 1,077
  • 5
  • 17
  • 44

2 Answers2

4

Please take a look at the BorderForParagraph example. It shows how to add a border for a paragraph like this:

enter image description here

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:

enter image description here

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();
        }
    }
}
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • 1
    Depending 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
1

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);
}
singhswat
  • 832
  • 7
  • 20
Pankaj Dubey
  • 146
  • 10