如何使用iText跳过文本插入点到下一列? [英] How to skip text insertion point to the next column using iText?

查看:121
本文介绍了如何使用iText跳过文本插入点到下一列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用iText java lib创建3列布局。

I'm creating the 3 columns layout with iText java lib.

最大的问题是,第一列中的文本可能小于第二列中的文本,或者第二列中的文本小于第三列中的文本。所以我需要将插入点移动到下一列。

The biggest problem is that, that the text in the first column could be less than in the second column or third or less in the second column than third column. So I need to move the insertion point to the next column.

我尝试使用nextColumn方法,该方法应该将插入点移动到
next列,但它会移动所有列都在右侧。

I tried to use nextColumn method which should move insertion point to the next column but it moves all columns to the right side.

也许有人有同样的问题,知道如何做对吗?

Maybe anyone had the same issue and know how to do it right?

谢谢你的回答!

下图显示了我想要的内容。

The image below shows what I want it.

更新:

好的我会尝试重新解释这个问题。

Ok I'll try to rephrase the question.

这是代码我如何声明3列:

Here is the code how I declare 3 columns:

MultiColumnText columnsFooter = new MultiColumnText(210f);
columnsFooter.addRegularColumns(document.left() - 10f,
    document.right(), 0, 3);
columnsFooter.setAlignment(Element.ALIGN_CENTER);

我有3列标题,每列有一些文字。文本取决于用户今天输入的文本数量。如果用户填写文本限制是正常的,因为第一列已填充,第二列文本转到第二列(第二列文本从第二列开始)。

I have 3 columns with headers and some text in each column. The text depends on how much text the user entered today. If the user filled the text limit is OK because the first column is filled and second text goes to second column (second text starts from second column).

但是如果用户没有填写第一列或第二列的文本限制,则下一个列开始写入未完全填充的列。

But if the user don't fill the text limit on the first column or second column the next starts writing from the column which are not fully filled up.

这是可以的,因为第一列和第二列已完全填满。

This is okay because the first and the second columns are fully filled up.

这很糟糕,因为第一列未完全填满,第二列文本从第一列开始。因此,我需要在HEADER 2和3之前添加列分隔符,以便在文本未完全填满时获得良好的布局结构。

This is bad because the first column are not fully filled up and second column text starts from the first column. So I need to add column break before HEADER 2 and 3 to get a good layout structure if the text are not fully filled up.

推荐答案

你能改写吗?这个问题?因为我不明白。什么是插入点?
我写了关于iText的书,我可以做任何我想用ColumnText做的事情,所以你可能有兴趣澄清你想要的东西。

Can you rephrase the question? Because I don't understand it. What is an insertion point? I wrote the book about iText, and I can do pretty much anything I want to do with ColumnText, so it may be in your interest to clarify what you want.

问题仍然不清楚,但图片说了千言万语。我给你做了一个4列和5篇文章的例子:

The question is still unclear, but the picture says a thousand words. I've made you an example with 4 columns and 5 articles:

    import java.io.FileOutputStream;
import java.io.IOException;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfWriter;


public class ColumnTextExample {


    /** Definition of four columns */
    public static final float[][] COLUMNS = {
        { 36, 36, 224, 579 } , { 230, 36, 418, 579 },
        { 424, 36, 612, 579 } , { 618, 36, 806, 579 }
    };

    public static final String ARTICLE1 = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
    public static final String ARTICLE2 = ARTICLE1 + " " + ARTICLE1 + " " + ARTICLE1;
    public static final String ARTICLE3 = ARTICLE1 + " " + ARTICLE1;
public static final String[] ARTICLES = { "HEADER 1\n" + ARTICLE1, "HEADER 2\n" + ARTICLE2, "HEADER 3\n" + ARTICLE3, "HEADER 4\n" + ARTICLE1, "HEADER 5\n" + ARTICLE3 };

    public static void main(String[] args) throws IOException, DocumentException {
        // step 1
        Document document = new Document(PageSize.A4.rotate());
        // step 2
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("columns.pdf"));
        // step 3
        document.open();
        // step 4
        ColumnText ct = new ColumnText(writer.getDirectContent());
        int column = 0;
        ct.setSimpleColumn(
            COLUMNS[column][0], COLUMNS[column][1],
            COLUMNS[column][2], COLUMNS[column][3]);
        int status = ColumnText.START_COLUMN;
        for (String article : ARTICLES) {
            ct.addElement(new Paragraph(article));
            status = ct.go();
            while (ColumnText.hasMoreText(status)) {
                column = nextColumn(document, column, ct);
                status = ct.go();
            }
            column = nextColumn(document, column, ct);
        }
        // step 5
        document.close();
    }

    public static int nextColumn(Document document, int column, ColumnText ct) {
        column = (column + 1) % 4;
        if (column == 0)
            document.newPage();
        ct.setSimpleColumn(
                COLUMNS[column][0], COLUMNS[column][1],
                COLUMNS[column][2], COLUMNS[column][3]);
        return column;
    }
}

第一篇文章适合第一列,剩下一半列打开。我们跳到第二篇文章的下一篇专栏文章。这不适合第二列:它需要一列半。第三篇文章适合第四篇,但我们需要跳到第四篇文章的下一页,依此类推......

The first article fits the first column, leaving half a column open. We skip to the next column for the second article. That doesn't fit the second column: it takes one column and a half. The third article fits the fourth column, but we need to skip to the next page for the fourth article, and so on...

这篇关于如何使用iText跳过文本插入点到下一列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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