在Itext中的当前位置绘制一条线

时间:2017-12-02 11:24:59

标签: java itext line

我正在尝试将图例作为单行(段落)添加到在我的页面上绘制为图像的图表中。图例需要具有描述符(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 Chunk,但这不是一个直接的东西,因为我解释了这篇文章。我不能只把一个块放在一个单元格中。这是我目前的代码:

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

1 个答案:

答案 0 :(得分:1)

如果你想绘制一条线,你可以使用由不间断空格组成的Chunk

Chunk line = new Chunk("\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0");

现在使用setUnderline()方法绘制一条线。请参阅the API docs

您可以在同一个块上调用此方法,因为您需要多行(例如,在某些情况下,您可能需要绘制一条双线)。您可以将颜色作为参数传递,包括线条的粗细,作为绝对值,或相对于字体大小的值,并包括相对于文本基线的y位置:

public Chunk setUnderline(BaseColor color,
    float thickness, float thicknessMul,
    float yPosition, float yPositionMul,
    int cap)

这是参数的内容:

  • color - 行的颜色或null以跟随文本颜色
  • thickness - 线条的绝对粗细
  • thicknessMul - 字体大小
  • 的厚度倍增系数
  • yPosition - 相对于基线的绝对y位置
  • yPositionMul - 字体大小的位置倍增因子
  • cap - 结束线上限。允许的值为PdfContentByte.LINE_CAP_BUTTPdfContentByte.LINE_CAP_ROUNDPdfContentByte.LINE_CAP_PROJECTING_SQUARE

您还可以使用分隔符(例如参见API docs中的LineSeparator类)?请参阅Separator示例。

您可以将这样的分隔符用作两行文本之间的一行:

但您也可以将此类分隔符用作Chunk。例如,参见我们所拥有的Create Index File(TOC) for merged pdf using itext library in java

p.add(new Chunk(new DottedLineSeparator()));

您可以为每种颜色定义一个特殊的LineSeparator类,并将其包装在Chunk内。

我发现您已尝试onGenericTag()功能,但未成功。如果您确实需要该功能,请确保将页面事件声明为PdfWriter

例如参见:How to draw a line every 25 words?

在此示例中,我们有一个名为WordCount的页面事件类,我们将该类的实例添加到PdfWriter对象:

writer.setPageEvent(new WordCounter());

如果你忘了这一点,什么都不会发生(出于一个非常合乎逻辑的原因)。

如果你想绘制一条线,你还需要确保Chunk不是空的,或者从点x,y到punt x,y画一条线(&#39) ; s是一个不可见的点,而不是一条线。)

另一种方法是创建PdfTemplate,为其绘制一条线,并将PdfTemplate包裹在图像中。请参阅How do I align a graphic in iTextPDF?或查看RectangleInCell示例:

PdfTemplate template = writer.getDirectContent().createTemplate(120, 80);
template.setColorFill(BaseColor.RED);
template.rectangle(0, 0, 120, 80);
template.fill();
writer.releaseTemplate(template);
table.addCell(Image.getInstance(template));

在这个例子中,我们创建一个测量120个用户单位的模板,然后在该模板上绘制一个红色矩形。我们发布模板(内容写入输出),我们将模板作为PdfPTable添加到Image

相关问题