001: /**
002: * Copyright (c) 2005, www.pdfbox.org
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions are met:
007: *
008: * 1. Redistributions of source code must retain the above copyright notice,
009: * this list of conditions and the following disclaimer.
010: * 2. Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: * 3. Neither the name of pdfbox; nor the names of its
014: * contributors may be used to endorse or promote products derived from this
015: * software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
018: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
019: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
020: * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
021: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
022: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
023: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
024: * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
026: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027: *
028: * http://www.pdfbox.org
029: *
030: */package org.pdfbox.examples.pdmodel;
031:
032: import java.io.IOException;
033:
034: import org.pdfbox.exceptions.COSVisitorException;
035: import org.pdfbox.pdmodel.PDDocument;
036: import org.pdfbox.pdmodel.PDPage;
037: import org.pdfbox.pdmodel.edit.PDPageContentStream;
038: import org.pdfbox.pdmodel.font.PDFont;
039: import org.pdfbox.pdmodel.font.PDTrueTypeFont;
040:
041: /**
042: * This is an example that creates a simple document
043: * with a ttf-font.
044: *
045: * @author <a href="mailto:m.g.n@gmx.de">Michael Niedermair</a>
046: * @version $Revision: 1.2 $
047: */
048: public class HelloWorldTTF {
049:
050: /**
051: * create the second sample document from the PDF file format specification.
052: *
053: * @param file The file to write the PDF to.
054: * @param message The message to write in the file.
055: * @param fontfile The ttf-font file.
056: *
057: * @throws IOException If there is an error writing the data.
058: * @throws COSVisitorException If there is an error writing the PDF.
059: */
060: public void doIt(final String file, final String message,
061: final String fontfile) throws IOException,
062: COSVisitorException {
063:
064: // the document
065: PDDocument doc = null;
066: try {
067: doc = new PDDocument();
068:
069: PDPage page = new PDPage();
070: doc.addPage(page);
071: PDFont font = PDTrueTypeFont.loadTTF(doc, fontfile);
072:
073: PDPageContentStream contentStream = new PDPageContentStream(
074: doc, page);
075: contentStream.beginText();
076: contentStream.setFont(font, 12);
077: contentStream.moveTextPositionByAmount(100, 700);
078: contentStream.drawString(message);
079: contentStream.endText();
080: contentStream.close();
081: doc.save(file);
082: System.out.println(file + " created!");
083: } finally {
084: if (doc != null) {
085: doc.close();
086: }
087: }
088: }
089:
090: /**
091: * This will create a hello world PDF document
092: * with a ttf-font.
093: * <br />
094: * see usage() for commandline
095: *
096: * @param args Command line arguments.
097: */
098: public static void main(String[] args) {
099:
100: HelloWorldTTF app = new HelloWorldTTF();
101: try {
102: if (args.length != 3) {
103: app.usage();
104: } else {
105: app.doIt(args[0], args[1], args[2]);
106: }
107: } catch (Exception e) {
108: e.printStackTrace();
109: }
110: }
111:
112: /**
113: * This will print out a message telling how to use this example.
114: */
115: private void usage() {
116: System.err.println("usage: " + this .getClass().getName()
117: + " <output-file> <Message> <ttf-file>");
118: }
119: }
|