KickJava   Java API By Example, From Geeks To Geeks.

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


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
32 package org.pdfbox.examples.pdmodel;
33
34 import java.io.IOException JavaDoc;
35
36 import org.pdfbox.exceptions.COSVisitorException;
37 import org.pdfbox.pdmodel.PDDocument;
38 import org.pdfbox.pdmodel.PDPage;
39 import org.pdfbox.pdmodel.edit.PDPageContentStream;
40 import org.pdfbox.pdmodel.font.PDFont;
41 import org.pdfbox.pdmodel.font.PDType1AfmPfbFont;
42
43 /**
44  * This is an example that creates a simple document
45  * with a Type 1 font (afm + pfb).
46  *
47  * @author <a HREF="mailto:m.g.n@gmx.de">Michael Niedermair</a>
48  * @version $Revision: 1.2 $
49  */

50 public class HelloWorldType1AfmPfb
51 {
52
53     /**
54      * create the second sample document from the PDF file format specification.
55      *
56      * @param file The file to write the PDF to.
57      * @param message The message to write in the file.
58      * @param fontfile The ttf-font file.
59      *
60      * @throws IOException If there is an error writing the data.
61      * @throws COSVisitorException If there is an error writing the PDF.
62      */

63     public void doIt(final String JavaDoc file, final String JavaDoc message,
64             final String JavaDoc fontfile) throws IOException JavaDoc, COSVisitorException
65     {
66
67         // the document
68
PDDocument doc = null;
69         try
70         {
71             doc = new PDDocument();
72
73             PDPage page = new PDPage();
74             doc.addPage(page);
75             PDFont font = new PDType1AfmPfbFont(doc,fontfile);
76
77             PDPageContentStream contentStream = new PDPageContentStream(doc,
78                     page);
79             contentStream.beginText();
80             contentStream.setFont(font, 12);
81             contentStream.moveTextPositionByAmount(100, 700);
82             contentStream.drawString(message);
83             contentStream.endText();
84             contentStream.close();
85             doc.save(file);
86             System.out.println(file + " created!");
87         }
88         finally
89         {
90             if (doc != null)
91             {
92                 doc.close();
93             }
94         }
95     }
96
97     /**
98      * This will create a hello world PDF document
99      * with a ttf-font.
100      * <br />
101      * see usage() for commandline
102      *
103      * @param args Command line arguments.
104      */

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

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