KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > tools > plugins > Tiff2Pdf


1 /*
2  * $Id: Tiff2Pdf.java,v 1.5 2005/04/06 12:08:19 blowagie Exp $
3  * $Name: $
4  *
5  * Copyright 2005 by Bruno Lowagie.
6  *
7  * The contents of this file are subject to the Mozilla Public License Version 1.1
8  * (the "License"); you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the License.
14  *
15  * The Original Code is 'iText, a free JAVA-PDF library'.
16  *
17  * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
18  * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
19  * All Rights Reserved.
20  * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
21  * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
22  *
23  * Contributor(s): all the names of the contributors are added in the source code
24  * where applicable.
25  *
26  * Alternatively, the contents of this file may be used under the terms of the
27  * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
28  * provisions of LGPL are applicable instead of those above. If you wish to
29  * allow use of your version of this file only under the terms of the LGPL
30  * License and not to allow others to use your version of this file under
31  * the MPL, indicate your decision by deleting the provisions above and
32  * replace them with the notice and other provisions required by the LGPL.
33  * If you do not delete the provisions above, a recipient may use your version
34  * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Library General Public License as published by the Free Software Foundation;
39  * either version 2 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
43  * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
44  * details.
45  *
46  * If you didn't download this code from the following link, you should check if
47  * you aren't using an obsolete version:
48  * http://www.lowagie.com/iText/
49  */

50 package com.lowagie.tools.plugins;
51
52 import java.io.File;
53 import java.io.FileOutputStream;
54
55 import javax.swing.JInternalFrame;
56 import javax.swing.JOptionPane;
57
58 import com.lowagie.text.Document;
59 import com.lowagie.text.Image;
60 import com.lowagie.text.Paragraph;
61 import com.lowagie.text.pdf.PdfContentByte;
62 import com.lowagie.text.pdf.PdfWriter;
63 import com.lowagie.text.pdf.RandomAccessFileOrArray;
64 import com.lowagie.text.pdf.codec.TiffImage;
65 import com.lowagie.tools.arguments.FileArgument;
66 import com.lowagie.tools.arguments.ImageFilter;
67 import com.lowagie.tools.arguments.PdfFilter;
68 import com.lowagie.tools.arguments.ToolArgument;
69
70 /**
71  * Converts a Tiff file to a PDF file.
72  */

73 public class Tiff2Pdf extends AbstractTool {
74     /**
75      * Constructs a Tiff2Pdf object.
76      */

77     public Tiff2Pdf() {
78         arguments.add(new FileArgument(this, "srcfile", "The file you want to convert", false, new ImageFilter(false, false, false, false, false, true)));
79         arguments.add(new FileArgument(this, "destfile", "The file to which the converted TIFF has to be written", true, new PdfFilter()));
80     }
81
82     /**
83      * @see com.lowagie.tools.plugins.AbstractTool#createFrame()
84      */

85     protected void createFrame() {
86         internalFrame = new JInternalFrame("Tiff2Pdf", true, true, true);
87         internalFrame.setSize(550, 250);
88         internalFrame.setJMenuBar(getMenubar());
89         internalFrame.getContentPane().add(getConsole(40, 30));
90     }
91
92     /**
93      * @see com.lowagie.tools.plugins.AbstractTool#execute()
94      */

95     public void execute() {
96         try {
97             if (getValue("srcfile") == null) throw new InstantiationException("You need to choose a sourcefile");
98             File tiff_file = (File)getValue("srcfile");
99             if (getValue("destfile") == null) throw new InstantiationException("You need to choose a destination file");
100             File pdf_file = (File)getValue("destfile");
101             Document document = new Document();
102             PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(pdf_file));
103             int pages = 0;
104             document.open();
105             PdfContentByte cb = writer.getDirectContent();
106             RandomAccessFileOrArray ra = null;
107             int comps = 0;
108             ra = new RandomAccessFileOrArray(tiff_file.getAbsolutePath());
109             comps = TiffImage.getNumberOfPages(ra);
110             for (int c = 0; c < comps; ++c) {
111                 Image img = TiffImage.getTiffImage(ra, c + 1);
112                 if (img != null) {
113                     if (img.scaledWidth() > 500 || img.scaledHeight() > 700) {
114                         img.scaleToFit(500, 700);
115                     }
116                     img.setAbsolutePosition(20, 20);
117                     document.add(new Paragraph(tiff_file + " - page " + (c + 1)));
118                     cb.addImage(img);
119                     System.out.println("Finished page " + (c + 1));
120                     document.newPage();
121                     ++pages;
122                 }
123             }
124             ra.close();
125             document.close();
126         } catch (Exception e) {
127             JOptionPane.showMessageDialog(internalFrame,
128                     e.getMessage(),
129                     e.getClass().getName(),
130                     JOptionPane.ERROR_MESSAGE);
131             System.err.println(e.getMessage());
132         }
133     }
134
135     /**
136      * @see com.lowagie.tools.plugins.AbstractTool#valueHasChanged(com.lowagie.tools.arguments.ToolArgument)
137      */

138     public void valueHasChanged(ToolArgument arg) {
139         if (internalFrame == null) {
140             // if the internal frame is null, the tool was called from the commandline
141
return;
142         }
143         // represent the changes of the argument in the internal frame
144
}
145
146     
147     /**
148      * Converts a tiff file to PDF.
149      * @param args
150      */

151     public static void main(String[] args) {
152         Tiff2Pdf tool = new Tiff2Pdf();
153         if (args.length < 2) {
154             System.err.println(tool.getUsage());
155         }
156         tool.setArguments(args);
157         tool.execute();
158     }
159
160     /**
161      * @see com.lowagie.tools.plugins.AbstractTool#getDestPathPDF()
162      */

163     protected File getDestPathPDF() throws InstantiationException {
164         return (File)getValue("destfile");
165     }
166 }
Popular Tags