2015-12-30 121 views
-1

如何使用java在iText中创建带背景色的段落。 我尝试过使用Chunk,但是它的高亮文本的颜色达到了它的长度,并且在行之间没有应用bg颜色。使用iTEXT创建Java PDF

+0

试试这个,http://stackoverflow.com/questions/6405623/how-to-set-a-background-color-of-a- table-cell-using-itext –

+3

可能的重复[如何将itext pdf文件的段落设置为带有Java背景颜色的矩形](http://stackoverflow.com/questions/19976343/how-to-set-the-段落-txt-pdf-file-as-rectangle-with-background-color-in) –

+0

我的答案解决了你的问题吗?还是还有任何问题 – mkl

回答

0

你的任务是创建与背景颜色一个段落(特别是线之间的不间断),可以通过页面事件监听器本地存储段落的起始位置,并尽快绘制背景矩形作为段落的结尾来实现发信号通知:

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(); 
     } 
    } 
} 

ParagraphBackground.java

每当此页面事件监听器在段落的一端设置为活动,段落背景着色。

它可用于这样的:

@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

结果看起来像这样:

screenshot


积分

这实际上是一个Bruno Lowagieanswer to "How to add border to paragraph in itext pdf library in java?" rip-off与填充,而不是笔触背景矩形的小改动。他甚至已经回来了,虽然这样的应用程序,他的示例程序写道:

本段现在有一个边框。这不是太棒了吗? 通过改变的情况下,我们甚至可以提供一个背景色

+0

谢谢..这是我想要的! – Ranji

+0

@Ranji *这是我想要的!!! * - 太棒了。在这种情况下,请将答案标记为已接受(点击其左上角的勾号)。 – mkl