表在同一行 [英] Table on same line

查看:162
本文介绍了表在同一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的发布商项目中使用了itextpdf-5.5.6。我想在一个段落或短语或文本的同一行设置一个表?

I use itextpdf-5.5.6 in my publisher project. I want to set a table on same line that a paragraph or phrase or text ?

这样的事情:

在我的段落|我的TAB OF ONE COLUMN |存在于同一行。

In My Para |MY TAB OF ONE COLUMN| is present on a same line.

是否可能?

推荐答案

你不需要桌子。您需要的是一个通用标记事件。看看Chris Haas对这个问题的回答:如何使用ITEXT PDF中的虚线标注文本

You don't need a table. What you need, is a generic tag event. Take a look at the answer by Chris Haas to this question: How do you underline text with dashedline in ITEXT PDF

在这个答案中,他描述了如何使用通常被称为的页面事件通用标记功能。您可以在此处找到Java示例: DashedUnderline

In this answer, he describes how to use a page event often referred to as the generic tag functionality. You can find a Java example here: DashedUnderline

在此示例中,我们使用通用标记功能为加下划线,但不是绘制虚线,而是可以轻松绘制矩形。

In this example, we use the generic tag functionality to underline a Chunk, but instead of drawing a dashed line, you can easily draw a rectangle.

你创建一个像这样的块并设置一个通用标签:

You create a chunk like this and set a generic tag:

Chunk chunk = new Chunk(" This chunk needs to be inside a rectangle ");
chunk.setGenericTag("");

你可以将这个作为一部分段落

You can make this chunk part of a Paragraph:

Paragraph p = new Paragraph("[Before]");
p.add(chunk);
p.add("[After]");

这只有在您声明如下页面事件时才有效:

This will only work if you declare a page event like this:

PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
writer.setPageEvent(new DrawRectangle());

当然,我们需要实现 DrawRectangle class;

Of course, we need to implement the DrawRectangle class;

public class DrawRectangle extends PdfPageEventHelper {

    @Override
    public void onGenericTag(PdfWriter writer, Document document, Rectangle rect, String text) {
        PdfContentByte canvas = writer.getDirectContent();
        canvas.rectangle(rect.getLeft(), rect.getBottom(), rect.getWidth(), rect.getHeight());
        canvas.stroke();
        canvas.restoreState();
    }
}

如需更多灵感,请查看 MovieYears 示例取自我的书。它在特定位置添加省略号和影片剪辑: movie_years.pdf

For more inspiration, take a look at the MovieYears example taken from my book. It adds ellipses and movie clips at specific places: movie_years.pdf

在这种情况下, onGenericTag()方法创建如下:

In this case, the onGenericTag() method is created like this:

class GenericTags extends PdfPageEventHelper {

    public void onGenericTag(PdfWriter writer, Document pdfDocument,
            Rectangle rect, String text) {
        if ("strip".equals(text))
            strip(writer.getDirectContent(), rect);
        else if ("ellipse".equals(text))
            ellipse(writer.getDirectContentUnder(), rect);
        else
            countYear(text);
    }

    public void strip(PdfContentByte content, Rectangle rect) {
        content.rectangle(rect.getLeft() - 1, rect.getBottom() - 5f,
                rect.getWidth(), rect.getHeight() + 8);
        content.rectangle(rect.getLeft(), rect.getBottom() - 2,
                rect.getWidth() - 2, rect.getHeight() + 2);
        float y1 = rect.getTop() + 0.5f;
        float y2 = rect.getBottom() - 4;
        for (float f = rect.getLeft(); f < rect.getRight() - 4; f += 5) {
            content.rectangle(f, y1, 4f, 1.5f);
            content.rectangle(f, y2, 4f, 1.5f);
        }
        content.eoFill();
    }

    public void ellipse(PdfContentByte content, Rectangle rect) {
        content.saveState();
        content.setRGBColorFill(0x00, 0x00, 0xFF);
        content.ellipse(rect.getLeft() - 3f, rect.getBottom() - 5f,
                rect.getRight() + 3f, rect.getTop() + 3f);
        content.fill();
        content.restoreState();
    }

    TreeMap<String, Integer> years = new TreeMap<String, Integer>();

    public void countYear(String text) {
        Integer count = years.get(text);
        if (count == null) {
            years.put(text, 1);
        }
        else {
            years.put(text, count + 1);
        }
    }
}

如您所见,我们检查传递给 setGenericTag()方法的值。另外:我们并不总是使用该方法来绘制某些东西。在这种情况下,我们还使用通用标记功能来计算事物。

As you can see, we check the value that was passed to the setGenericTag() method here. Also: we don't always use the method to draw something. In this case, we also use the generic tag functionality to count things.

这篇关于表在同一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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