KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ImageToPDF


1 // $Id: ImageToPDF.java,v 1.3 2004/05/16 21:54:30 mike Exp $
2

3 import java.util.*;
4 import java.io.*;
5 import org.faceless.pdf2.*;
6
7 /**
8  * Takes a list of image files on the command line
9  * and creates a PDF with each image, one per page.
10  * If an image has multiple pages (like some TIFFs),
11  * include each one.
12  *
13  * Recognized file formats include most PNG, GIF, JPEG
14  * and TIFF images. Unrecognized files will display an
15  * error and the process will continue.
16  */

17 public class ImageToPDF
18 {
19     public static void main(String JavaDoc[] args)
20         throws IOException
21     {
22     if (args.length==0) {
23         System.out.println("Usage: java ImageToPDF <file1> [<file2> ... ]\n");
24         System.out.println(" Creates the file \"ImageToPDF.pdf\"\n\n");
25         System.exit(0);
26     }
27
28     PDF pdf = new PDF();
29
30     for (int i=0;i<args.length;i++) {
31         // Load the specified image as a PDFImageSet - even if it only
32
// contains one page, this keeps things simple.
33
//
34
InputStream in = new FileInputStream(args[i]);
35         PDFImageSet imgs = new PDFImageSet(in);
36         in.close();
37         for (int j=0;j<imgs.getNumImages();j++)
38         {
39         // For each page in the image set, create a new page in the
40
// PDF that's the same size,and display the image.
41
//
42
PDFImage img = imgs.getImage(j);
43         PDFPage page = pdf.newPage((int)img.getWidth(), (int)img.getHeight());
44         page.drawImage(img,0,0,page.getWidth(),page.getHeight());
45         }
46     }
47
48     OutputStream fo = new FileOutputStream("ImageToPDF.pdf");
49     pdf.render(fo);
50     fo.close();
51     }
52 }
53
Popular Tags