如何使用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