1 50 51 package com.lowagie.tools; 52 53 import java.io.FileOutputStream ; 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 67 public class handout_pdf extends java.lang.Object { 68 69 73 public static void main (String 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 PdfReader reader = new PdfReader(args[0]); 103 int n = reader.getNumberOfPages(); 105 System.out.println("There are " + n + " pages in the original file."); 106 107 Document document = new Document(PageSize.A4); 109 PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream (args[1])); 111 document.open(); 113 PdfContentByte cb = writer.getDirectContent(); 114 PdfImportedPage page; 115 int rotation; 116 int i = 0; 117 int p = 0; 118 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 document.close(); 152 } 153 catch(Exception e) { 154 System.err.println(e.getClass().getName() + ": " + e.getMessage()); 155 } 156 } 157 } 158 } | Popular Tags |