Columns

Files: 
/**
 * Example written for JavaOne 2016.
 * Differences between iText 5 and iText 7 are discussed in the JavaOne talk
 * "Oops, I broke my API" by Raf Hens and Bruno Lowagie.
 * This is the iText 5 version of one of the examples.
 */
package javaone.edition16;
 
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Font.FontFamily;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import sandbox.WrapToTest;
 
@WrapToTest
public class Text2PdfColumns {
 
    public static final String TEXT
        = "resources/text/jekyll_hyde.txt";
    public static final String DEST
        = "results/javaone/edition16/text2pdf_columns.pdf";
 
    public static void main(String[] args)
    	throws DocumentException, IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
    	new Text2PdfColumns().createPdf(DEST);
    }
 
    public void createPdf(String dest)
	throws DocumentException, IOException {
        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();
        ColumnText ct = new ColumnText(writer.getDirectContent());
        BufferedReader br = new BufferedReader(new FileReader(TEXT));
        String line;
        Paragraph p;
        Font normal = new Font(FontFamily.TIMES_ROMAN, 12);
        Font bold = new Font(FontFamily.TIMES_ROMAN, 12, Font.BOLD);
        boolean title = true;
        while ((line = br.readLine()) != null) {
            p = new Paragraph(line, title ? bold : normal);
            p.setAlignment(Element.ALIGN_JUSTIFIED);
            title = line.isEmpty();
            ct.addElement(p);
        }
        Rectangle[] columns = {
            new Rectangle(36, 36, 290, 806), new Rectangle(305, 36, 559, 806)
        };
        int c = 0;
        int status = ColumnText.START_COLUMN;
        while (ColumnText.hasMoreText(status)) {
            ct.setSimpleColumn(columns[c]);
            status = ct.go();
            if (++c == 2) {
                document.newPage();
                c = 0;
            }
        }
        document.close();
    }
}
/**
 * Example written for JavaOne 2016.
 * Differences between iText 5 and iText 7 are discussed in the JavaOne talk
 * "Oops, I broke my API" by Raf Hens and Bruno Lowagie.
 * This is the iText 7 version of one of the examples.
 */
package com.itextpdf.sandbox.javaone16;
 
import com.itextpdf.io.font.FontConstants;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.ColumnDocumentRenderer;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.property.TextAlignment;
import com.itextpdf.test.annotations.WrapToTest;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
 
@WrapToTest
public class Text2PdfColumns {
 
    public static final String TEXT
        = "src/main/resources/txt/jekyll_hyde.txt";
    public static final String DEST
        = "results/javaone16/text2pdf_columns.pdf";
 
    public static void main(String[] args) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
    	new Text2PdfColumns().createPdf(DEST);
    }
 
    public void createPdf(String dest) throws IOException {
        PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
        Document document = new Document(pdf)
            .setTextAlignment(TextAlignment.JUSTIFIED);
        Rectangle[] columns = {
            new Rectangle(36, 36, 254, 770),
            new Rectangle(305, 36, 254, 770)};
        document.setRenderer(new ColumnDocumentRenderer(document, columns));
        BufferedReader br = new BufferedReader(new FileReader(TEXT));
        String line;
        PdfFont normal = PdfFontFactory.createFont(FontConstants.TIMES_ROMAN);
        PdfFont bold = PdfFontFactory.createFont(FontConstants.TIMES_BOLD);
        boolean title = true;
        while ((line = br.readLine()) != null) {
            document.add(new Paragraph(line).setFont(title ? bold : normal));
            title = line.isEmpty();
        }
        document.close();
    }
}
File nameRaw URLUpdated
Text2PdfColumns.javaText2PdfColumns.java2016-09-02 11:25 am
Text2PdfColumns.javaText2PdfColumns.java2016-09-02 11:35 am
Resources: 
File nameRaw URLUpdated
jekyll_hyde.txtjekyll_hyde.txt2016-09-02 11:10 am
jekyll_hyde.txtjekyll_hyde.txt2016-09-02 11:18 am
Results: 
File nameRaw URLUpdated
cmp_text2pdf_columns.pdfcmp_text2pdf_columns.pdf2016-09-02 11:36 am
cmp_text2pdf_columns.pdfcmp_text2pdf_columns.pdf2016-09-02 11:37 am