Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to skip text insertion point to the next column using iText?

Tags:

java

layout

itext

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.

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?

Thanks for answers!

The image below shows what I want it. columns layout

UPDATE:

Ok I'll try to rephrase the question.

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

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.

I.e.

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

enter image description here

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.

enter image description here

like image 705
burjulius Avatar asked Oct 07 '22 19:10

burjulius


1 Answers

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.

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... enter image description here

like image 121
Bruno Lowagie Avatar answered Oct 10 '22 17:10

Bruno Lowagie