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.Paragraph;
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();
        BufferedReader reader = new BufferedReader(new FileReader("a.txt"));
        String line;
        Paragraph p;
        float pos;
        while ((line = reader.readLine()) != null) {
            p = new Paragraph("    " + line);
            p.setAlignment(Element.ALIGN_JUSTIFIED);
            document.add(p);
            pos = writer.getVerticalPosition(false);
            cb.moveTo(0, pos);
            cb.lineTo(PageSize.A4.width(), pos);
            cb.stroke();
            if (pos < 90)
                document.newPage();
        }
        reader.close();
        document.close();
    }
}