KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: Handouts.java,v 1.2 2005/04/06 12:08:18 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.PageSize;
60 import com.lowagie.text.Rectangle;
61 import com.lowagie.text.pdf.PdfContentByte;
62 import com.lowagie.text.pdf.PdfImportedPage;
63 import com.lowagie.text.pdf.PdfReader;
64 import com.lowagie.text.pdf.PdfWriter;
65 import com.lowagie.tools.arguments.FileArgument;
66 import com.lowagie.tools.arguments.OptionArgument;
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 Handouts extends AbstractTool {
74     /**
75      * Constructs a Tiff2Pdf object.
76      */

77     public Handouts() {
78         arguments.add(new FileArgument(this, "srcfile", "The file you want to convert", false, new PdfFilter()));
79         arguments.add(new FileArgument(this, "destfile", "The file to which the converted TIFF has to be written", true, new PdfFilter()));
80         OptionArgument oa = new OptionArgument(this, "pages", "The number of pages you want on one handout page");
81         oa.addOption("2 pages on 1", "2");
82         oa.addOption("3 pages on 1", "3");
83         oa.addOption("4 pages on 1", "4");
84         oa.addOption("5 pages on 1", "5");
85         oa.addOption("6 pages on 1", "6");
86         oa.addOption("7 pages on 1", "7");
87         oa.addOption("8 pages on 1", "8");
88         arguments.add(oa);
89     }
90
91     /**
92      * @see com.lowagie.tools.plugins.AbstractTool#createFrame()
93      */

94     protected void createFrame() {
95         internalFrame = new JInternalFrame("Handouts", true, true, true);
96         internalFrame.setSize(300, 250);
97         internalFrame.setJMenuBar(getMenubar());
98         internalFrame.getContentPane().add(getConsole(20, 10));
99     }
100
101     /**
102      * @see com.lowagie.tools.plugins.AbstractTool#execute()
103      */

104     public void execute() {
105         try {
106             if (getValue("srcfile") == null) throw new InstantiationException("You need to choose a sourcefile");
107             File src = (File)getValue("srcfile");
108             if (getValue("destfile") == null) throw new InstantiationException("You need to choose a destination file");
109             File dest = (File)getValue("destfile");
110             int pages;
111             try {
112                 pages = Integer.parseInt((String) getValue("pages"));
113             }
114             catch(Exception e) {
115                 pages = 4;
116             }
117             
118             float x1 = 30f;
119             float x2 = 280f;
120             float x3 = 320f;
121             float x4 = 565f;
122             
123             float[] y1 = new float[pages];
124             float[] y2 = new float[pages];
125             
126             float height = (778f - (20f * (pages - 1))) / pages;
127             y1[0] = 812f;
128             y2[0] = 812f - height;
129             
130             for (int i = 1; i < pages; i++) {
131                 y1[i] = y2[i - 1] - 20f;
132                 y2[i] = y1[i] - height;
133             }
134             
135             // we create a reader for a certain document
136
PdfReader reader = new PdfReader(src.getAbsolutePath());
137             // we retrieve the total number of pages
138
int n = reader.getNumberOfPages();
139             System.out.println("There are " + n + " pages in the original file.");
140             
141             // step 1: creation of a document-object
142
Document document = new Document(PageSize.A4);
143             // step 2: we create a writer that listens to the document
144
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
145             // step 3: we open the document
146
document.open();
147             PdfContentByte cb = writer.getDirectContent();
148             PdfImportedPage page;
149             int rotation;
150             int i = 0;
151             int p = 0;
152             // step 4: we add content
153
while (i < n) {
154                 i++;
155                 Rectangle rect = reader.getPageSizeWithRotation(i);
156                 float factorx = (x2 - x1) / rect.width();
157                 float factory = (y1[p] - y2[p]) / rect.height();
158                 float factor = (factorx < factory ? factorx : factory);
159                 float dx = (factorx == factor ? 0f : ((x2 - x1) - rect.width() * factor) / 2f);
160                 float dy = (factory == factor ? 0f : ((y1[p] - y2[p]) - rect.height() * factor) / 2f);
161                 page = writer.getImportedPage(reader, i);
162                 rotation = reader.getPageRotation(i);
163                 if (rotation == 90 || rotation == 270) {
164                     cb.addTemplate(page, 0, -factor, factor, 0, x1 + dx, y2[p] + dy + rect.height() * factor);
165                 }
166                 else {
167                     cb.addTemplate(page, factor, 0, 0, factor, x1 + dx, y2[p] + dy);
168                 }
169                 cb.setRGBColorStroke(0xC0, 0xC0, 0xC0);
170                 cb.rectangle(x3 - 5f, y2[p] - 5f, x4 - x3 + 10f, y1[p] - y2[p] + 10f);
171                 for (float l = y1[p] - 19; l > y2[p]; l -= 16) {
172                     cb.moveTo(x3, l);
173                     cb.lineTo(x4, l);
174                 }
175                 cb.rectangle(x1 + dx, y2[p] + dy, rect.width() * factor, rect.height() * factor);
176                 cb.stroke();
177                 System.out.println("Processed page " + i);
178                 p++;
179                 if (p == pages) {
180                     p = 0;
181                     document.newPage();
182                 }
183             }
184             // step 5: we close the document
185
document.close();
186         }
187         catch(Exception e) {
188             JOptionPane.showMessageDialog(internalFrame,
189                     e.getMessage(),
190                     e.getClass().getName(),
191                     JOptionPane.ERROR_MESSAGE);
192             System.err.println(e.getMessage());
193         }
194     }
195
196     /**
197      * @see com.lowagie.tools.plugins.AbstractTool#valueHasChanged(com.lowagie.tools.arguments.ToolArgument)
198      */

199     public void valueHasChanged(ToolArgument arg) {
200         if (internalFrame == null) {
201             // if the internal frame is null, the tool was called from the commandline
202
return;
203         }
204         // represent the changes of the argument in the internal frame
205
}
206
207     
208     /**
209      * Converts a tiff file to PDF.
210      * @param args
211      */

212     public static void main(String[] args) {
213         Handouts tool = new Handouts();
214         if (args.length < 2) {
215             System.err.println(tool.getUsage());
216         }
217         tool.setArguments(args);
218         tool.execute();
219     }
220
221     /**
222      * @see com.lowagie.tools.plugins.AbstractTool#getDestPathPDF()
223      */

224     protected File getDestPathPDF() throws InstantiationException {
225         return (File)getValue("destfile");
226     }
227 }
228
Popular Tags