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.Image; 60 import com.lowagie.text.Paragraph; 61 import com.lowagie.text.pdf.PdfContentByte; 62 import com.lowagie.text.pdf.PdfWriter; 63 import com.lowagie.text.pdf.RandomAccessFileOrArray; 64 import com.lowagie.text.pdf.codec.TiffImage; 65 import com.lowagie.tools.arguments.FileArgument; 66 import com.lowagie.tools.arguments.ImageFilter; 67 import com.lowagie.tools.arguments.PdfFilter; 68 import com.lowagie.tools.arguments.ToolArgument; 69 70 73 public class Tiff2Pdf extends AbstractTool { 74 77 public Tiff2Pdf() { 78 arguments.add(new FileArgument(this, "srcfile", "The file you want to convert", false, new ImageFilter(false, false, false, false, false, true))); 79 arguments.add(new FileArgument(this, "destfile", "The file to which the converted TIFF has to be written", true, new PdfFilter())); 80 } 81 82 85 protected void createFrame() { 86 internalFrame = new JInternalFrame("Tiff2Pdf", true, true, true); 87 internalFrame.setSize(550, 250); 88 internalFrame.setJMenuBar(getMenubar()); 89 internalFrame.getContentPane().add(getConsole(40, 30)); 90 } 91 92 95 public void execute() { 96 try { 97 if (getValue("srcfile") == null) throw new InstantiationException("You need to choose a sourcefile"); 98 File tiff_file = (File)getValue("srcfile"); 99 if (getValue("destfile") == null) throw new InstantiationException("You need to choose a destination file"); 100 File pdf_file = (File)getValue("destfile"); 101 Document document = new Document(); 102 PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(pdf_file)); 103 int pages = 0; 104 document.open(); 105 PdfContentByte cb = writer.getDirectContent(); 106 RandomAccessFileOrArray ra = null; 107 int comps = 0; 108 ra = new RandomAccessFileOrArray(tiff_file.getAbsolutePath()); 109 comps = TiffImage.getNumberOfPages(ra); 110 for (int c = 0; c < comps; ++c) { 111 Image img = TiffImage.getTiffImage(ra, c + 1); 112 if (img != null) { 113 if (img.scaledWidth() > 500 || img.scaledHeight() > 700) { 114 img.scaleToFit(500, 700); 115 } 116 img.setAbsolutePosition(20, 20); 117 document.add(new Paragraph(tiff_file + " - page " + (c + 1))); 118 cb.addImage(img); 119 System.out.println("Finished page " + (c + 1)); 120 document.newPage(); 121 ++pages; 122 } 123 } 124 ra.close(); 125 document.close(); 126 } catch (Exception e) { 127 JOptionPane.showMessageDialog(internalFrame, 128 e.getMessage(), 129 e.getClass().getName(), 130 JOptionPane.ERROR_MESSAGE); 131 System.err.println(e.getMessage()); 132 } 133 } 134 135 138 public void valueHasChanged(ToolArgument arg) { 139 if (internalFrame == null) { 140 return; 142 } 143 } 145 146 147 151 public static void main(String[] args) { 152 Tiff2Pdf tool = new Tiff2Pdf(); 153 if (args.length < 2) { 154 System.err.println(tool.getUsage()); 155 } 156 tool.setArguments(args); 157 tool.execute(); 158 } 159 160 163 protected File getDestPathPDF() throws InstantiationException { 164 return (File)getValue("destfile"); 165 } 166 } | Popular Tags |