MainClass.java Source code

Java tutorial

Introduction

Here is the source code for MainClass.java

Source

import java.io.FileOutputStream;

import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.MultiColumnText;
import com.lowagie.text.pdf.PdfWriter;

public class MainClass {
    public static void main(String[] args) throws Exception {
        Document document = new Document(PageSize.A4);
        PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));

        document.open();

        MultiColumnText mct = new MultiColumnText();
        mct.addSimpleColumn(100, 280);
        mct.addSimpleColumn(300, 480);

        for (int i = 0; i < 30; i++) {
            mct.addElement(new Paragraph(String.valueOf(i + 1)));
            mct.addElement(newParagraph("noun", Element.ALIGN_CENTER, Font.BOLDITALIC));
            for (int j = 0; j < 4; j++) {
                mct.addElement(newParagraph("Line", Element.ALIGN_LEFT, Font.NORMAL));
            }
            mct.addElement(newParagraph("adverb", Element.ALIGN_LEFT, Font.NORMAL));
            mct.addElement(newParagraph("\n\n", Element.ALIGN_LEFT, Font.NORMAL));
        }
        document.add(mct);
        document.close();
        document.close();
    }

    private static Element newParagraph(String text, int alignment, int type) {
        Font font = FontFactory.getFont(BaseFont.HELVETICA, 10, type);
        Paragraph p = new Paragraph(text, font);
        p.setAlignment(alignment);
        p.setLeading(font.size() * 1.2f);
        return p;
    }

}