Package com.lowagie.examples.objects.columns

Source Code of com.lowagie.examples.objects.columns.MultiColumnR2L

/*
* $Id$
*
* This code is part of the 'iText Tutorial'.
* You can find the complete tutorial at the following address:
* http://itextdocs.lowagie.com/tutorial/
*
* This code is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* itext-questions@lists.sourceforge.net
*/
package com.lowagie.examples.objects.columns;

import java.awt.Color;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Random;

import com.lowagie.text.LwgDocument;
import com.lowagie.text.DocumentException;
import com.lowagie.text.LwgElement;
import com.lowagie.text.LwgFont;
import com.lowagie.text.FontFactory;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.MultiColumnText;
import com.lowagie.text.pdf.PdfWriter;

/**
* An example using MultiColumnText with irregular columns.
*/
public class MultiColumnR2L {

    /**
     * An example using MultiColumnText with irregular columns.
     * @param args no arguments needed
     */
    public static void main(String[] args) {
       
        System.out.println("MultiColumnText Right to Left");
        try {
            LwgDocument document = new LwgDocument();
            OutputStream out = new FileOutputStream("multicolumnR2L.pdf");
            PdfWriter.getInstance(document, out);
            document.open();


            MultiColumnText mct = new MultiColumnText();
            mct.setColumnsRightToLeft(true);

            // set up 3 even columns with 10pt space between
            mct.addRegularColumns(document.left(), document.right(), 10f, 3);

            // Write some iText poems
            for (int i = 0; i < 30; i++) {
              mct.addElement(new Paragraph(String.valueOf(i + 1)));
                mct.addElement(newPara(randomWord(noun), LwgElement.ALIGN_CENTER, LwgFont.BOLDITALIC));
                for (int j = 0; j < 4; j++) {
                    mct.addElement(newPara(poemLine(), LwgElement.ALIGN_LEFT, LwgFont.NORMAL));
                }
                mct.addElement(newPara(randomWord(adverb), LwgElement.ALIGN_LEFT, LwgFont.NORMAL));
                mct.addElement(newPara("\n\n", LwgElement.ALIGN_LEFT, LwgFont.NORMAL));
            }

            document.add(mct);
            document.close();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

    }

    private static LwgElement newPara(String text, int alignment, int type) {
       LwgFont font = FontFactory.getFont("Helvetica", 10, type, Color.BLACK);
       Paragraph p = new Paragraph(text, font);
       p.setAlignment(alignment);
       p.setLeading(font.getSize() * 1.2f);
       return p;
    }

    static Random rand = new Random();


    static String[] verb = {"flows", "draws", "renders", "throws exception", "runs",
        "crashes", "downloads", "usurps", "vexes", "whispers", "boils",
        "capitulates", "crashes", "craves", "looks", "defies", "defers",
        "defines", "envelops", "entombs", "falls", "fails", "halts",
        "appears", "nags", "overflows", "burns", "dies", "writes",
        "flushes"};

    static String[] noun = {"ColumnText", "paragraph", "phrase", "chunk", "PdfContentByte",
        "PdfPTable", "iText", "color", "vertical alignment", "horizontal alignment", "PdfWriter",
        "ListItem", "PdfStamper", "PDF", "HTML", "XML", "column", "font",
        "table", "FDF", "field", "NullPointerException", "CJK font"};

    static String[] adjective = {"foul", "broken", "gray", "slow", "beautiful",
       "throbbing", "sharp", "stout", "soundless", "neat",
       "swift", "uniform", "upright", "vibrant", "dingy",
       "vestigal", "messy", "sloppy", "baleful", "boastful",
       "dark", "capricious", "concrete", "deliberate", "sharp",
        "drunken", "undisciplined", "perfect", "bloated"};

    static String[] adverb = {"randomly", "quickly", "triumphantly", "suggestively",
       "slowly", "angrily", "uncomfortably", "finally", "unexpectedly",
       "hysterically", "thinly", "dryly", "blazingly",
       "terribly", "bleakly", "irritably", "dazzlingly", "expectantly",
       "impersonally", "abruptly", "awfully", "caressingly", "completely",
       "undesirably", "drolly", "hypocritically", "blankly",
       "dimly"};

    private static String randomWord(String[] type)
    {
       return type[rand.nextInt(type.length)];
    }

    /**
     * Generates a random poem line.
     * @return a poem that is generated with some keywords.
     */
    public static String poemLine()
    {
       StringBuffer results = new StringBuffer(150);
       results.append(randomWord(adjective));
       results.append(' ');
       results.append(randomWord(noun));
       results.append(' ');
       results.append(randomWord(verb));
       results.append(' ');
       results.append(randomWord(adverb));
       results.append(", ");
       return results.toString();
}

}
TOP

Related Classes of com.lowagie.examples.objects.columns.MultiColumnR2L

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.