MainClass.java Source code

Java tutorial

Introduction

Here is the source code for MainClass.java

Source

import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.FileReader;

import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.PageSize;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.ColumnText;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;

public class MainClass {
    public static void main(String[] args) throws Exception {
        Document document = new Document(PageSize.A4);
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
        document.open();
        PdfContentByte cb = writer.getDirectContent();
        StringBuffer sb = new StringBuffer(1024);
        BufferedReader reader = new BufferedReader(new FileReader("a.txt"));
        int c;
        while ((c = reader.read()) > -1) {
            sb.append((char) c);
        }
        reader.close();
        ColumnText ct = new ColumnText(cb);
        ct.setSimpleColumn(new Phrase(sb.toString()), 36, 36, PageSize.A4.width() - 36, PageSize.A4.height() - 36,
                18, Element.ALIGN_JUSTIFIED);
        int status = ColumnText.START_COLUMN;
        while (ColumnText.hasMoreText(status)) {
            status = ct.go();
            ct.setYLine(PageSize.A4.height() - 36);
            document.newPage();
        }
        document.close();
    }
}