Table on same line

末鹿安然 提交于 2019-12-13 05:53:22

问题


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 ?

Something like that :

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

is it possible ?


回答1:


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

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());

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();
    }
}

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

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);
        }
    }
}

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.



来源:https://stackoverflow.com/questions/32837498/table-on-same-line

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!