KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Images


1 import java.util.*;
2 import java.io.*;
3 import java.awt.Color JavaDoc;
4 import org.faceless.pdf2.*;
5
6 /**
7  * The Images example demonstrates inserting images into a document.
8  * We insert a GIF image, two JPEG's and a PNG onto the page.
9  *
10  */

11 public class Images
12 {
13     public static void main(String JavaDoc[] args)
14         throws IOException
15     {
16     // Create a new PDF document.
17
//
18
PDF pdf = new PDF();
19
20     // Create a new A4 Landscape page - 842 points wide by 595 points high
21
//
22
PDFPage page = pdf.newPage(PDF.PAGESIZE_A4_LANDSCAPE);
23
24
25     // To make layout easier, store the width and height of the
26
// page in a couple of variables, and have the whole page and measured in points
27
// from the top-left of the page.
28
//
29
float width = page.getWidth();
30     float height = page.getHeight();
31     page.setUnits(PDFPage.UNITS_POINTS, PDFPage.ORIGIN_PAGETOP);
32
33
34     // Draw a light blue rectangle filling most of the page.
35
//
36
PDFStyle background = new PDFStyle();
37     background.setFillColor(new Color JavaDoc(208,231,240));
38     page.setStyle(background);
39     page.drawRectangle(100, 100, width-100, height-100);
40
41
42     // Load the images.
43
//
44
// We're loading the images from files here, but there is no
45
// reason whey they couldn't be loaded from a ByteArrayInputStream
46
// or any other stream.
47
//
48
PDFImage map = new PDFImage(new FileInputStream("resources/images/africa.gif"));
49     PDFImage sand = new PDFImage(new FileInputStream("resources/images/sanddune.jpg"));
50     PDFImage canoe = new PDFImage(new FileInputStream("resources/images/canoe.jpg"));
51     PDFImage logo = new PDFImage(new FileInputStream("resources/images/logosmall.png"));
52
53
54     // Draw the images.
55
//
56
// Although we are specifying the upper-left and lower-right hand
57
// corners for each images' rectangle, any two opposite corners
58
// would do.
59
//
60
// We're blatently ignoring the image aspect ratios.
61
//
62

63     // Draw the first image (the map, a GIF image) with the top right
64
// corner 100 pixels in from the top-right hand corner of the page
65
//
66
page.drawImage(map, width-430, 100, width-100, height-100);
67
68     // Draw the next two images, both JPEGs. Put them above eachother
69
// 120 pixels in from the left of the page (just inside the blue
70
// rectangle).
71
//
72
page.drawImage(canoe, 120, 120, 350, 260);
73     page.drawImage(sand, 120, 315, 350, 455);
74
75     // Draw the last image - our logo, a PNG image - near the
76
// bottom of the page. Although this appears as the smallest
77
// image on the page, it's actually the highest resolution - you
78
// will notice the difference when you zoom in on the images.
79
//
80
page.drawImage(logo, width-220, height, width-100, height-100);
81
82
83
84     // Draw a black box around the blue rectangle. Do this after
85
// the images. If we did it before we drew the imagse, they
86
// would overwrite part of the line.
87
//
88
PDFStyle borderStyle = new PDFStyle();
89     borderStyle.setLineColor(Color.black);
90     page.setStyle(borderStyle);
91     page.drawRectangle(100, 100, width-100, height-100);
92
93
94
95     // Add some text to complete the document.
96
//
97
// We're not doing anything clever with fonts in this
98
// example, just using the built in Times-Roman and
99
// Helvetica-Oblique
100
//
101

102     // Create three new styles to write in.
103
//
104
// 1. "footer" - 8 point Helvetica Oblique, Right aligned
105
// 2. "header" - 24 point Times Roman
106
// 3. "caption" - 12 point Times Roman
107
//
108
PDFStyle footer = new PDFStyle();
109     footer.setFont(new StandardFont(StandardFont.HELVETICAOBLIQUE), 8);
110     footer.setFillColor(Color.black);
111     footer.setTextAlign(PDFStyle.TEXTALIGN_RIGHT);
112
113     PDFStyle caption = new PDFStyle();
114     caption.setFont(new StandardFont(StandardFont.TIMES), 12);
115     caption.setFillColor(Color.black);
116
117     PDFStyle heading = new PDFStyle();
118     heading.setFont(new StandardFont(StandardFont.TIMES), 24);
119     heading.setFillColor(Color.black);
120
121     // Now we can actually draw the text
122
//
123
page.setStyle(heading);
124     page.drawText("Some Photos from Africa", 100, 70);
125
126     page.setStyle(caption);
127     page.drawText("An example showing two JPEG images, a GIF and a PNG image", 100, 90);
128     page.drawText("A fishing boat on the beach - Western Ghana", 120, 280);
129     page.drawText("The largest sand dunes in the world - Namibia", 120, 475);
130
131     page.setStyle(footer);
132     page.drawText("Document created using the Big Faceless PDF Library - http://big.faceless.org/products/pdf", width-240, height-45);
133
134
135     //----------------------------------------------------------------
136
//
137
// That's it! We've created the contents of the document. Now
138
// to finish off we'll add some document meta-information and
139
// set the page to resize when the document is opened.
140
//
141
//----------------------------------------------------------------
142

143
144     // Add some document info.
145
//
146
pdf.setInfo("Author", "Big Faceless Organization, Inc.");
147     pdf.setInfo("Title", "Some Photos from Africa");
148     pdf.setInfo("Subject", "A feeble excuse to parade two of Mikes better holiday snaps");
149     pdf.setAction(Event.OPEN, PDFAction.goToFit(page));
150
151     // Write the document to a file
152
//
153
OutputStream fo = new FileOutputStream("Images.pdf");
154     pdf.render(fo);
155     fo.close();
156     }
157 }
158
Popular Tags