001: /**
002: * Copyright (c) 2005-2006, 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.File;
033: import java.io.FileInputStream;
034: import java.io.IOException;
035:
036: import org.pdfbox.exceptions.COSVisitorException;
037: import org.pdfbox.io.RandomAccessFile;
038:
039: import org.pdfbox.pdmodel.PDDocument;
040: import org.pdfbox.pdmodel.PDPage;
041:
042: import org.pdfbox.pdmodel.edit.PDPageContentStream;
043:
044: import org.pdfbox.pdmodel.graphics.xobject.PDCcitt;
045: import org.pdfbox.pdmodel.graphics.xobject.PDJpeg;
046: import org.pdfbox.pdmodel.graphics.xobject.PDXObjectImage;
047:
048: /**
049: * This is an example that creates a simple document.
050: *
051: * The example is taken from the pdf file format specification.
052: *
053: * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
054: * @version $Revision: 1.7 $
055: */
056: public class ImageToPDF {
057:
058: /**
059: * create the second sample document from the PDF file format specification.
060: *
061: * @param file The file to write the PDF to.
062: * @param image The filename of the image to put in the PDF.
063: *
064: * @throws IOException If there is an error writing the data.
065: * @throws COSVisitorException If there is an error writing the PDF.
066: */
067: public void createPDFFromImage(String file, String image)
068: throws IOException, COSVisitorException {
069: // the document
070: PDDocument doc = null;
071: try {
072: doc = new PDDocument();
073:
074: PDPage page = new PDPage();
075: doc.addPage(page);
076:
077: PDXObjectImage ximage = null;
078: if (image.toLowerCase().endsWith(".jpg")) {
079: ximage = new PDJpeg(doc, new FileInputStream(image));
080: } else if (image.toLowerCase().endsWith(".tif")
081: || image.toLowerCase().endsWith(".tiff")) {
082: ximage = new PDCcitt(doc, new RandomAccessFile(
083: new File(image), "r"));
084: } else {
085: //BufferedImage awtImage = ImageIO.read( new File( image ) );
086: //ximage = new PDPixelMap(doc, awtImage);
087: throw new IOException("Image type not supported:"
088: + image);
089: }
090: PDPageContentStream contentStream = new PDPageContentStream(
091: doc, page);
092:
093: contentStream.drawImage(ximage, 20, 20);
094:
095: contentStream.close();
096: doc.save(file);
097: } finally {
098: if (doc != null) {
099: doc.close();
100: }
101: }
102: }
103:
104: /**
105: * This will create a PDF document with a single image on it.
106: * <br />
107: * see usage() for commandline
108: *
109: * @param args Command line arguments.
110: */
111: public static void main(String[] args) {
112: ImageToPDF app = new ImageToPDF();
113: try {
114: if (args.length != 2) {
115: app.usage();
116: } else {
117: app.createPDFFromImage(args[0], args[1]);
118: }
119: } catch (Exception e) {
120: e.printStackTrace();
121: }
122: }
123:
124: /**
125: * This will print out a message telling how to use this example.
126: */
127: private void usage() {
128: System.err.println("usage: " + this .getClass().getName()
129: + " <output-file> <image>");
130: }
131: }
|