KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * Copyright (c) 2005, 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.io.IOException JavaDoc;
34 import java.util.List 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.common.PDRectangle;
42 import org.pdfbox.pdmodel.edit.PDPageContentStream;
43
44 import org.pdfbox.pdmodel.font.PDFont;
45 import org.pdfbox.pdmodel.font.PDType1Font;
46
47
48 /**
49  * This is an example of how to add a message to every page
50  * in a pdf document.
51  *
52  * @author <a HREF="mailto:ben@benlitchfield.com">Ben Litchfield</a>
53  * @version $Revision: 1.3 $
54  */

55 public class AddMessageToEachPage
56 {
57     /**
58      * Constructor.
59      */

60     public AddMessageToEachPage()
61     {
62         super();
63     }
64
65     /**
66      * create the second sample document from the PDF file format specification.
67      *
68      * @param file The file to write the PDF to.
69      * @param message The message to write in the file.
70      * @param outfile The resulting PDF.
71      *
72      * @throws IOException If there is an error writing the data.
73      * @throws COSVisitorException If there is an error writing the PDF.
74      */

75     public void doIt( String JavaDoc file, String JavaDoc message, String JavaDoc outfile ) throws IOException JavaDoc, COSVisitorException
76     {
77         // the document
78
PDDocument doc = null;
79         try
80         {
81             doc = PDDocument.load( file );
82             
83             List JavaDoc allPages = doc.getDocumentCatalog().getAllPages();
84             PDFont font = PDType1Font.HELVETICA_BOLD;
85             float fontSize = 12.0f;
86             
87             for( int i=0; i<allPages.size(); i++ )
88             {
89                 PDPage page = (PDPage)allPages.get( i );
90                 PDRectangle pageSize = page.findMediaBox();
91                 float stringWidth = font.getStringWidth( message );
92                 float centeredPosition = (pageSize.getWidth() - (stringWidth*fontSize)/1000f)/2f;
93                 PDPageContentStream contentStream = new PDPageContentStream(doc, page, true, true);
94                 contentStream.beginText();
95                 contentStream.setFont( font, fontSize );
96                 contentStream.moveTextPositionByAmount( centeredPosition, 30 );
97                 contentStream.drawString( message );
98                 contentStream.endText();
99                 contentStream.close();
100             }
101             
102             
103             doc.save( outfile );
104         }
105         finally
106         {
107             if( doc != null )
108             {
109                 doc.close();
110             }
111         }
112     }
113
114     /**
115      * This will create a hello world PDF document.
116      * <br />
117      * see usage() for commandline
118      *
119      * @param args Command line arguments.
120      */

121     public static void main(String JavaDoc[] args)
122     {
123         AddMessageToEachPage app = new AddMessageToEachPage();
124         try
125         {
126             if( args.length != 3 )
127             {
128                 app.usage();
129             }
130             else
131             {
132                 app.doIt( args[0], args[1], args[2] );
133             }
134         }
135         catch (Exception JavaDoc e)
136         {
137             e.printStackTrace();
138         }
139     }
140
141     /**
142      * This will print out a message telling how to use this example.
143      */

144     private void usage()
145     {
146         System.err.println( "usage: " + this.getClass().getName() + " <input-file> <Message> <output-file>" );
147     }
148 }
Popular Tags