【问题标题】:Drawing a Line at current location in Itext在 Itext 中的当前位置绘制一条线
【发布时间】: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 的东西,但我会看看。

标签: java itext line


【解决方案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 到平底船 x,y 画一条线(这是一个不可见的点,而不是行)。

另一种方法是创建一个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 x 80 个用户单位的模板,并在该模板上绘制了一个红色矩形。我们发布模板(内容写入输出),我们将模板添加到PdfPTable 作为Image

【讨论】:

  • 我不想强调任何东西。我想大多数人都会知道图表上的图例是什么。图例使用描述符显示线条类型、颜色等,以便读者可以将适用于哪些线条与其在随附图表上所代表的内容联系起来。所以我的目标是创建一个包含描述符文本的单元格,然后创建一个包含与图表中颜色相同的线条的单元格。我确实添加了助手类,但没有显示。我已经为其他事件工作了,所以我只是展示了通用标签的添加方法,因为其余的与我的问题无关。
  • 感谢您重新安排我的 OP。我没有想到这一点,也不知道如何将格式化代码添加到评论中。我想我不能。最后,我使用的是 itext 5 而不是 7,因为:stackoverflow.com/questions/47593083/…
  • 所以基本上,你想要一个矢量图像?这是通过将PdfTemplate 包裹在Image 中完成的(如果需要,图像可以包裹在Chunk 中)。您正在尝试使用Chunk 执行此操作,但由于Chunk 为空,因此没有显示任何内容。将Chunk line=new Chunk(); 更改为Chunk line=new Chunk("\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0");,您应该会看到一行。请注意,我的回答仍然有效:您可以在line 对象上使用setUnderline(),并且该行也会显示。使用y 值来定位线。这比使用页面事件要容易得多。
  • 我已经更新了我的答案。基本上有4种不同的方式来实现你想要的。 (1.) 添加一个Chunk,用setUnderline() 绘制线条,(2.) 添加一个Chunk,用分隔符类绘制线条,(3.) 使用onGenericTag() 绘制内容,以及(4. ) 在PdfTemplate 中绘制内容。
  • 感谢您的建议和对不清楚的地方表示歉意。我决定厌倦 onGenericTag 并更新了 OP。我正在开始一个更适合该线程所采用的新方向的新线程。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-04
  • 2013-03-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多