在Itext中的当前位置绘制一条线 [英] Drawing a Line at current location in Itext

查看:173
本文介绍了在Itext中的当前位置绘制一条线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将图例作为单行(段落)添加到在我的页面上绘制为图像的图表中。图例需要具有描述符(Chunk),后跟适当颜色的线以匹配图表。当我搜索如何绘制线条时,似乎我只能使用绝对定位来完成。是否有某种方法可以在描述符后面绘制线条,无论描述符在段落和页面中的位置如何?它之后还会有一个或多个附加描述符(块),每个描述符都有一条适当的彩色线。 TIA。

I am trying to add a legend as a single line (Paragraph) to a chart drawn as an image on my page. The legend needs to have the descriptor (Chunk) followed by a line of the appropriate color to match the chart. When I search on how to draw a line, it appears that I can only do it using absolute positioning. Is there some way to draw the line following the descriptor regardless of where the descriptor will be in the paragraph and on the page? It will also be followed by one or more additional descriptors (Chunks) each with an appropriately colored line. 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发表评论后更新:

我想我在本文中找到了你的意思:在iTExt中的表格的单元格中绘制对角线?。所以我将以下内容添加到我的帮助器中以绘制水平线而不是文章中的对角线:

I think I found what you meant in this article: Drawing diagonal line in a cell of a table in iTExt?. So I added the following to my helper to draw a horizontal line rather than the diagonal in the article:

@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大块,但这不是一个直接的事情,因为我解释了这篇文章。我不能只把一个块放在一个单元格中。这是我当前的代码:

Then I changed my code (along with some other tweaking) to include the onGenericTag Chunk but it was not a direct thing as I interpreted the article. I can't put just a Chunk in a Cell. Here is my current code:

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。

Unfortunately the line does not show up. What am I doing wrong? TIA.

完成所有建议后

感谢他们。我决定尝试坚持使用OnGenericTag。我认为在这一点上,开始一个新线程更合适。
使用OnGenericTag在PdfPCell中绘制一条线

Thanks for them. I've decided to try to stick with the OnGenericTag. I think it is more appropriate, at this point, to start a new thread. Drawing a Line in a PdfPCell Using OnGenericTag

推荐答案

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

If you want to draw a line, you can use a Chunk consisting of non-breaking spaces:

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

现在使用 setUnderline()方法画一条线。参见 API文档

Now use the setUnderline() method to draw a line. See the API docs.

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

You can invoke this method on the same chunk as many times as you need lines (e.g. in some cases you might want to draw a double line). You can pass the color as a parameter, including the thickness of the line, either as an absolute value, or as a value relative to the font size, and including the y position with respect to the baseline of the text:

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

这就是参数大约是:


  • color - 行的颜色或null文字颜色

  • 厚度 - 线条的绝对厚度

  • thicknessMul - 厚度乘法因子与字体大小

  • yPosition - 绝对y位置相对到基线

  • yPositionMul - 位置乘法因子与字体大小

  • 上限 - 结束线上限。允许的值为 PdfContentByte.LINE_CAP_BUTT PdfContentByte.LINE_CAP_ROUND PdfContentByte.LINE_CAP_PROJECTING_SQUARE

  • color - the color of the line or null to follow the text color
  • thickness - the absolute thickness of the line
  • thicknessMul - the thickness multiplication factor with the font size
  • yPosition - the absolute y position relative to the baseline
  • yPositionMul - the position multiplication factor with the font size
  • cap - the end line cap. Allowed values are PdfContentByte.LINE_CAP_BUTT, PdfContentByte.LINE_CAP_ROUND and PdfContentByte.LINE_CAP_PROJECTING_SQUARE

您还可以使用分隔符(例如参见 LineSeparator API文档中的课程)?请参阅分隔符示例。

You can also use a separator (see for instance the LineSeparator class in the API docs)? See the Separator examples.

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

You can use such a separator as a line in-between two lines of text:

  • How to create a custom dashed line separator?
  • itextsharp how to add a full line break
  • How to decrease the line spacing in LineSeparator itext
  • ...

但是你也可以使用这样的分隔符作为。例如,参见创建索引文件(TOC)使用java中的itext库合并pdf 我们有:

But you can also use such a separator as a Chunk. See for instance Create Index File(TOC) for merged pdf using itext library in java where we have:

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

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

You could define a special LineSeparator class for each color, and wrap it inside a Chunk.

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

I see that you've tried the onGenericTag() functionality, but were unsuccessful. If you really need that functionality make sure that you declare the page event to the PdfWriter.

参见例如: 如何每25个字画一条线?

在这个例子中,我们有一个名为 WordCount 的页面事件类,我们将该类的实例添加到 PdfWriter object:

In this example, we have a page event class named WordCount, and we add an instance of that class to the PdfWriter object:

writer.setPageEvent(new WordCounter());

如果你忘了这一点,不会发生任何事情(原因很合理)。

If you forget this, nothing will happen (for a very logical reason).

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

If you want a line to be drawn, you also need to be sure the Chunk isn't empty, or you draw a line from point x,y to punt x,y (that's an invisible dot, not a line).

另一种方法是创建一个 PdfTemplate ,在其中绘制一条线,并在图像中包装 PdfTemplate 。请参阅如何在iTextPDF中对齐图形?或采取查看 RectangleInCell 示例:

An alternative, is to create a PdfTemplate, draw a line to it, and wrap that PdfTemplate inside an image. See How do I align a graphic in iTextPDF? or take a look at the RectangleInCell example:

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 作为图像

In this example, we create a template measuring 120 by 80 user units, and we draw a red rectangle on that template. We release the template (the content is written to the output), and we add the template to a PdfPTable as an Image.

这篇关于在Itext中的当前位置绘制一条线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆