1 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 73 public class Handouts extends AbstractTool { 74 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 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 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 PdfReader reader = new PdfReader(src.getAbsolutePath()); 137 int n = reader.getNumberOfPages(); 139 System.out.println("There are " + n + " pages in the original file."); 140 141 Document document = new Document(PageSize.A4); 143 PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest)); 145 document.open(); 147 PdfContentByte cb = writer.getDirectContent(); 148 PdfImportedPage page; 149 int rotation; 150 int i = 0; 151 int p = 0; 152 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 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 199 public void valueHasChanged(ToolArgument arg) { 200 if (internalFrame == null) { 201 return; 203 } 204 } 206 207 208 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 224 protected File getDestPathPDF() throws InstantiationException { 225 return (File)getValue("destfile"); 226 } 227 } 228 | Popular Tags |