使用iText将矩形添加为内联元素 [英] Add rectangle as inline-element with iText

查看:143
本文介绍了使用iText将矩形添加为内联元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将矩形(或其他图形元素)作为内联元素添加到iText PDF?



我正在尝试实现的示例代码:

  foreach(入组列表中的行r)
{
p = new Paragraph();
p.IndentationLeft = 10;
p.SpacingBefore = 10;
p.SpacingAfter = 10;

p.Add(new Rectangle(0,0,10,10)); <<<<<<<<<那一个失败
p.Add(新段落(r.GetString(caption),tahoma12b));
p.Add(new Paragraph(r.GetString(description),tahoma12));
((段落)p [1])。IndentationLeft = 10;
doc.Add(p);
}

它类似于一列文本块,其中每个都有(仅打印)复选框。



我用 DirectContent 尝试过各种各样的东西,但它要求我提供绝对X和Y值。我根本就没有。元素应该打印在当前位置,无论在哪里。



任何线索?

解决方案

你需要一个 Chunk ,你已经定义了 通用标记 。例如,在此示例中列出了许多电影,在制作电影的一年中绘制了一个pellicule,并在IMDB链接的背景中绘制了一个椭圆。



如果你看一下 MovieYears 示例,您将了解如何使用 PdfPageEvent 接口及其 onGenericTag()方法。你是不对的,你不能将 Rectangle 添加到段落(恕我直言,这没什么意义)。正如您所指出的,您需要将矩形绘制为直接内容,并使用 setGenericTag()获得 Chunk 的坐标方法。只要在页面上绘制 Chunk ,其坐标就会传递给 onGenericTag()方法。 / p>

How do I add a rectangle (or other graphical elements) as inline-elements to an iText PDF?

Example code of what I'm trying to achieve:

foreach (Row r in entrylist)
{
    p = new Paragraph();
    p.IndentationLeft = 10;
    p.SpacingBefore = 10;
    p.SpacingAfter = 10;

    p.Add(new Rectangle(0, 0, 10, 10));  <<<<<<<<< THAT ONE FAILS
    p.Add(new Paragraph(r.GetString("caption"), tahoma12b));
    p.Add(new Paragraph(r.GetString("description"), tahoma12));
    ((Paragraph)p[1]).IndentationLeft = 10;
    doc.Add(p);
}

It's something like a column of text-blocks, of which each of them have (only a printed) checkbox.

I've tried various things with DirectContent, but it requires me to provide absolute X and Y values. Which I simply don't have. The elements should be printed at the current position, wherever that may be.

Any clues?

解决方案

You need a Chunk for which you've defined a generic tag. For instance, in this example listing a number of movies, a snippet of pellicule is drawn around the year a movie was produced and an ellipse was drawn in the background of the link to IMDB.

If you look at the MovieYears example, you'll find out how to use the PdfPageEvent interface and its onGenericTag() method. You're right that you can't add a Rectangle to a Paragraph (IMHO that wouldn't make much sense). As you indicate, you need to draw the rectangle to the direct content, and you get the coordinates of a Chunk by using the setGenericTag() method. As soon as the Chunk is drawn on the page, its coordinates will be passed to the onGenericTag() method.

这篇关于使用iText将矩形添加为内联元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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