2014年10月25日 星期六

itext-Chunk itext-concat

http://tutorials.jenkov.com/java-itext/chunk.html


import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;

public class DocumentExample {

    public static void main(String[] args) {

        Document document = new Document();

        try {
            PdfWriter.getInstance(document,
                    new FileOutputStream("Chunk.pdf"));

            document.open();
            document.add(new Chunk("This is sentence 1. "));
            document.add(new Chunk("This is sentence 2. "));
            document.add(new Chunk("This is sentence 3. "));
            document.add(new Chunk("This is sentence 4. "));
            document.add(new Chunk("This is sentence 5. "));
            document.add(new Chunk("This is sentence 6. "));
            document.close();

        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

    }
}
Here is what the generated document looks like:
An IText Chunk example
An IText Chunk example
Notice how sentence 1 and sentence 6 are printed ontop of each other. The IText Chunk object does not add line breaks, paragraph spacing or any other kind of spacing. It just adds the raw text at the next available location, left to right. When it reaches the right edge of the document, it restarts from the left edge, at the same Y location (same height).

沒有留言:

張貼留言