【发布时间】:2017-12-02 11:24:58
【问题描述】:
我正在尝试将图例作为单行(段落)添加到在我的页面上绘制为图像的图表中。图例需要有描述符 (Chunk),后跟一条适当颜色的线以匹配图表。当我搜索如何画线时,似乎只能使用绝对定位来完成。无论描述符在段落和页面中的位置,是否有某种方法可以在描述符之后绘制线?它后面还会跟着一个或多个附加描述符(块),每个描述符都带有适当颜色的线。 TIA。
String[] monitors=Configuration.getInstance().getMonitorIds();
Chunk title=new Chunk("Legend: ");
title.setFont(legendFont);
Paragraph legend=new Paragraph(title);
for (String entry : monitors) {
Monitor mon=Configuration.getInstance().getMonitor(entry);
Chunk name=new Chunk(mon.getName());
Font nameFont=new Font(FontFamily.TIMES_ROMAN, 20.f, Font.BOLD,new BaseColor(mon.getColor().getRGB()));
name.setFont(nameFont);
// What goes here to draw the line???
legend.add(name);
}
document.add(legend);
@mkl 发表评论后更新:
我想我在这篇文章中找到了你的意思:Drawing diagonal line in a cell of a table in iTExt?。所以我在我的助手中添加了以下内容来绘制一条水平线而不是文章中的对角线:
@Override
public void onGenericTag(PdfWriter writer_, Document document_, Rectangle rectangle_, String tag_) {
PdfContentByte canvas = writer_.getDirectContent();
canvas.saveState();
Monitor mon=Configuration.getInstance().getMonitor(tag_);
canvas.setColorStroke(new BaseColor(mon.getColor().getRGB()));
canvas.moveTo(Rectangle.LEFT,Rectangle.ALIGN_CENTER);
canvas.lineTo(Rectangle.RIGHT,Rectangle.ALIGN_CENTER);
canvas.stroke();
canvas.restoreState();
}
然后我更改了我的代码(以及其他一些调整)以包含 onGenericTag 块,但在我解释文章时这不是直接的事情。我不能只在单元格中放入一个块。这是我当前的代码:
String[] monitorIds=Configuration.getInstance().getMonitorIds();
PdfPTable leg=new PdfPTable(monitorIds.length+2);
Paragraph p=new Paragraph("Legend: ",legendFont);
PdfPCell title=new PdfPCell(p);
title.setBorder(Rectangle.NO_BORDER);
leg.addCell(title);
for (String entry : monitorIds) {
Monitor mon=Configuration.getInstance().getMonitor(entry);
Font nameFont=new Font(FontFamily.TIMES_ROMAN, 14.f, Font.BOLD,new BaseColor(mon.getColor().getRGB()));
Paragraph name=new Paragraph(mon.getName(),nameFont);
PdfPCell c=new PdfPCell(name);
c.setBorder(Rectangle.NO_BORDER);
leg.addCell(c);
Chunk line=new Chunk();
line.setGenericTag(entry);
c=new PdfPCell(new Paragraph(line));
c.setBorder(Rectangle.NO_BORDER);
leg.addCell(c);
}
document.add(leg);
不幸的是,这条线没有出现。我究竟做错了什么? TIA。
在所有建议之后
感谢他们。我决定尝试坚持使用 OnGenericTag。我认为在这一点上开始一个新线程更合适。 Drawing a Line in a PdfPCell Using OnGenericTag
【问题讨论】:
-
您是否研究过自定义事件?我假设您可以使用它们来实现您的目标。
-
事件?这不是页面处理结束之类的吗?我想这可能是某种 PdfTemplate 的东西,但我会看看。