KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > LayoutExample


1 import org.faceless.pdf2.*;
2 import java.io.*;
3 import java.awt.Color JavaDoc;
4 import java.util.*;
5
6 // An example demonstrating how to use the LayoutBox class for
7
// advanced combinations of text and graphics.
8
//
9
// $Id: LayoutExample.java,v 1.2 2003/11/03 10:56:14 mike Exp $
10
//
11
public class LayoutExample
12 {
13     static String JavaDoc text1 = "orem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.";
14
15
16     public static void main(String JavaDoc[] args)
17         throws IOException
18     {
19     // Start things off - create a PDF, a page, a style to draw in,
20
// and load an image to use in the demonstration
21
//
22
PDF pdf = new PDF();
23     PDFPage page = pdf.newPage(PDF.PAGESIZE_A4);
24
25     PDFStyle style = new PDFStyle();
26     style.setTextAlign(PDFStyle.TEXTALIGN_JUSTIFY + PDFStyle.TEXTALIGN_BOTTOM);
27     style.setFont(new StandardFont(StandardFont.TIMES), 9);
28     style.setFillColor(Color.black);
29
30     PDFImage image = new PDFImage(new FileInputStream("resources/L.gif"));
31     LayoutBox.Box imagebox=null;
32
33
34     // Create the LayoutBox - the width of the page less 200 points.
35
//
36
LayoutBox flow = new LayoutBox(page.getWidth()-200);
37     flow.setStyle(style);
38
39     // Add the first block - create an image "floating" on the left, which
40
// will cause all further text to wrap around the image.
41
//
42
imagebox = flow.addBoxLeft(image.getWidth()/3, image.getHeight()/3, LayoutBox.CLEAR_LEFT);
43     imagebox.setImage(image);
44     flow.addText(text1, style, Locale.getDefault());
45     flow.addLineBreak(style);
46     flow.addLineBreak(style);
47
48     // Add the second block. Place the first image "inline", so it's treated as part
49
// of the text. Because the text style has the TEXTALIGN_BOTTOM alignment, the
50
// text will be aligned so that the bottom of the text is flush with the bottom of
51
// the line (and therefore, the image).
52
//
53
imagebox = flow.addBoxInline(image.getWidth()/3, image.getHeight()/3, 0);
54     imagebox.setImage(image);
55     flow.addText(text1, style, Locale.getDefault());
56     //
57
// Add another image inline so you can see more clearly how this works.
58
//
59
imagebox = flow.addBoxInline(9, 9, 0);
60     imagebox.setImage(image);
61     flow.addText(text1, style, Locale.getDefault());
62
63     // Draw the LayoutBox on the page, then draw a red outline around the whole box
64
//
65
int top = (int)page.getHeight()-50;
66     page.drawLayoutBox(flow, 100, top);
67     style.setLineColor(Color.red);
68     style.setFillColor(null);
69     page.setStyle(style);
70     page.drawRectangle(100, top, page.getWidth()-100, top-flow.getHeight());
71
72     pdf.render(new FileOutputStream("LayoutExample.pdf"));
73     }
74 }
75
Popular Tags