001: /**
002: * Copyright (c) 2004, 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:
036: import org.pdfbox.pdmodel.PDDocument;
037: import org.pdfbox.pdmodel.PDPage;
038:
039: import org.pdfbox.pdmodel.edit.PDPageContentStream;
040:
041: import org.pdfbox.pdmodel.font.PDFont;
042: import org.pdfbox.pdmodel.font.PDType1Font;
043:
044: /**
045: * This is an example that creates a simple document.
046: *
047: * The example is taken from the pdf file format specification.
048: *
049: * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
050: * @version $Revision: 1.6 $
051: */
052: public class HelloWorld {
053: /**
054: * Constructor.
055: */
056: public HelloWorld() {
057: super ();
058: }
059:
060: /**
061: * create the second sample document from the PDF file format specification.
062: *
063: * @param file The file to write the PDF to.
064: * @param message The message to write in the file.
065: *
066: * @throws IOException If there is an error writing the data.
067: * @throws COSVisitorException If there is an error writing the PDF.
068: */
069: public void doIt(String file, String message) throws IOException,
070: COSVisitorException {
071: // the document
072: PDDocument doc = null;
073: try {
074: doc = new PDDocument();
075:
076: PDPage page = new PDPage();
077: doc.addPage(page);
078: PDFont font = PDType1Font.HELVETICA_BOLD;
079:
080: PDPageContentStream contentStream = new PDPageContentStream(
081: doc, page);
082: contentStream.beginText();
083: contentStream.setFont(font, 12);
084: contentStream.moveTextPositionByAmount(100, 700);
085: contentStream.drawString(message);
086: contentStream.endText();
087: contentStream.close();
088: doc.save(file);
089: } finally {
090: if (doc != null) {
091: doc.close();
092: }
093: }
094: }
095:
096: /**
097: * This will create a hello world PDF document.
098: * <br />
099: * see usage() for commandline
100: *
101: * @param args Command line arguments.
102: */
103: public static void main(String[] args) {
104: HelloWorld app = new HelloWorld();
105: try {
106: if (args.length != 2) {
107: app.usage();
108: } else {
109: app.doIt(args[0], args[1]);
110: }
111: } catch (Exception e) {
112: e.printStackTrace();
113: }
114: }
115:
116: /**
117: * This will print out a message telling how to use this example.
118: */
119: private void usage() {
120: System.err.println("usage: " + this .getClass().getName()
121: + " <output-file> <Message>");
122: }
123: }
|