【发布时间】:2015-05-05 12:58:07
【问题描述】:
我在 java 中使用 itext pdf 库创建了一个段落。我必须为段落添加边框,而不是整个文档。怎么办?
【问题讨论】:
-
@user3271518 ...还没有找到任何东西
-
@DeepikaRajani 适用于整个文档而不是段落
-
是的,我知道。我认为这可能会有所帮助。
我在 java 中使用 itext pdf 库创建了一个段落。我必须为段落添加边框,而不是整个文档。怎么办?
【问题讨论】:
请查看BorderForParagraph 示例。它显示了如何为这样的段落添加边框:
没有方法可以让您为Paragraph 创建边框,但您可以创建一个PdfPageEvent 实现,允许您根据Paragraph 的开始和结束位置绘制一个矩形:
class ParagraphBorder extends PdfPageEventHelper {
public boolean active = false;
public void setActive(boolean active) {
this.active = active;
}
public float offset = 5;
public float startPosition;
@Override
public void onParagraph(PdfWriter writer, Document document, float paragraphPosition) {
this.startPosition = paragraphPosition;
}
@Override
public void onParagraphEnd(PdfWriter writer, Document document, float paragraphPosition) {
if (active) {
PdfContentByte cb = writer.getDirectContentUnder();
cb.rectangle(document.left(), paragraphPosition - offset,
document.right() - document.left(), startPosition - paragraphPosition);
cb.stroke();
}
}
}
如您所见,我引入了一个名为active 的boolean 参数。默认情况下,我将此参数设置为false。我还创建了一个offset(更改此值以微调结果)和一个startPosition 参数。
每次 iText 开始渲染 Paragraph 对象时,startPosition 值都会更新。每次 iText 结束渲染 Paragraph 时,如果 active 是 true,则会绘制一个矩形(否则不会发生任何事情)。
我们这样使用这个事件:
public void createPdf(String dest) throws IOException, DocumentException {
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
ParagraphBorder border = new ParagraphBorder();
writer.setPageEvent(border);
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:"));
border.setActive(true);
document.add(new Paragraph("This paragraph now has a border. Isn't that fantastic? By changing the event, we can even provide a background color, change the line width of the border and many other things. Now let's deactivate the event."));
border.setActive(false);
document.add(new Paragraph("This paragraph no longer has a border."));
document.close();
}
如您所见,我们使用setPageEvent() 方法向PdfWriter 声明事件。我们像这样激活事件:
border.setActive(true);
我们这样停用它:
border.setActive(false);
这只是概念证明!如果您希望它适用于跨越一页以上的段落,您将需要实现 onStartPage() 和 onEndPage() 方法。这显示在BorderForParagraph2:
onStartPage() 和 onEndPage() 的实现很简单:
class ParagraphBorder extends PdfPageEventHelper {
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.rectangle(document.left(), document.bottom() - offset,
document.right() - document.left(), startPosition - document.bottom());
cb.stroke();
}
}
@Override
public void onParagraphEnd(PdfWriter writer, Document document, float paragraphPosition) {
if (active) {
PdfContentByte cb = writer.getDirectContentUnder();
cb.rectangle(document.left(), paragraphPosition - offset,
document.right() - document.left(), startPosition - paragraphPosition);
cb.stroke();
}
}
}
【讨论】:
试试这个:
public static void main(String[] args) {
Document document = new Document();
// step 2
PdfWriter writer=PdfWriter.getInstance(document, new FileOutputStream("somepath"));
document.setPageSize(PageSize.LETTER);
document.setMargins(36, 72, 108, 180);
document.setMarginMirroring(false);
// step 3
document.open();
// step 4
Rectangle rect= new Rectangle(36,108);
rect.enableBorderSide(1);
rect.enableBorderSide(2);
rect.enableBorderSide(4);
rect.enableBorderSide(8);
rect.setBorder(2);
rect.setBorderColor(BaseColor.BLACK);
rect.setBorderWidth(2);
document.add(rect);
}
【讨论】: