KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > tools > handout_pdf


1 /*
2  * $Id: handout_pdf.java 2752 2007-05-15 14:58:33Z blowagie $
3  * $Name$
4  *
5  * Copyright 2002 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-2006 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-2006 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
51 package com.lowagie.tools;
52
53 import java.io.FileOutputStream JavaDoc;
54
55 import com.lowagie.text.Document;
56 import com.lowagie.text.DocumentException;
57 import com.lowagie.text.PageSize;
58 import com.lowagie.text.Rectangle;
59 import com.lowagie.text.pdf.PdfContentByte;
60 import com.lowagie.text.pdf.PdfImportedPage;
61 import com.lowagie.text.pdf.PdfReader;
62 import com.lowagie.text.pdf.PdfWriter;
63
64 /**
65  * Takes an existing PDF file and makes handouts.
66  */

67 public class handout_pdf extends java.lang.Object JavaDoc {
68     
69     /**
70      * Makes handouts based on an existing PDF file.
71      * @param args the command line arguments
72      */

73     public static void main (String JavaDoc args[]) {
74         if (args.length != 3) {
75             System.err.println("arguments: srcfile destfile pages");
76         }
77         else {
78             try {
79                 int pages = Integer.parseInt(args[2]);
80                 if (pages < 2 || pages > 8) {
81                     throw new DocumentException("You can't have " + pages + " pages on one page (minimum 2; maximum 8).");
82                 }
83                 
84                 float x1 = 30f;
85                 float x2 = 280f;
86                 float x3 = 320f;
87                 float x4 = 565f;
88                 
89                 float[] y1 = new float[pages];
90                 float[] y2 = new float[pages];
91                 
92                 float height = (778f - (20f * (pages - 1))) / pages;
93                 y1[0] = 812f;
94                 y2[0] = 812f - height;
95                 
96                 for (int i = 1; i < pages; i++) {
97                     y1[i] = y2[i - 1] - 20f;
98                     y2[i] = y1[i] - height;
99                 }
100                 
101                 // we create a reader for a certain document
102
PdfReader reader = new PdfReader(args[0]);
103                 // we retrieve the total number of pages
104
int n = reader.getNumberOfPages();
105                 System.out.println("There are " + n + " pages in the original file.");
106                 
107                 // step 1: creation of a document-object
108
Document document = new Document(PageSize.A4);
109                 // step 2: we create a writer that listens to the document
110
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream JavaDoc(args[1]));
111                 // step 3: we open the document
112
document.open();
113                 PdfContentByte cb = writer.getDirectContent();
114                 PdfImportedPage page;
115                 int rotation;
116                 int i = 0;
117                 int p = 0;
118                 // step 4: we add content
119
while (i < n) {
120                     i++;
121                     Rectangle rect = reader.getPageSizeWithRotation(i);
122                     float factorx = (x2 - x1) / rect.getWidth();
123                     float factory = (y1[p] - y2[p]) / rect.getHeight();
124                     float factor = (factorx < factory ? factorx : factory);
125                     float dx = (factorx == factor ? 0f : ((x2 - x1) - rect.getWidth() * factor) / 2f);
126                     float dy = (factory == factor ? 0f : ((y1[p] - y2[p]) - rect.getHeight() * factor) / 2f);
127                     page = writer.getImportedPage(reader, i);
128                     rotation = reader.getPageRotation(i);
129                     if (rotation == 90 || rotation == 270) {
130                         cb.addTemplate(page, 0, -factor, factor, 0, x1 + dx, y2[p] + dy + rect.getHeight() * factor);
131                     }
132                     else {
133                         cb.addTemplate(page, factor, 0, 0, factor, x1 + dx, y2[p] + dy);
134                     }
135                     cb.setRGBColorStroke(0xC0, 0xC0, 0xC0);
136                     cb.rectangle(x3 - 5f, y2[p] - 5f, x4 - x3 + 10f, y1[p] - y2[p] + 10f);
137                     for (float l = y1[p] - 19; l > y2[p]; l -= 16) {
138                         cb.moveTo(x3, l);
139                         cb.lineTo(x4, l);
140                     }
141                     cb.rectangle(x1 + dx, y2[p] + dy, rect.getWidth() * factor, rect.getHeight() * factor);
142                     cb.stroke();
143                     System.out.println("Processed page " + i);
144                     p++;
145                     if (p == pages) {
146                         p = 0;
147                         document.newPage();
148                     }
149                 }
150                 // step 5: we close the document
151
document.close();
152             }
153             catch(Exception JavaDoc e) {
154                 System.err.println(e.getClass().getName() + ": " + e.getMessage());
155             }
156         }
157     }
158 }
Popular Tags