How to create a paragraph with background color in iText using java. I tried with Chunk but its highlighted colour for text upto its length and there no bg color applied between lines.
-
Try this , http://stackoverflow.com/questions/6405623/how-to-set-a-background-color-of-a-table-cell-using-itext – Dinidu Hewage Dec 30 '15 at 04:15
-
3Possible duplicate of [How to set the paragraph of itext pdf file as rectangle with background color in Java](http://stackoverflow.com/questions/19976343/how-to-set-the-paragraph-of-itext-pdf-file-as-rectangle-with-background-color-in) – Dinidu Hewage Dec 30 '15 at 04:17
-
Did my answer solve your question? Or are there any issues left – mkl Jan 06 '16 at 11:16
1 Answers
Your task to create a paragraph with background color (in particular uninterrupted between lines) can be implemented by means of a page event listener which locally stores paragraph start positions and draws background rectangles as soon as the end of paragraph is signaled:
public class ParagraphBackground extends PdfPageEventHelper
{
public BaseColor color = BaseColor.YELLOW;
public void setColor(BaseColor color)
{
this.color = color;
}
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.saveState();
cb.setColorFill(color);
cb.rectangle(document.left(), document.bottom() - offset,
document.right() - document.left(), startPosition - document.bottom());
cb.fill();
cb.restoreState();
}
}
@Override
public void onParagraphEnd(PdfWriter writer, Document document, float paragraphPosition)
{
if (active)
{
PdfContentByte cb = writer.getDirectContentUnder();
cb.saveState();
cb.setColorFill(color);
cb.rectangle(document.left(), paragraphPosition - offset,
document.right() - document.left(), startPosition - paragraphPosition);
cb.fill();
cb.restoreState();
}
}
}
Whenever this page event listener is set active at an end of a paragraph, the paragraph background is colored.
It can be used like this:
@Test
public void testParagraphBackgroundEventListener() throws DocumentException, FileNotFoundException
{
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("document-with-paragraph-backgrounds.pdf"));
ParagraphBackground back = new ParagraphBackground();
writer.setPageEvent(back);
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:"));
back.setActive(true);
document.add(new Paragraph("This paragraph now has a background. Isn't that fantastic? By changing the event, we can even draw a border, change the line width of the border and many other things. Now let's deactivate the event."));
back.setActive(false);
document.add(new Paragraph("This paragraph no longer has a background."));
document.close();
}
(ColorParagraphBackground.java)
The result looks like this:
Credits
This actually is a rip-off of Bruno Lowagie's answer to "How to add border to paragraph in itext pdf library in java?" with small changes to fill instead of stroke background rectangles. He even had already back then though about an application like this as his sample program wrote:
This paragraph now has a border. Isn't that fantastic? By changing the event, we can even provide a background color
-
@Ranji *this is what I want!!!* - Great. In that case, please mark the answer as accepted (click on the tick on its top left corner). – mkl Jan 07 '16 at 12:39