001: /*
002: * $Id: handout_pdf.java 2752 2007-05-15 14:58:33Z blowagie $
003: * $Name$
004: *
005: * Copyright 2002 by Bruno Lowagie
006: *
007: * The contents of this file are subject to the Mozilla Public License Version 1.1
008: * (the "License"); you may not use this file except in compliance with the License.
009: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
010: *
011: * Software distributed under the License is distributed on an "AS IS" basis,
012: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
013: * for the specific language governing rights and limitations under the License.
014: *
015: * The Original Code is 'iText, a free JAVA-PDF library'.
016: *
017: * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
018: * the Initial Developer are Copyright (C) 1999-2006 by Bruno Lowagie.
019: * All Rights Reserved.
020: * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
021: * are Copyright (C) 2000-2006 by Paulo Soares. All Rights Reserved.
022: *
023: * Contributor(s): all the names of the contributors are added in the source code
024: * where applicable.
025: *
026: * Alternatively, the contents of this file may be used under the terms of the
027: * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
028: * provisions of LGPL are applicable instead of those above. If you wish to
029: * allow use of your version of this file only under the terms of the LGPL
030: * License and not to allow others to use your version of this file under
031: * the MPL, indicate your decision by deleting the provisions above and
032: * replace them with the notice and other provisions required by the LGPL.
033: * If you do not delete the provisions above, a recipient may use your version
034: * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
035: *
036: * This library is free software; you can redistribute it and/or modify it
037: * under the terms of the MPL as stated above or under the terms of the GNU
038: * Library General Public License as published by the Free Software Foundation;
039: * either version 2 of the License, or any later version.
040: *
041: * This library is distributed in the hope that it will be useful, but WITHOUT
042: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
043: * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
044: * details.
045: *
046: * If you didn't download this code from the following link, you should check if
047: * you aren't using an obsolete version:
048: * http://www.lowagie.com/iText/
049: */
050:
051: package com.lowagie.tools;
052:
053: import java.io.FileOutputStream;
054:
055: import com.lowagie.text.Document;
056: import com.lowagie.text.DocumentException;
057: import com.lowagie.text.PageSize;
058: import com.lowagie.text.Rectangle;
059: import com.lowagie.text.pdf.PdfContentByte;
060: import com.lowagie.text.pdf.PdfImportedPage;
061: import com.lowagie.text.pdf.PdfReader;
062: import com.lowagie.text.pdf.PdfWriter;
063:
064: /**
065: * Takes an existing PDF file and makes handouts.
066: */
067: public class handout_pdf extends java.lang.Object {
068:
069: /**
070: * Makes handouts based on an existing PDF file.
071: * @param args the command line arguments
072: */
073: public static void main(String args[]) {
074: if (args.length != 3) {
075: System.err.println("arguments: srcfile destfile pages");
076: } else {
077: try {
078: int pages = Integer.parseInt(args[2]);
079: if (pages < 2 || pages > 8) {
080: throw new DocumentException(
081: "You can't have "
082: + pages
083: + " pages on one page (minimum 2; maximum 8).");
084: }
085:
086: float x1 = 30f;
087: float x2 = 280f;
088: float x3 = 320f;
089: float x4 = 565f;
090:
091: float[] y1 = new float[pages];
092: float[] y2 = new float[pages];
093:
094: float height = (778f - (20f * (pages - 1))) / pages;
095: y1[0] = 812f;
096: y2[0] = 812f - height;
097:
098: for (int i = 1; i < pages; i++) {
099: y1[i] = y2[i - 1] - 20f;
100: y2[i] = y1[i] - height;
101: }
102:
103: // we create a reader for a certain document
104: PdfReader reader = new PdfReader(args[0]);
105: // we retrieve the total number of pages
106: int n = reader.getNumberOfPages();
107: System.out.println("There are " + n
108: + " pages in the original file.");
109:
110: // step 1: creation of a document-object
111: Document document = new Document(PageSize.A4);
112: // step 2: we create a writer that listens to the document
113: PdfWriter writer = PdfWriter.getInstance(document,
114: new FileOutputStream(args[1]));
115: // step 3: we open the document
116: document.open();
117: PdfContentByte cb = writer.getDirectContent();
118: PdfImportedPage page;
119: int rotation;
120: int i = 0;
121: int p = 0;
122: // step 4: we add content
123: while (i < n) {
124: i++;
125: Rectangle rect = reader.getPageSizeWithRotation(i);
126: float factorx = (x2 - x1) / rect.getWidth();
127: float factory = (y1[p] - y2[p]) / rect.getHeight();
128: float factor = (factorx < factory ? factorx
129: : factory);
130: float dx = (factorx == factor ? 0f
131: : ((x2 - x1) - rect.getWidth() * factor) / 2f);
132: float dy = (factory == factor ? 0f
133: : ((y1[p] - y2[p]) - rect.getHeight()
134: * factor) / 2f);
135: page = writer.getImportedPage(reader, i);
136: rotation = reader.getPageRotation(i);
137: if (rotation == 90 || rotation == 270) {
138: cb.addTemplate(page, 0, -factor, factor, 0, x1
139: + dx, y2[p] + dy + rect.getHeight()
140: * factor);
141: } else {
142: cb.addTemplate(page, factor, 0, 0, factor, x1
143: + dx, y2[p] + dy);
144: }
145: cb.setRGBColorStroke(0xC0, 0xC0, 0xC0);
146: cb.rectangle(x3 - 5f, y2[p] - 5f, x4 - x3 + 10f,
147: y1[p] - y2[p] + 10f);
148: for (float l = y1[p] - 19; l > y2[p]; l -= 16) {
149: cb.moveTo(x3, l);
150: cb.lineTo(x4, l);
151: }
152: cb.rectangle(x1 + dx, y2[p] + dy, rect.getWidth()
153: * factor, rect.getHeight() * factor);
154: cb.stroke();
155: System.out.println("Processed page " + i);
156: p++;
157: if (p == pages) {
158: p = 0;
159: document.newPage();
160: }
161: }
162: // step 5: we close the document
163: document.close();
164: } catch (Exception e) {
165: System.err.println(e.getClass().getName() + ": "
166: + e.getMessage());
167: }
168: }
169: }
170: }
|