1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.pdfbox.examples.pdmodel;
18
19 import java.io.File;
20 import java.io.FileInputStream;
21 import java.io.IOException;
22
23 import org.apache.pdfbox.exceptions.COSVisitorException;
24 import org.apache.pdfbox.io.RandomAccessFile;
25
26 import org.apache.pdfbox.pdmodel.PDDocument;
27 import org.apache.pdfbox.pdmodel.PDPage;
28
29 import org.apache.pdfbox.pdmodel.edit.PDPageContentStream;
30
31 import org.apache.pdfbox.pdmodel.graphics.xobject.PDCcitt;
32 import org.apache.pdfbox.pdmodel.graphics.xobject.PDJpeg;
33 import org.apache.pdfbox.pdmodel.graphics.xobject.PDXObjectImage;
34
35
36 /**
37 * This is an example that creates a simple document.
38 *
39 * The example is taken from the pdf file format specification.
40 *
41 * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
42 * @version $Revision: 1.7 $
43 */
44 public class ImageToPDF
45 {
46
47 /**
48 * create the second sample document from the PDF file format specification.
49 *
50 * @param file The file to write the PDF to.
51 * @param image The filename of the image to put in the PDF.
52 *
53 * @throws IOException If there is an error writing the data.
54 * @throws COSVisitorException If there is an error writing the PDF.
55 */
56 public void createPDFFromImage( String file, String image) throws IOException, COSVisitorException
57 {
58 // the document
59 PDDocument doc = null;
60 try
61 {
62 doc = new PDDocument();
63
64 PDPage page = new PDPage();
65 doc.addPage( page );
66
67 PDXObjectImage ximage = null;
68 if( image.toLowerCase().endsWith( ".jpg" ) )
69 {
70 ximage = new PDJpeg(doc, new FileInputStream( image ) );
71 }
72 else if (image.toLowerCase().endsWith(".tif") || image.toLowerCase().endsWith(".tiff"))
73 {
74 ximage = new PDCcitt(doc, new RandomAccessFile(new File(image),"r"));
75 }
76 else
77 {
78 //BufferedImage awtImage = ImageIO.read( new File( image ) );
79 //ximage = new PDPixelMap(doc, awtImage);
80 throw new IOException( "Image type not supported:" + image );
81 }
82 PDPageContentStream contentStream = new PDPageContentStream(doc, page);
83
84 contentStream.drawImage( ximage, 20, 20 );
85
86 contentStream.close();
87 doc.save( file );
88 }
89 finally
90 {
91 if( doc != null )
92 {
93 doc.close();
94 }
95 }
96 }
97
98 /**
99 * This will create a PDF document with a single image on it.
100 * <br />
101 * see usage() for commandline
102 *
103 * @param args Command line arguments.
104 */
105 public static void main(String[] args)
106 {
107 ImageToPDF app = new ImageToPDF();
108 try
109 {
110 if( args.length != 2 )
111 {
112 app.usage();
113 }
114 else
115 {
116 app.createPDFFromImage( args[0], args[1] );
117 }
118 }
119 catch (Exception e)
120 {
121 e.printStackTrace();
122 }
123 }
124
125 /**
126 * This will print out a message telling how to use this example.
127 */
128 private void usage()
129 {
130 System.err.println( "usage: " + this.getClass().getName() + " <output-file> <image>" );
131 }
132 }