KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pdfbox > examples > pdmodel > ShowColorBoxes


1 /**
2  * Copyright (c) 2004, www.pdfbox.org
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  * 3. Neither the name of pdfbox; nor the names of its
14  * contributors may be used to endorse or promote products derived from this
15  * software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * http://www.pdfbox.org
29  *
30  */

31 package org.pdfbox.examples.pdmodel;
32
33 import java.awt.Color JavaDoc;
34 import java.io.IOException JavaDoc;
35
36 import org.pdfbox.exceptions.COSVisitorException;
37
38 import org.pdfbox.pdmodel.PDDocument;
39 import org.pdfbox.pdmodel.PDPage;
40
41 import org.pdfbox.pdmodel.edit.PDPageContentStream;
42
43 /**
44  * This is an example that creates a simple document.
45  *
46  * The example is taken from the pdf file format specification.
47  *
48  * @author <a HREF="mailto:ben@benlitchfield.com">Ben Litchfield</a>
49  * @version $Revision: 1.2 $
50  */

51 public class ShowColorBoxes
52 {
53     /**
54      * Constructor.
55      */

56     public ShowColorBoxes()
57     {
58         super();
59     }
60
61     /**
62      * create the second sample document from the PDF file format specification.
63      *
64      * @param file The file to write the PDF to.
65      *
66      * @throws IOException If there is an error writing the data.
67      * @throws COSVisitorException If there is an error writing the PDF.
68      */

69     public void doIt( String JavaDoc file) throws IOException JavaDoc, COSVisitorException
70     {
71         // the document
72
PDDocument doc = null;
73         try
74         {
75             doc = new PDDocument();
76             
77             PDPage page = new PDPage();
78             doc.addPage( page );
79             
80             PDPageContentStream contentStream = new PDPageContentStream(doc, page);
81             //first fill the entire background with cyan
82
contentStream.setNonStrokingColor( Color.CYAN );
83             contentStream.fillRect( 0,0, page.findMediaBox().getWidth(), page.findMediaBox().getHeight() );
84             
85             //then draw a red box in the lower left hand corner
86
contentStream.setNonStrokingColor( Color.RED );
87             contentStream.fillRect( 10, 10, 100, 100 );
88             
89             contentStream.close();
90             doc.save( file );
91         }
92         finally
93         {
94             if( doc != null )
95             {
96                 doc.close();
97             }
98         }
99     }
100
101     /**
102      * This will create a hello world PDF document.
103      * <br />
104      * see usage() for commandline
105      *
106      * @param args Command line arguments.
107      */

108     public static void main(String JavaDoc[] args)
109     {
110         ShowColorBoxes app = new ShowColorBoxes();
111         try
112         {
113             if( args.length != 1 )
114             {
115                 app.usage();
116             }
117             else
118             {
119                 app.doIt( args[0] );
120             }
121         }
122         catch (Exception JavaDoc e)
123         {
124             e.printStackTrace();
125         }
126     }
127
128     /**
129      * This will print out a message telling how to use this example.
130      */

131     private void usage()
132     {
133         System.err.println( "usage: " + this.getClass().getName() + " <output-file>" );
134     }
135 }
Popular Tags