如何使用java在iText中創建帶背景色的段落。 我嘗試過使用Chunk,但是它的高亮文本的顏色達到了它的長度,並且在行之間沒有應用bg顏色。使用iTEXT創建Java PDF
-1
A
回答
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();
}
}
}
每當此頁面事件監聽器在段落的一端設置爲活動,段落背景着色。
它可用於這樣的:
@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)
結果看起來像這樣:
積分
這實際上是一個Bruno Lowagie的answer to "How to add border to paragraph in itext pdf library in java?" rip-off與填充,而不是筆觸背景矩形的小改動。他甚至已經回來了,雖然這樣的應用程序,他的示例程序寫道:
本段現在有一個邊框。這不是太棒了嗎? 通過改變的情況下,我們甚至可以提供一個背景色
相關問題
- 1. 使用iText創建PDF
- 2. JavaFX - IText - 創建PDF時創建PDF
- 3. itext java pdf到文本創建
- 4. 使用iText從TIFF圖像創建PDF
- 5. 使用Spring,IText,水印創建PDF
- 6. 使用IText創建PDF文檔#
- 7. 無法使用iText和JSF創建PDF
- 8. 使用itext創建pdf的問題
- 9. zip使用itext創建的pdf文件
- 10. 的Android iText的PDF創建
- 11. 在JAVA中使用Apache POI和iText創建Word(DOC)中的PDF
- 12. 使用iText和java創建PDF的自定義模板
- 13. java itext使用希伯來語(rtl)和英語創建pdf
- 14. Itext使用Java創建PDF格式的XMP
- 15. 使用java創建PDF
- 16. iText Java disable print pdf
- 17. 使用itext創建PDF,使用按鈕嵌入動畫
- 18. 使用iText和java的PDF生成器
- 19. 使用java生成pdf文件,Itext
- 20. 使用iText在Java中使用獨立方法創建表格
- 21. 如何編寫用於打印使用iText創建的PDF的Java代碼
- 22. 的Android創建PDF以外的iText
- 23. 如何爲Itext創建子文檔PDF
- 24. iText - 創建超過1000個PDF的OutOfMemory
- 25. 如何使用iText創建啓用XFA的可編輯PDF
- 26. Java PDF創建
- 27. 與iText的Java的創建循環新的PDF
- 28. 如何使用Java和itext從Graphics對象創建多個頁面的PDF
- 29. 如何使用java中的itext創建pdf文檔的最後一頁底部
- 30. 在java中使用jasper或itext可以創建不可打印的pdf嗎?
試試這個,http://stackoverflow.com/questions/6405623/how-to-set-a-background-color-of-a- table-cell-using-itext –
可能的重複[如何將itext pdf文件的段落設置爲帶有Java背景顏色的矩形](http://stackoverflow.com/questions/19976343/how-to-set-the-段落-txt-pdf-file-as-rectangle-with-background-color-in) –
我的答案解決了你的問題嗎?還是還有任何問題 – mkl