1 /* 2 * $Id$ 3 * Copyright (c) 2005-2007 Bruno Lowagie, Carsten Hammer 4 * 5 * Permission is hereby granted, free of charge, to any person 6 * obtaining a copy of this software and associated documentation 7 * files (the "Software"), to deal in the Software without 8 * restriction, including without limitation the rights to use, 9 * copy, modify, merge, publish, distribute, sublicense, and/or sell 10 * copies of the Software, and to permit persons to whom the 11 * Software is furnished to do so, subject to the following 12 * conditions: 13 * 14 * The above copyright notice and this permission notice shall be 15 * included in all copies or substantial portions of the Software. 16 * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 19 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 21 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 22 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 24 * OTHER DEALINGS IN THE SOFTWARE. 25 */ 26 27 /* 28 * This class was originally published under the MPL by Bruno Lowagie 29 * and Carsten Hammer. 30 * It was a part of iText, a Java-PDF library. You can now use it under 31 * the MIT License; for backward compatibility you can also use it under 32 * the MPL version 1.1: http://www.mozilla.org/MPL/ 33 * A copy of the MPL license is bundled with the source code FYI. 34 */ 35 36 package com.lowagie.toolbox.plugins; 37 38 import java.io.File; 39 import java.io.FileOutputStream; 40 41 import javax.swing.JInternalFrame; 42 import javax.swing.JOptionPane; 43 44 import com.lowagie.text.Document; 45 import com.lowagie.text.Image; 46 import com.lowagie.text.PageSize; 47 import com.lowagie.text.Paragraph; 48 import com.lowagie.text.Rectangle; 49 import com.lowagie.text.pdf.PdfContentByte; 50 import com.lowagie.text.pdf.PdfWriter; 51 import com.lowagie.text.pdf.RandomAccessFileOrArray; 52 import com.lowagie.text.pdf.codec.TiffImage; 53 import com.lowagie.toolbox.AbstractTool; 54 import com.lowagie.toolbox.arguments.AbstractArgument; 55 import com.lowagie.toolbox.arguments.FileArgument; 56 import com.lowagie.toolbox.arguments.OptionArgument; 57 import com.lowagie.toolbox.arguments.filters.ImageFilter; 58 import com.lowagie.toolbox.arguments.filters.PdfFilter; 59 60 /** 61 * Converts a Tiff file to a PDF file. 62 * @since 2.1.1 (imported from itexttoolbox project) 63 */ 64 public class Tiff2Pdf extends AbstractTool { 65 66 static { 67 addVersion("$Id$"); 68 } 69 /** 70 * Constructs a Tiff2Pdf object. 71 */ Tiff2Pdf()72 public Tiff2Pdf() { 73 menuoptions = MENU_EXECUTE | MENU_EXECUTE_SHOW; 74 arguments.add(new FileArgument(this, "srcfile", "The file you want to convert", false, new ImageFilter(false, false, false, false, false, true))); 75 arguments.add(new FileArgument(this, "destfile", "The file to which the converted TIFF has to be written", true, new PdfFilter())); 76 OptionArgument oa = new OptionArgument(this, "pagesize", "Pagesize"); 77 oa.addOption("A4", "A4"); 78 oa.addOption("Letter", "LETTER"); 79 oa.addOption("Original format", "ORIGINAL"); 80 arguments.add(oa); 81 } 82 83 /** 84 * @see com.lowagie.toolbox.AbstractTool#createFrame() 85 */ createFrame()86 protected void createFrame() { 87 internalFrame = new JInternalFrame("Tiff2Pdf", true, false, true); 88 internalFrame.setSize(550, 250); 89 internalFrame.setJMenuBar(getMenubar()); 90 System.out.println("=== Tiff2Pdf OPENED ==="); 91 } 92 93 /** 94 * @see com.lowagie.toolbox.AbstractTool#execute() 95 */ execute()96 public void execute() { 97 try { 98 if (getValue("srcfile") == null) throw new InstantiationException("You need to choose a sourcefile"); 99 File tiff_file = (File)getValue("srcfile"); 100 if (getValue("destfile") == null) throw new InstantiationException("You need to choose a destination file"); 101 File pdf_file = (File)getValue("destfile"); 102 RandomAccessFileOrArray ra = new RandomAccessFileOrArray(tiff_file.getAbsolutePath()); 103 int comps = TiffImage.getNumberOfPages(ra); 104 boolean adjustSize = false; 105 Document document = new Document(PageSize.A4); 106 float width = PageSize.A4.getWidth() - 40; 107 float height = PageSize.A4.getHeight() - 120; 108 if ("ORIGINAL".equals(getValue("pagesize"))) { 109 Image img = TiffImage.getTiffImage(ra, 1); 110 if (img.getDpiX() > 0 && img.getDpiY() > 0) { 111 img.scalePercent(7200f / img.getDpiX(), 7200f / img.getDpiY()); 112 } 113 document.setPageSize(new Rectangle(img.getScaledWidth(), img.getScaledHeight())); 114 adjustSize = true; 115 } 116 else if ("LETTER".equals(getValue("pagesize"))) { 117 document.setPageSize(PageSize.LETTER); 118 width = PageSize.LETTER.getWidth() - 40; 119 height = PageSize.LETTER.getHeight() - 120; 120 } 121 PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(pdf_file)); 122 document.open(); 123 PdfContentByte cb = writer.getDirectContent(); 124 for (int c = 0; c < comps; ++c) { 125 Image img = TiffImage.getTiffImage(ra, c + 1); 126 if (img != null) { 127 if (img.getDpiX() > 0 && img.getDpiY() > 0) { 128 img.scalePercent(7200f / img.getDpiX(), 7200f / img.getDpiY()); 129 } 130 if (adjustSize) { 131 document.setPageSize(new Rectangle(img.getScaledWidth(), 132 img.getScaledHeight())); 133 document.newPage(); 134 img.setAbsolutePosition(0, 0); 135 } 136 else { 137 if (img.getScaledWidth() > width || img.getScaledHeight() > height) { 138 if (img.getDpiX() > 0 && img.getDpiY() > 0) { 139 float adjx = width / img.getScaledWidth(); 140 float adjy = height / img.getScaledHeight(); 141 float adj = Math.min(adjx, adjy); 142 img.scalePercent(7200f / img.getDpiX() * adj, 7200f / img.getDpiY() * adj); 143 } 144 else 145 img.scaleToFit(width, height); 146 } 147 img.setAbsolutePosition(20, 20); 148 document.newPage(); 149 document.add(new Paragraph(tiff_file + " - page " + (c + 1))); 150 } 151 cb.addImage(img); 152 System.out.println("Finished page " + (c + 1)); 153 } 154 } 155 ra.close(); 156 document.close(); 157 } catch (Exception e) { 158 JOptionPane.showMessageDialog(internalFrame, 159 e.getMessage(), 160 e.getClass().getName(), 161 JOptionPane.ERROR_MESSAGE); 162 System.err.println(e.getMessage()); 163 } 164 } 165 166 /** 167 * 168 * @see com.lowagie.toolbox.AbstractTool#valueHasChanged(com.lowagie.toolbox.arguments.AbstractArgument) 169 * @param arg StringArgument 170 */ valueHasChanged(AbstractArgument arg)171 public void valueHasChanged(AbstractArgument arg) { 172 if (internalFrame == null) { 173 // if the internal frame is null, the tool was called from the command line 174 return; 175 } 176 // represent the changes of the argument in the internal frame 177 } 178 179 180 /** 181 * Converts a tiff file to PDF. 182 * 183 * @param args String[] 184 */ main(String[] args)185 public static void main(String[] args) { 186 Tiff2Pdf tool = new Tiff2Pdf(); 187 if (args.length < 2) { 188 System.err.println(tool.getUsage()); 189 } 190 tool.setMainArguments(args); 191 tool.execute(); 192 } 193 194 /** 195 * 196 * @see com.lowagie.toolbox.AbstractTool#getDestPathPDF() 197 * @throws InstantiationException 198 * @return File 199 */ getDestPathPDF()200 protected File getDestPathPDF() throws InstantiationException { 201 return (File)getValue("destfile"); 202 } 203 } 204