2015-12-30 120 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