From 6025b6016517c6d898d8957d1d7e03ba71431912 Mon Sep 17 00:00:00 2001 From: tknall Date: Fri, 1 Dec 2006 12:20:24 +0000 Subject: Initial import of release 2.2. git-svn-id: https://joinup.ec.europa.eu/svn/pdf-as/trunk@4 7b5415b0-85f9-ee4d-85bd-d5d0c3b42d1c --- src/main/java/com/lowagie/tools/BuildTutorial.java | 156 +++++ src/main/java/com/lowagie/tools/CvsLogParser.java | 138 ++++ src/main/java/com/lowagie/tools/Executable.java | 322 +++++++++ src/main/java/com/lowagie/tools/LPR.java | 716 +++++++++++++++++++++ src/main/java/com/lowagie/tools/SwingWorker.java | 127 ++++ src/main/java/com/lowagie/tools/ToolMenuItems.java | 84 +++ src/main/java/com/lowagie/tools/Toolbox.java | 354 ++++++++++ src/main/java/com/lowagie/tools/Versions.java | 143 ++++ .../lowagie/tools/arguments/BitsetArgument.java | 136 ++++ .../com/lowagie/tools/arguments/DirFilter.java | 65 ++ .../com/lowagie/tools/arguments/FileArgument.java | 165 +++++ .../com/lowagie/tools/arguments/ImageArgument.java | 117 ++++ .../com/lowagie/tools/arguments/ImageFilter.java | 135 ++++ .../lowagie/tools/arguments/LabelAccessory.java | 158 +++++ .../lowagie/tools/arguments/OptionArgument.java | 210 ++++++ .../tools/arguments/PageSelectionTableDialog.java | 243 +++++++ .../tools/arguments/PageSelectorToolArgument.java | 112 ++++ .../lowagie/tools/arguments/PageSizeArgument.java | 172 +++++ .../lowagie/tools/arguments/PageTableModel.java | 146 +++++ .../com/lowagie/tools/arguments/PdfFilter.java | 77 +++ .../java/com/lowagie/tools/arguments/TableMap.java | 89 +++ .../com/lowagie/tools/arguments/TableSorter.java | 367 +++++++++++ .../com/lowagie/tools/arguments/ToolArgument.java | 281 ++++++++ src/main/java/com/lowagie/tools/concat_pdf.java | 96 +++ src/main/java/com/lowagie/tools/encrypt_pdf.java | 99 +++ src/main/java/com/lowagie/tools/handout_pdf.java | 122 ++++ .../com/lowagie/tools/plugins/AbstractTool.java | 341 ++++++++++ .../com/lowagie/tools/plugins/Bookmarks2XML.java | 148 +++++ src/main/java/com/lowagie/tools/plugins/Burst.java | 140 ++++ .../java/com/lowagie/tools/plugins/Concat.java | 185 ++++++ .../java/com/lowagie/tools/plugins/Decrypt.java | 148 +++++ .../java/com/lowagie/tools/plugins/DvdCover.java | 201 ++++++ .../java/com/lowagie/tools/plugins/Encrypt.java | 193 ++++++ .../lowagie/tools/plugins/ExtractAttachments.java | 227 +++++++ .../java/com/lowagie/tools/plugins/Handouts.java | 232 +++++++ .../com/lowagie/tools/plugins/HtmlBookmarks.java | 288 +++++++++ .../com/lowagie/tools/plugins/ImageXRefViewer.java | 256 ++++++++ .../java/com/lowagie/tools/plugins/InspectPDF.java | 173 +++++ .../java/com/lowagie/tools/plugins/KnitTiff.java | 178 +++++ .../java/com/lowagie/tools/plugins/LPRClient.java | 245 +++++++ src/main/java/com/lowagie/tools/plugins/NUp.java | 165 +++++ .../java/com/lowagie/tools/plugins/PhotoAlbum.java | 200 ++++++ .../tools/plugins/RemoveLaunchApplication.java | 189 ++++++ .../com/lowagie/tools/plugins/ReversePages.java | 180 ++++++ .../com/lowagie/tools/plugins/SelectedPages.java | 150 +++++ src/main/java/com/lowagie/tools/plugins/Split.java | 168 +++++ .../java/com/lowagie/tools/plugins/Tiff2Pdf.java | 192 ++++++ .../com/lowagie/tools/plugins/TreeViewPDF.java | 160 +++++ .../java/com/lowagie/tools/plugins/Txt2Pdf.java | 169 +++++ .../com/lowagie/tools/plugins/Watermarker.java | 163 +++++ .../com/lowagie/tools/plugins/XML2Bookmarks.java | 156 +++++ .../lowagie/tools/plugins/treeview/AnalyzePDF.java | 507 +++++++++++++++ .../tools/plugins/treeview/ArrayTreeNode.java | 107 +++ .../tools/plugins/treeview/DictionaryTreeNode.java | 112 ++++ .../tools/plugins/treeview/FileTreeNode.java | 86 +++ .../tools/plugins/treeview/ICommonAnalyzer.java | 79 +++ .../tools/plugins/treeview/IUpdatenodeview.java | 59 ++ .../plugins/treeview/OutlinelistTreeNode.java | 98 +++ .../tools/plugins/treeview/PagelistTreeNode.java | 87 +++ .../tools/plugins/treeview/Pagetreenode.java | 208 ++++++ .../tools/plugins/treeview/ProgressDialog.java | 124 ++++ .../tools/plugins/treeview/SimpletextTreeNode.java | 69 ++ .../tools/plugins/treeview/TextpaneTreeNode.java | 91 +++ .../plugins/treeview/TreeViewInternalFrame.java | 272 ++++++++ .../tools/plugins/treeview/UpdateableTreeNode.java | 74 +++ src/main/java/com/lowagie/tools/split_pdf.java | 103 +++ 66 files changed, 11753 insertions(+) create mode 100644 src/main/java/com/lowagie/tools/BuildTutorial.java create mode 100644 src/main/java/com/lowagie/tools/CvsLogParser.java create mode 100644 src/main/java/com/lowagie/tools/Executable.java create mode 100644 src/main/java/com/lowagie/tools/LPR.java create mode 100644 src/main/java/com/lowagie/tools/SwingWorker.java create mode 100644 src/main/java/com/lowagie/tools/ToolMenuItems.java create mode 100644 src/main/java/com/lowagie/tools/Toolbox.java create mode 100644 src/main/java/com/lowagie/tools/Versions.java create mode 100644 src/main/java/com/lowagie/tools/arguments/BitsetArgument.java create mode 100644 src/main/java/com/lowagie/tools/arguments/DirFilter.java create mode 100644 src/main/java/com/lowagie/tools/arguments/FileArgument.java create mode 100644 src/main/java/com/lowagie/tools/arguments/ImageArgument.java create mode 100644 src/main/java/com/lowagie/tools/arguments/ImageFilter.java create mode 100644 src/main/java/com/lowagie/tools/arguments/LabelAccessory.java create mode 100644 src/main/java/com/lowagie/tools/arguments/OptionArgument.java create mode 100644 src/main/java/com/lowagie/tools/arguments/PageSelectionTableDialog.java create mode 100644 src/main/java/com/lowagie/tools/arguments/PageSelectorToolArgument.java create mode 100644 src/main/java/com/lowagie/tools/arguments/PageSizeArgument.java create mode 100644 src/main/java/com/lowagie/tools/arguments/PageTableModel.java create mode 100644 src/main/java/com/lowagie/tools/arguments/PdfFilter.java create mode 100644 src/main/java/com/lowagie/tools/arguments/TableMap.java create mode 100644 src/main/java/com/lowagie/tools/arguments/TableSorter.java create mode 100644 src/main/java/com/lowagie/tools/arguments/ToolArgument.java create mode 100644 src/main/java/com/lowagie/tools/concat_pdf.java create mode 100644 src/main/java/com/lowagie/tools/encrypt_pdf.java create mode 100644 src/main/java/com/lowagie/tools/handout_pdf.java create mode 100644 src/main/java/com/lowagie/tools/plugins/AbstractTool.java create mode 100644 src/main/java/com/lowagie/tools/plugins/Bookmarks2XML.java create mode 100644 src/main/java/com/lowagie/tools/plugins/Burst.java create mode 100644 src/main/java/com/lowagie/tools/plugins/Concat.java create mode 100644 src/main/java/com/lowagie/tools/plugins/Decrypt.java create mode 100644 src/main/java/com/lowagie/tools/plugins/DvdCover.java create mode 100644 src/main/java/com/lowagie/tools/plugins/Encrypt.java create mode 100644 src/main/java/com/lowagie/tools/plugins/ExtractAttachments.java create mode 100644 src/main/java/com/lowagie/tools/plugins/Handouts.java create mode 100644 src/main/java/com/lowagie/tools/plugins/HtmlBookmarks.java create mode 100644 src/main/java/com/lowagie/tools/plugins/ImageXRefViewer.java create mode 100644 src/main/java/com/lowagie/tools/plugins/InspectPDF.java create mode 100644 src/main/java/com/lowagie/tools/plugins/KnitTiff.java create mode 100644 src/main/java/com/lowagie/tools/plugins/LPRClient.java create mode 100644 src/main/java/com/lowagie/tools/plugins/NUp.java create mode 100644 src/main/java/com/lowagie/tools/plugins/PhotoAlbum.java create mode 100644 src/main/java/com/lowagie/tools/plugins/RemoveLaunchApplication.java create mode 100644 src/main/java/com/lowagie/tools/plugins/ReversePages.java create mode 100644 src/main/java/com/lowagie/tools/plugins/SelectedPages.java create mode 100644 src/main/java/com/lowagie/tools/plugins/Split.java create mode 100644 src/main/java/com/lowagie/tools/plugins/Tiff2Pdf.java create mode 100644 src/main/java/com/lowagie/tools/plugins/TreeViewPDF.java create mode 100644 src/main/java/com/lowagie/tools/plugins/Txt2Pdf.java create mode 100644 src/main/java/com/lowagie/tools/plugins/Watermarker.java create mode 100644 src/main/java/com/lowagie/tools/plugins/XML2Bookmarks.java create mode 100644 src/main/java/com/lowagie/tools/plugins/treeview/AnalyzePDF.java create mode 100644 src/main/java/com/lowagie/tools/plugins/treeview/ArrayTreeNode.java create mode 100644 src/main/java/com/lowagie/tools/plugins/treeview/DictionaryTreeNode.java create mode 100644 src/main/java/com/lowagie/tools/plugins/treeview/FileTreeNode.java create mode 100644 src/main/java/com/lowagie/tools/plugins/treeview/ICommonAnalyzer.java create mode 100644 src/main/java/com/lowagie/tools/plugins/treeview/IUpdatenodeview.java create mode 100644 src/main/java/com/lowagie/tools/plugins/treeview/OutlinelistTreeNode.java create mode 100644 src/main/java/com/lowagie/tools/plugins/treeview/PagelistTreeNode.java create mode 100644 src/main/java/com/lowagie/tools/plugins/treeview/Pagetreenode.java create mode 100644 src/main/java/com/lowagie/tools/plugins/treeview/ProgressDialog.java create mode 100644 src/main/java/com/lowagie/tools/plugins/treeview/SimpletextTreeNode.java create mode 100644 src/main/java/com/lowagie/tools/plugins/treeview/TextpaneTreeNode.java create mode 100644 src/main/java/com/lowagie/tools/plugins/treeview/TreeViewInternalFrame.java create mode 100644 src/main/java/com/lowagie/tools/plugins/treeview/UpdateableTreeNode.java create mode 100644 src/main/java/com/lowagie/tools/split_pdf.java (limited to 'src/main/java/com/lowagie/tools') diff --git a/src/main/java/com/lowagie/tools/BuildTutorial.java b/src/main/java/com/lowagie/tools/BuildTutorial.java new file mode 100644 index 0000000..8087731 --- /dev/null +++ b/src/main/java/com/lowagie/tools/BuildTutorial.java @@ -0,0 +1,156 @@ +/* + * $Id: BuildTutorial.java,v 1.8 2004/12/10 14:21:01 blowagie Exp $ + * $Name: $ + * + * This code is free software. It may only be copied or modified + * if you include the following copyright notice: + * + * This code is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * + * itext@lowagie.com + */ +package com.lowagie.tools; + +import java.io.*; + +import javax.xml.transform.*; +import javax.xml.transform.stream.*; + +/** + * This class can be used to build the iText website. + * + * @author Bruno Lowagie (based on an example found in the Developer's Almanac) + */ +public class BuildTutorial { + + static String root; + static FileWriter build; + + //~ Methods + // ---------------------------------------------------------------- + + /** + * Main method so you can call the convert method from the command line. + * @param args 4 arguments are expected: + * + */ + + public static void main(String[] args) { + if (args.length == 4) { + File srcdir = new File(args[0]); + File destdir = new File(args[1]); + File xsl_examples = new File(srcdir, args[2]); + File xsl_site = new File(srcdir, args[3]); + try { + System.out.print("Building tutorial: "); + root = new File(args[1], srcdir.getName()).getCanonicalPath(); + System.out.println(root); + build = new FileWriter(new File(root, "build.xml")); + build.write("\n"); + build.write("\n"); + action(srcdir, destdir, xsl_examples, xsl_site); + build.write("\n"); + build.flush(); + build.close(); + } + catch(IOException ioe) { + ioe.printStackTrace(); + } + } else { + System.err + .println("Wrong number of parameters.\nUsage: BuildSite srcdr destdir xsl_examples xsl_site"); + } + } + + /** + * Inspects a file or directory that is given and performs the necessary actions on it (transformation or recursion). + * @param source a sourcedirectory (possibly with a tutorial xml-file) + * @param destination a destination directory (where the html and build.xml file will be generated, if necessary) + * @param xsl_examples an xsl to transform the index.xml into a build.xml + * @param xsl_site an xsl to transform the index.xml into am index.html + * @throws IOException when something goes wrong while reading or creating a file or directory + */ + public static void action(File source, File destination, File xsl_examples, File xsl_site) throws IOException { + if ("CVS".equals(source.getName())) return; + System.out.print(source.getName()); + if (source.isDirectory()) { + System.out.print(" "); + System.out.println(source.getCanonicalPath()); + File dest = new File(destination, source.getName()); + dest.mkdir(); + File current; + File[] xmlFiles = source.listFiles(); + for (int i = 0; i < xmlFiles.length; i++) { + current = xmlFiles[i]; + action(current, dest, xsl_examples, xsl_site); + } + } + else if (source.getName().equals("index.xml")) { + System.out.println("... transformed"); + convert(source, xsl_site, new File(destination, "index.html")); + File buildfile = new File(destination, "build.xml"); + String path = buildfile.getCanonicalPath().substring(root.length()); + path = path.replace(File.separatorChar, '/'); + if ("/build.xml".equals(path)) return; + convert(source, xsl_examples, buildfile); + build.write("\t\n"); + } + else { + System.out.println("... skipped"); + } + } + + /** + * Converts an infile, using an xslfile to an + * outfile. + * + * @param infile + * the path to an XML file + * @param xslfile + * the path to the XSL file + * @param outfile + * the path for the output file + */ + public static void convert(File infile, File xslfile, File outfile) { + try { + // Create transformer factory + TransformerFactory factory = TransformerFactory.newInstance(); + + // Use the factory to create a template containing the xsl file + Templates template = factory.newTemplates(new StreamSource( + new FileInputStream(xslfile))); + + // Use the template to create a transformer + Transformer xformer = template.newTransformer(); + + // passing 2 parameters + String branch = outfile.getParentFile().getCanonicalPath().substring(root.length()); + branch = branch.replace(File.separatorChar, '/'); + StringBuffer path = new StringBuffer(); + for (int i = 0; i < branch.length(); i++) { + if (branch.charAt(i) == '/') path.append("/.."); + } + + xformer.setParameter("branch", branch); + xformer.setParameter("root", path.toString()); + + // Prepare the input and output files + Source source = new StreamSource(new FileInputStream(infile)); + Result result = new StreamResult(new FileOutputStream(outfile)); + + // Apply the xsl file to the source file and write the result to the + // output file + xformer.transform(source, result); + } catch (Exception e) { + e.printStackTrace(); + } + } +} +//The End diff --git a/src/main/java/com/lowagie/tools/CvsLogParser.java b/src/main/java/com/lowagie/tools/CvsLogParser.java new file mode 100644 index 0000000..39181f1 --- /dev/null +++ b/src/main/java/com/lowagie/tools/CvsLogParser.java @@ -0,0 +1,138 @@ +/* + * $Id: CvsLogParser.java,v 1.5 2006/05/22 11:00:30 blowagie Exp $ + * $Name: $ + * + * This code is free software. It may only be copied or modified + * if you include the following copyright notice: + * + * This code is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * + * itext@lowagie.com + */ +package com.lowagie.tools; + +import java.io.BufferedReader; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.StreamTokenizer; + +import java.util.Enumeration; + +/** + * If you get a changelog from CVS, for instance: + * cvs -d:ext:blowagie@itext.cvs.sourceforge.net:/cvsroot/itext log -d ">2005-07-29" + * you get an overview that contains all the changes. + * With this class, you can parse out the important entries. + * + * @author blowagie + */ +public class CvsLogParser implements Enumeration { + + /** the tokenizer object. */ + protected StreamTokenizer st; + + /** indicates if the current token contains changes. */ + protected boolean changes = false; + + /** indicates if the tokenizer has more tokens. */ + protected boolean more = false; + + /** + * @param file + * @throws FileNotFoundException + */ + public CvsLogParser(String file) throws FileNotFoundException { + BufferedReader r = new BufferedReader(new InputStreamReader(new FileInputStream(file))); + st = new StreamTokenizer(r); + st.eolIsSignificant(true); + st.ordinaryChar('/'); + st.ordinaryChar('\''); + more = true; + } + + /** + * @see java.util.Enumeration#hasMoreElements() + */ + public boolean hasMoreElements() { + return more; + } + + /** + * Returns the next token in the log file. + * @see java.util.Enumeration#nextElement() + */ + public Object nextElement(){ + StringBuffer token = new StringBuffer(); + StringBuffer line = new StringBuffer(); + boolean moreToken = true; + changes = false; + try { + while (more && moreToken) { + st.nextToken(); + switch(st.ttype) { + case StreamTokenizer.TT_EOF: + more = false; + case StreamTokenizer.TT_EOL: + token.append(line.toString()); + if (line.toString().endsWith("=============================================================================")) { + moreToken = false; + } + else { + line = new StringBuffer("\n"); + } + break; + case StreamTokenizer.TT_WORD: + line.append(st.sval); + line.append(" "); + break; + case StreamTokenizer.TT_NUMBER: + if (st.nval > 0 && line.toString().endsWith("selected revisions :")) { + changes = true; + } + line.append(st.nval); + break; + default: + line.append((char) st.ttype); + } + } + return token.toString(); + } + catch(IOException ioe) { + more = false; + return ""; + } + } + + /** + * Indicates if the current token is one that contains changes. + * @return true if the token is relevant + */ + private boolean hasChanged() { + return changes; + } + + + /** + * Parses a log form CVS. + * @param args the path to the logfile + */ + public static void main(String[] args) { + try { + CvsLogParser p = new CvsLogParser(args[0]); + String token; + while (p.hasMoreElements()) { + token = (String) p.nextElement(); + if (p.hasChanged()) { + System.out.println(token); + } + } + } + catch(Exception e) { + e.printStackTrace(); + } + } +} diff --git a/src/main/java/com/lowagie/tools/Executable.java b/src/main/java/com/lowagie/tools/Executable.java new file mode 100644 index 0000000..0467b24 --- /dev/null +++ b/src/main/java/com/lowagie/tools/Executable.java @@ -0,0 +1,322 @@ +/* + * $Id: Executable.java,v 1.6 2005/08/10 14:32:51 blowagie Exp $ + * $Name: $ + * + * Copyright 2005 by Bruno Lowagie / Roger Mistelli + * + * The contents of this file are subject to the Mozilla Public License Version 1.1 + * (the "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the License. + * + * The Original Code is 'iText, a free JAVA-PDF library'. + * + * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by + * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie. + * All Rights Reserved. + * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer + * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved. + * + * Contributor(s): all the names of the contributors are added in the source code + * where applicable. + * + * Alternatively, the contents of this file may be used under the terms of the + * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the + * provisions of LGPL are applicable instead of those above. If you wish to + * allow use of your version of this file only under the terms of the LGPL + * License and not to allow others to use your version of this file under + * the MPL, indicate your decision by deleting the provisions above and + * replace them with the notice and other provisions required by the LGPL. + * If you do not delete the provisions above, a recipient may use your version + * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE. + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the MPL as stated above or under the terms of the GNU + * Library General Public License as published by the Free Software Foundation; + * either version 2 of the License, or any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more + * details. + * + * If you didn't download this code from the following link, you should check if + * you aren't using an obsolete version: + * http://www.lowagie.com/iText/ + */ +package com.lowagie.tools; + +import java.io.File; +import java.io.IOException; +import java.lang.reflect.Method; + +/** + * This class enables you to call an executable that will show a PDF file. + */ +public class Executable { + + /** + * The path to Acrobat Reader. + */ + public static String acroread = null; + + + /** + * Performs an action on a PDF document. + * @param fileName + * @param parameters + * @param waitForTermination + * @return a process + * @throws IOException + */ + private static Process action(final String fileName, + String parameters, boolean waitForTermination) throws IOException { + Process process = null; + if (parameters.trim().length() > 0) { + parameters = " " + parameters.trim(); + } + else { + parameters = ""; + } + if (acroread != null) { + process = Runtime.getRuntime().exec( + acroread + parameters + " \"" + fileName + "\""); + } + else if (isWindows()) { + if (isWindows9X()) { + process = Runtime.getRuntime().exec( + "command.com /C start acrord32" + parameters + " \"" + fileName + "\""); + } + else { + process = Runtime.getRuntime().exec( + "cmd /c start acrord32" + parameters + " \"" + fileName + "\""); + } + } + else if (isMac()) { + if (parameters.trim().length() == 0) { + process = Runtime.getRuntime().exec( + new String[] { "/usr/bin/open", fileName }); + } + else { + process = Runtime.getRuntime().exec( + new String[] { "/usr/bin/open", parameters.trim(), fileName }); + } + } + try { + if (process != null && waitForTermination) + process.waitFor(); + } catch (InterruptedException ie) { + } + return process; + } + + /** + * Opens a PDF document. + * @param fileName + * @param waitForTermination + * @return a process + * @throws IOException + */ + public static final Process openDocument(String fileName, + boolean waitForTermination) throws IOException { + return action(fileName, "", waitForTermination); + } + + /** + * Opens a PDF document. + * @param file + * @param waitForTermination + * @return a process + * @throws IOException + */ + public static final Process openDocument(File file, + boolean waitForTermination) throws IOException { + return openDocument(file.getAbsolutePath(), waitForTermination); + } + + /** + * Opens a PDF document. + * @param fileName + * @return a process + * @throws IOException + */ + public static final Process openDocument(String fileName) throws IOException { + return openDocument(fileName, false); + } + + /** + * Opens a PDF document. + * @param file + * @return a process + * @throws IOException + */ + public static final Process openDocument(File file) throws IOException { + return openDocument(file, false); + } + + /** + * Prints a PDF document. + * @param fileName + * @param waitForTermination + * @return a process + * @throws IOException + */ + public static final Process printDocument(String fileName, + boolean waitForTermination) throws IOException { + return action(fileName, "/p", waitForTermination); + } + + /** + * Prints a PDF document. + * @param file + * @param waitForTermination + * @return a process + * @throws IOException + */ + public static final Process printDocument(File file, + boolean waitForTermination) throws IOException { + return printDocument(file.getAbsolutePath(), waitForTermination); + } + + /** + * Prints a PDF document. + * @param fileName + * @return a process + * @throws IOException + */ + public static final Process printDocument(String fileName) throws IOException { + return printDocument(fileName, false); + } + + /** + * Prints a PDF document. + * @param file + * @return a process + * @throws IOException + */ + public static final Process printDocument(File file) throws IOException { + return printDocument(file, false); + } + + /** + * Prints a PDF document without opening a Dialog box. + * @param fileName + * @param waitForTermination + * @return a process + * @throws IOException + */ + public static final Process printDocumentSilent(String fileName, + boolean waitForTermination) throws IOException { + return action(fileName, "/p /h", waitForTermination); + } + + /** + * Prints a PDF document without opening a Dialog box. + * @param file + * @param waitForTermination + * @return a process + * @throws IOException + */ + public static final Process printDocumentSilent(File file, + boolean waitForTermination) throws IOException { + return printDocumentSilent(file.getAbsolutePath(), waitForTermination); + } + + /** + * Prints a PDF document without opening a Dialog box. + * @param fileName + * @return a process + * @throws IOException + */ + public static final Process printDocumentSilent(String fileName) throws IOException { + return printDocumentSilent(fileName, false); + } + + /** + * Prints a PDF document without opening a Dialog box. + * @param file + * @return a process + * @throws IOException + */ + public static final Process printDocumentSilent(File file) throws IOException { + return printDocumentSilent(file, false); + } + + /** + * Launches a browser opening an URL. + * This code was based on the Public Domain class BareBonesBrowserLaunch, + * found at Centerkey. + * + * @param url the URL you want to open in the browser + * @throws IOException + */ + public static final void launchBrowser(String url) throws IOException { + try { + if (isMac()) { + Class macUtils = Class.forName("com.apple.mrj.MRJFileUtils"); + Method openURL = macUtils.getDeclaredMethod("openURL", new Class[] {String.class}); + openURL.invoke(null, new Object[] {url}); + } + else if (isWindows()) + Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url); + else { //assume Unix or Linux + String[] browsers = { + "firefox", "opera", "konqueror", "mozilla", "netscape" }; + String browser = null; + for (int count = 0; count < browsers.length && browser == null; count++) + if (Runtime.getRuntime().exec(new String[] {"which", browsers[count]}).waitFor() == 0) + browser = browsers[count]; + if (browser == null) + throw new Exception("Could not find web browser."); + else + Runtime.getRuntime().exec(new String[] {browser, url}); + } + } + catch (Exception e) { + throw new IOException("Error attempting to launch web browser"); + } + } + + /** + * Checks the Operating System. + * + * @return true if the current os is Windows + */ + public static boolean isWindows() { + String os = System.getProperty("os.name").toLowerCase(); + return os.indexOf("windows") != -1 || os.indexOf("nt") != -1; + } + + /** + * Checks the Operating System. + * + * @return true if the current os is Windows + */ + public static boolean isWindows9X() { + String os = System.getProperty("os.name").toLowerCase(); + return os.equals("windows 95") || os.equals("windows 98"); + } + + /** + * Checks the Operating System. + * + * @return true if the current os is Apple + */ + public static boolean isMac() { + String os = System.getProperty("os.name").toLowerCase(); + return os.indexOf("mac") != -1; + } + + /** + * Checks the Operating System. + * + * @return true if the current os is Linux + */ + public static boolean isLinux() { + String os = System.getProperty("os.name").toLowerCase(); + return os.indexOf("linux") != -1; + } +} \ No newline at end of file diff --git a/src/main/java/com/lowagie/tools/LPR.java b/src/main/java/com/lowagie/tools/LPR.java new file mode 100644 index 0000000..f969f8a --- /dev/null +++ b/src/main/java/com/lowagie/tools/LPR.java @@ -0,0 +1,716 @@ +/* + * Class based on freeware by Mario Muller (http://www.hemasoft.de/dev/lprj/ ) + * posted by Anonymous. */ +package com.lowagie.tools; + +import java.io.*; +import java.net.*; +import java.util.*; + +/** + * Modified! + * + * This class provides methods for the Line Printer Daemon Protocol
+ * more info about LPD/LPR + * + * @author Mario Müller + * @version 1.0 (12/98) + */ +public class LPR { + public static final int UNKNOWN = 0; + + private String host; + + private int port = 515; + + private String user = System.getProperty("user.name"); + + private Vector jobs = new Vector(); + + private String hostname = null; + + private String jobname = ""; + + private String cfAlen; + + private String cfA; + + private int copies = 1; + + private int timeout = 60000; + + private boolean cfA_formatted = false; // f + + private boolean cfA_postscript = false; // o + + private boolean cfA_banner = false; // L + + private boolean cfA_pr = false; + + /** + * default constructor without parameters, standard port is 515 + */ + public LPR() { + try { + hostname = InetAddress.getLocalHost().getHostName(); + } catch (Exception e2) { + System.out.println("can't resolve hostname"); + hostname = null; + } + } + + /** + * constuctor with host and username standard port is 515 + */ + public LPR(String host, String user) { + this(); + setHost(host); + setUser(user); + } + + /** + * constuctor with host, port and username + */ + public LPR(String host, int port, String user) { + this(); + setHost(host); + setUser(user); + setPort(port); + } + + /** + * set LPD host + */ + public void setHost(String value) { + host = value; + } + + /** + * get LPD host + */ + public String getHost() { + return host; + } + + /** + * set LPD port + */ + public void setPort(int value) { + port = value; + } + + /** + * get LPD port + */ + public int getPort() { + return port; + } + + /** + * set username + */ + public void setUser(String value) { + user = value; + } + + /** + * get username + */ + public String getUser() { + return user; + } + + /** + * set the timeout for any commands + */ + public void setTimeout(int timeout) { + this.timeout = timeout; + } + + /** + * get the timeout for any commands + */ + public int getTimeout() { + return timeout; + } + + /** + * get if file printed as postscript file + */ + public boolean getCfA_postscript() { + return cfA_postscript; + } + + /** + * set if file printed as postscript file + *

+ * o option in control file + *

+ *

+ * This command prints the data file to be printed, treating the data as + * standard Postscript input. + *

+ */ + public void setCfA_postscript(boolean value) { + cfA_postscript = value; + + if (value) { + cfA_formatted = false; + } + } + + /** + * get the job name + */ + public String getCfA_jobname() { + return jobname; + } + + /** + * set the job name + *

+ * J option in control file + *

+ *

+ * This command sets the job name to be printed on the banner page. The name + * of the job must be 99 or fewer octets. It can be omitted. The job name is + * conventionally used to display the name of the file or files which were + * "printed". It will be ignored unless the print banner command ('L') is + * also used. + *

+ */ + public void setCfA_jobname(String value) { + jobname = value; + cfA_banner = true; + } + + /** + * get if print file with 'pr' format + */ + public boolean getCfA_pr() { + return cfA_pr; + } + + /** + *

+ * p - Print file with 'pr' format + *

+ *

+ * This command causes the data file to be printed with a heading, page + * numbers, and pagination. The heading should include the date and time + * that printing was started, the title, and a page number identifier + * followed by the page number. The title is the name of file as specified + * by the 'N' command, unless the 'T' command (title) has been given. After + * a page of text has been printed, a new page is started with a new page + * number. (There is no way to specify the length of the page.) + *

+ */ + public void setCfA_pr(boolean value) { + cfA_pr = value; + } + + /** + * get if job printed with banner page + */ + public boolean getCfA_banner() { + return cfA_banner; + } + + /** + * set if job printed with banner page + *

+ * L option in control file + *

+ *

+ * This command causes the banner page to be printed. The user name can be + * omitted. The class name for banner page and job name for banner page + * commands must precede this command in the control file to be effective. + *

+ */ + public void setCfA_banner(boolean value) { + cfA_banner = value; + } + + /** + * get the host name for this computer + */ + public String getHostname() { + return hostname; + } + + /** + * set the host name for this computer + */ + public void setHostname(String value) { + hostname = value; + } + + /** + * set Copies + */ + public void setCopies(int value) { + copies = value; + } + + /** + * get Copies + */ + public int getCopies() { + return copies; + } + + /** + * Print any waiting jobs + *

+ * This command starts the printing process if it not already running. + *

+ */ + public boolean printWaitingJobs(String queue) { + Socket printer = connect(); + + if (printer != null) { + try { + BufferedReader in = new BufferedReader(new InputStreamReader( + printer.getInputStream())); + DataOutputStream out = new DataOutputStream(printer + .getOutputStream()); + +//-------------------------- start ------------------------------- + out.write(01); + out.writeBytes(queue + "\n"); + + if (in.read() != 0) { + System.err.println("Error while start print jobs on queue " + queue); + + return false; + } + + close(printer, in, out); + + return true; + } catch (Exception e) { + e.printStackTrace(); + } + } + + return false; + } + + /** + * Remove job + *

+ * This command deletes the print jobs from the specified queue which are + * listed as the other operands. If only the agent is given, the command is + * to delete the currently active job. Unless the agent is "root", it is not + * possible to delete a job which is not owned by the user. This is also the + * case for specifying user names instead of numbers. That is, agent "root" + * can delete jobs by user name but no other agents can. + *

+ */ + public boolean removeJob(String queue, String user, String jobid) { + Socket printer = connect(); + + if (printer != null) { + try { + BufferedReader in = new BufferedReader(new InputStreamReader( + printer.getInputStream())); + DataOutputStream out = new DataOutputStream(printer + .getOutputStream()); + +//-------------------------- start ------------------------------- + out.write(05); + out.writeBytes(queue + " " + user + " " + jobid + "\n"); + + if (in.read() != 0) { + System.err.println("Error while remove print job " + jobid + " on queue " + queue); + return false; + } + + close(printer, in, out); + + return true; + } catch (Exception e) { + e.printStackTrace(); + } + } + + return false; + } + + /** + * gets the state and description of the printer queue in short or long + * format + */ + public String getQueueState(String queue, boolean shortstate) { + Socket printer = connect(); + + if (printer != null) { + try { + BufferedReader in = new BufferedReader(new InputStreamReader( + printer.getInputStream())); + + DataOutputStream out = new DataOutputStream(printer + .getOutputStream()); + + if (shortstate) { + out.write(03); + } else { + out.write(04); + } + + out.writeBytes(queue + " \n"); + + LineNumberReader lnr = new LineNumberReader(in); + String line = in.readLine(); + close(printer, in, out); + + return line; + } catch (Exception e) { + e.printStackTrace(); + } + } + + return null; + } + + /** + * print a byte array with the document name as parameter + * + * @see LPR#print(String queue, String dfA, String document) { + */ + public String print(String queue, byte[] dfA, String document) { + PrintJob job = new PrintJob(queue, dfA, document); + job.start(); + + return job.getJobId(); + } + + /** + * print a String with the document name as parameter + * + * @see LPR#print(String queue, String dfA, String document) { + */ + public String print(String queue, String dfA, String document) { + PrintJob job = new PrintJob(queue, dfA.toCharArray(), document); + job.start(); + + return job.getJobId(); + } + + /** + * print a char array with the document name as parameter + * + * @see LPR#print(String queue, String dfA, String document) { + */ + public String print(String queue, char[] dfA, String document) { + PrintJob job = new PrintJob(queue, dfA, document); + job.start(); + + return job.getJobId(); + } + + /** + * print a file with the document name as parameter + * + * @see LPR#print(String queue, String dfA, String document) { + */ + public String print(String queue, File file, String document) { + if (file.exists()) { + try { + FileInputStream in = new FileInputStream(file); + byte[] data = new byte[(int) file.length()]; + int count = 0; + int size = (int) file.length(); + + while (count < size) { + count += in.read(data, count, size - count); + } + + in.close(); + + PrintJob job = new PrintJob(queue, data, document); + job.start(); + waitFor(job.getJobId()); + + return job.getJobId(); + } catch (Exception e) { + System.out.println("error while printig file " + file.getName() + + "\n" + e.getMessage()); + } + } else { + System.out.println("file " + file.getName() + " not found"); + } + + return null; + } + + /** + * wait until job is printed + */ + public void waitFor(String jobid) { + try { + while (jobs.contains(jobid)) { + Thread.sleep(500); + } + } catch (Exception e) { + } + } + + /** + * internal funktion for printing + */ + private boolean print(String queue, String document, byte[] dfA1, + char[] dfA2, int mode, String jobid) { + Socket printer = connect(); + + if (printer != null) { + boolean rc = true; + makecfA(document, jobid); + + String dfAlen = String.valueOf(((mode == 1) ? dfA1.length + : dfA2.length)); + + try { + BufferedReader in = new BufferedReader(new InputStreamReader( + printer.getInputStream())); + DataOutputStream out = new DataOutputStream(printer + .getOutputStream()); + System.out.println("jobid: " + jobid + " queue: " + queue); + +//-------------------------- start ------------------------------- + out.write(02); + out.writeBytes(queue + "\n"); + out.flush(); + + if (in.read() != 0) { + rc = false; + System.out.println("Error while start printing on queue " + + queue); + } + +//----------------------- write control file ---------------------------- + out.write(02); + out.writeBytes(cfAlen); + out.writeBytes(" "); + out.writeBytes("cfA" + jobid + user + "\n"); + out.flush(); + + if (in.read() != 0) { + rc = false; + System.out + .println("Error while start sending control file"); + } + + out.writeBytes(cfA); + out.writeByte(0); + out.flush(); + + if (in.read() != 0) { + rc = false; + System.out.println("Error while sending control file"); + } + +//----------------------- write data file ---------------------------- + out.write(03); + + /** + * Länge des Datenfiles + */ + out.writeBytes(dfAlen); + out.writeBytes(" "); + out.writeBytes("dfA" + jobid + user + "\n"); + out.flush(); + + if (in.read() != 0) { + rc = false; + System.err.println("Error while start sending data file"); + } + + if (mode == 1) { + out.write(dfA1); + } else { + out.writeBytes(new String(dfA2)); + } + + out.writeByte(0); + out.flush(); + + if (in.read() != 0) { + rc = false; + System.out.println("Error while sending data file"); + } + out.flush(); + close(printer, in, out); + } catch (Exception e) { + rc = false; + e.printStackTrace(); + } + + return rc; + } + + return false; + } + + /** + * connect to the LPD Server + */ + private Socket connect() { + try { + System.out.println("Connect with " + host); + + Socket socket = new Socket(InetAddress.getByName(host), port); + socket.setSoTimeout(timeout); + + return socket; + } catch (Exception e1) { + System.out + .println("Error while connecting to " + host + ":" + port); + System.out.println(e1.getMessage()); + } + + return null; + } + + /** + * closed connection to the LPD + */ + private void close(Socket socket, BufferedReader in, DataOutputStream out) { + try { + in.close(); + out.close(); + socket.close(); + } catch (Exception e) { + System.out.println("Errror while closing printerport"); + System.out.println(e.getMessage()); + } + } + + /** + * internal methode to create the control file + */ + private void makecfA(String document, String jobid) { + cfA = ""; + + if (hostname != null) { + cfA += ("H" + hostname + "\n"); + } + + cfA += ("P" + user + "\n"); + cfA += ("J" + document + "\n"); + cfA += ("L" + user + "\n"); + + if (cfA_formatted) { + cfA += ("f" + document + "\n"); + } + + if (cfA_postscript) { + cfA += ("o" + document + "\n"); + } + + if (cfA_banner) { + cfA += ("L" + jobname + "\n"); + } + + cfA += ("UdfA" + jobid + hostname + "\n"); + + for (int i = 0; i < copies; i++) { + cfA += ("ldfA" + jobid + user + "\n"); + } + + cfA += ("N" + document + "\n"); + cfAlen = String.valueOf(cfA.length()); + + System.out.println(cfA); + + } + + private String getNewJobId() { + return fillLeft(String.valueOf((int) Math.floor(Math.random() * 999)), + 3, "0"); + } + + private String fillLeft(String data, int size, String filler) { + while (data.length() < size) { + data = filler + data; + } + + return data; + } + + /** ******************* Job ************************ */ + private class PrintJob extends Thread { + String queue; + + String document; + + String id; + + int mode; + + private byte[] dfA1 = null; + + private char[] dfA2 = null; + + public PrintJob(String queue, byte[] data, String document) { + super(); + this.queue = queue; + this.dfA1 = data; + this.document = document; + this.mode = 1; + this.id = getNewJobId(); + jobs.addElement(id); + System.out.println("Printjob (byte) " + id + " queue=" + queue + " document=" + document); + } + + public PrintJob(String queue, char[] data, String document) { + super(); + this.queue = queue; + this.dfA2 = data; + this.document = document; + this.mode = 2; + this.id = getNewJobId(); + jobs.addElement(id); + System.out.println("Printjob (char) " + id + " queue=" + queue + " document=" + document); + } + + public void run() { + boolean rc = false; + int i = 0; + System.out.println("Printjob (char) " + id + " queue=" + queue + " document=" + document); + + while (!rc) { + try { + i++; + rc = print(queue, document, dfA1, dfA2, mode, id); + } catch (Exception e) { + System.err.println(e.getMessage()); + } + + if (!rc && (i > 5)) { + break; + } + + if (!rc) { + try { + Thread.sleep(1000); + } catch (InterruptedException ex) { + break; + } + } + } + + jobs.removeElement(id); + + if (rc) { + System.out.println("Job " + id + " allready printed and removed from queue (" + i + " trys)"); + } else { + System.out.println("Job " + id + " not printed and removed from queue (" + i + " trys)"); + } + } + + public String getJobId() { + return this.id; + } + } +} \ No newline at end of file diff --git a/src/main/java/com/lowagie/tools/SwingWorker.java b/src/main/java/com/lowagie/tools/SwingWorker.java new file mode 100644 index 0000000..634a61a --- /dev/null +++ b/src/main/java/com/lowagie/tools/SwingWorker.java @@ -0,0 +1,127 @@ +package com.lowagie.tools; +import javax.swing.SwingUtilities; + +/** + * This is the 3rd version of SwingWorker (also known as + * SwingWorker 3), an abstract class that you subclass to + * perform GUI-related work in a dedicated thread. For + * instructions on using this class, see: + * + * http://java.sun.com/docs/books/tutorial/uiswing/misc/threads.html + */ +public abstract class SwingWorker { + private Object value; // see getValue(), setValue() + private Thread thread; + + /** + * Class to maintain reference to current worker thread + * under separate synchronization control. + */ + private static class ThreadVar { + private Thread thread; + ThreadVar(Thread t) { thread = t; } + synchronized Thread get() { return thread; } + synchronized void clear() { thread = null; } + } + + private ThreadVar threadVar; + + /** + * Get the value produced by the worker thread, or null if it + * hasn't been constructed yet. + */ + protected synchronized Object getValueX() { + return value; + } + + /** + * Set the value produced by worker thread + */ + private synchronized void setValueX(Object x) { + value = x; + } + + /** + * Compute the value to be returned by the get method. + */ + public abstract Object construct(); + + /** + * Called on the event dispatching thread (not on the worker thread) + * after the construct method has returned. + */ + public void finished() { + } + + /** + * A new method that interrupts the worker thread. Call this method + * to force the worker to stop what it's doing. + */ + public void interrupt() { + Thread t = threadVar.get(); + if (t != null) { + t.interrupt(); + } + threadVar.clear(); + } + + /** + * Return the value created by the construct method. + * Returns null if either the constructing thread or the current + * thread was interrupted before a value was produced. + * + * @return the value created by the construct method + */ + public Object get() { + while (true) { + Thread t = threadVar.get(); + if (t == null) { + return getValueX(); + } + try { + t.join(); + } + catch (InterruptedException e) { + Thread.currentThread().interrupt(); // propagate + return null; + } + } + } + + + /** + * Start a thread that will call the construct method + * and then exit. + */ + public SwingWorker() { + final Runnable doFinished = new Runnable() { + public void run() { finished(); } + }; + + Runnable doConstruct = new Runnable() { + public void run() { + try { + setValueX(construct()); + } + finally { + threadVar.clear(); + } + + SwingUtilities.invokeLater(doFinished); + } + }; + + Thread t = new Thread(doConstruct); + threadVar = new ThreadVar(t); + } + + /** + * Start the worker thread. + */ + public void start() { + Thread t = threadVar.get(); + if (t != null) { + t.start(); + } + } +} diff --git a/src/main/java/com/lowagie/tools/ToolMenuItems.java b/src/main/java/com/lowagie/tools/ToolMenuItems.java new file mode 100644 index 0000000..fdcf916 --- /dev/null +++ b/src/main/java/com/lowagie/tools/ToolMenuItems.java @@ -0,0 +1,84 @@ +/* + * $Id: ToolMenuItems.java,v 1.4 2005/08/29 08:44:16 blowagie Exp $ + * $Name: $ + * + * Copyright 2005 by Bruno Lowagie. + * + * The contents of this file are subject to the Mozilla Public License Version 1.1 + * (the "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the License. + * + * The Original Code is 'iText, a free JAVA-PDF library'. + * + * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by + * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie. + * All Rights Reserved. + * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer + * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved. + * + * Contributor(s): all the names of the contributors are added in the source code + * where applicable. + * + * Alternatively, the contents of this file may be used under the terms of the + * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the + * provisions of LGPL are applicable instead of those above. If you wish to + * allow use of your version of this file only under the terms of the LGPL + * License and not to allow others to use your version of this file under + * the MPL, indicate your decision by deleting the provisions above and + * replace them with the notice and other provisions required by the LGPL. + * If you do not delete the provisions above, a recipient may use your version + * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE. + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the MPL as stated above or under the terms of the GNU + * Library General Public License as published by the Free Software Foundation; + * either version 2 of the License, or any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more + * details. + * + * If you didn't download this code from the following link, you should check if + * you aren't using an obsolete version: + * http://www.lowagie.com/iText/ + */ +package com.lowagie.tools; + +/** + * Keeps all the possible menuitems. + */ +public interface ToolMenuItems { + + /** An item in the menubar. */ + public static final String FILE = "File"; + /** An item in the menubar. */ + public static final String CLOSE = "Close"; + /** An item in the menubar. */ + public static final String TOOLS = "Tools"; + /** An item in the menubar. */ + public static final String HELP = "Help"; + /** An item in the menubar. */ + public static final String VERSION = "Version"; + /** An item in the menubar. */ + public static final String ABOUT = "About"; + /** An item in the menubar. */ + public static final String TOOL = "Tool"; + /** An item in the menubar. */ + public static final String USAGE = "Usage"; + /** An item in the menubar. */ + public static final String ARGUMENTS = "Arguments"; + /** An item in the menubar. */ + public static final String EXECUTE = "Execute"; + /** An item in the menubar. */ + public static final String EXECUTESHOW = "Execute+Open"; + /** An item in the menubar. */ + public static final String EXECUTEPRINT = "Execute+Printdialog"; + /** An item in the menubar. */ + public static final String EXECUTEPRINTSILENT = "Execute+Print"; + +} diff --git a/src/main/java/com/lowagie/tools/Toolbox.java b/src/main/java/com/lowagie/tools/Toolbox.java new file mode 100644 index 0000000..89a0bc3 --- /dev/null +++ b/src/main/java/com/lowagie/tools/Toolbox.java @@ -0,0 +1,354 @@ +/* + * $Id: Toolbox.java,v 1.16 2006/05/29 10:30:39 blowagie Exp $ + * $Name: $ + * + * Copyright 2005 by Bruno Lowagie. + * + * The contents of this file are subject to the Mozilla Public License Version 1.1 + * (the "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the License. + * + * The Original Code is 'iText, a free JAVA-PDF library'. + * + * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by + * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie. + * All Rights Reserved. + * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer + * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved. + * + * Contributor(s): all the names of the contributors are added in the source code + * where applicable. + * + * Alternatively, the contents of this file may be used under the terms of the + * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the + * provisions of LGPL are applicable instead of those above. If you wish to + * allow use of your version of this file only under the terms of the LGPL + * License and not to allow others to use your version of this file under + * the MPL, indicate your decision by deleting the provisions above and + * replace them with the notice and other provisions required by the LGPL. + * If you do not delete the provisions above, a recipient may use your version + * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE. + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the MPL as stated above or under the terms of the GNU + * Library General Public License as published by the Free Software Foundation; + * either version 2 of the License, or any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more + * details. + * + * If you didn't download this code from the following link, you should check if + * you aren't using an obsolete version: + * http://www.lowagie.com/iText/ + */ +package com.lowagie.tools; + +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.KeyEvent; +import java.beans.PropertyVetoException; +import java.io.IOException; +import java.io.PipedInputStream; +import java.io.PipedOutputStream; +import java.io.PrintStream; +import java.util.Iterator; +import java.util.Properties; +import java.util.TreeMap; + +import javax.swing.Box; +import javax.swing.JDesktopPane; +import javax.swing.JFrame; +import javax.swing.JInternalFrame; +import javax.swing.JMenu; +import javax.swing.JMenuBar; +import javax.swing.JMenuItem; +import javax.swing.JOptionPane; +import javax.swing.JScrollPane; +import javax.swing.JSplitPane; +import javax.swing.JTextArea; + +import com.lowagie.tools.plugins.AbstractTool; + +import java.awt.Toolkit; +import java.awt.Dimension; + +/** + * This is a utility that allows you to use a number of iText tools. + */ +public class Toolbox extends JFrame implements ToolMenuItems, ActionListener { + + /** The DesktopPane of the toolbox. */ + private JDesktopPane desktop; + /** The ConsolePane of the toolbox. */ + private JScrollPane console; + + /** The list of tools in the toolbox. */ + private Properties toolmap = new Properties(); + + /** x-coordinate of the location of a new internal frame. */ + private int locationX = 0; + + /** y-coordinate of the location of a new internal frame. */ + private int locationY = 0; + + /** + * Constructs the Toolbox object. + */ + public Toolbox() { + super(); + setSize(600, 500); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setResizable(true); + setTitle("iText Toolbox"); + setJMenuBar(getMenubar()); + desktop = new JDesktopPane(); + Console c; + try { + c = new Console(); + console = new JScrollPane(c.textArea); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, desktop, console); + splitPane.setContinuousLayout(true); + splitPane.setOneTouchExpandable(true); + splitPane.setDividerLocation(300); + setContentPane(splitPane); + centerFrame(this); + setVisible(true); + } + + /** + * Starts the Toolbox utility. + * + * @param args + * no arguments needed + */ + public static void main(String[] args) { + Toolbox toolbox = new Toolbox(); + if(args.length==0){ + + }else{ + try { + AbstractTool tool = toolbox.createFrame(args[0]); + String[] nargs=new String[args.length-1]; + System.arraycopy(args,1,nargs,0,args.length-1); + tool.setArguments(nargs); + tool.execute(); + } + catch (PropertyVetoException ex) { + } + catch (ClassNotFoundException ex) { + } + catch (IllegalAccessException ex) { + } + catch (InstantiationException ex) { + } + } + } + + /** + * Gets the menubar. + * + * @return a menubar + */ + private JMenuBar getMenubar() { + Properties p = new Properties(); + try { + p.load(Toolbox.class.getClassLoader().getResourceAsStream( + "com/lowagie/tools/plugins/tools.txt")); + } catch (IOException e) { + e.printStackTrace(); + } + toolmap = new Properties(); + TreeMap tmp = new TreeMap(); + tmp.putAll(p); + JMenuBar menubar = new JMenuBar(); + JMenu file = new JMenu(FILE); + JMenuItem close = new JMenuItem(CLOSE); + close.setMnemonic(KeyEvent.VK_C); + close.addActionListener(this); + file.add(close); + JMenu tools = new JMenu(TOOLS); + file.setMnemonic(KeyEvent.VK_T); + String name, tool; + JMenu current = null; + JMenuItem item; + for (Iterator i = tmp.keySet().iterator(); i.hasNext();) { + name = (String) i.next(); + if (current == null || !name.startsWith(current.getText())) { + current = new JMenu(name.substring(0, name.indexOf("."))); + tools.add(current); + } + item = new JMenuItem(name.substring(current.getText().length() + 1)); + item.addActionListener(this); + tool = (String) tmp.get(name); + try { + Class.forName(tool); + toolmap.put(item.getText(), tool); + current.add(item); + } catch (ClassNotFoundException e) { + System.err.println("Plugin " + name + + " was not found in your CLASSPATH."); + } + } + JMenu help = new JMenu(HELP); + JMenuItem about = new JMenuItem(ABOUT); + about.setMnemonic(KeyEvent.VK_A); + about.addActionListener(this); + help.add(about); + JMenuItem versions = new JMenuItem(VERSION); + versions.addActionListener(this); + help.add(versions); + menubar.add(file); + menubar.add(tools); + menubar.add(Box.createGlue()); + menubar.add(help); + return menubar; + } + + /** + * Creates an Internal Frame. + * + * @param name + * the name of the app + * @throws ClassNotFoundException + * @throws IllegalAccessException + * @throws InstantiationException + * @throws PropertyVetoException + */ + public AbstractTool createFrame(String name) throws InstantiationException, + IllegalAccessException, ClassNotFoundException, + PropertyVetoException { + AbstractTool ti = (AbstractTool) Class.forName( + (String) toolmap.get(name)).newInstance(); + JInternalFrame f = ti.getInternalFrame(); + f.setLocation(locationX, locationY); + locationX += 25; + if (locationX > this.getWidth() + 50) + locationX = 0; + locationY += 25; + if (locationY > this.getHeight() + 50) + locationY = 0; + f.setVisible(true); + desktop.add(f); + f.setSelected(true); + return ti; + } + + public static void centerFrame(JFrame f) { + Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); + Dimension frameSize = f.getSize(); + if (frameSize.height > screenSize.height) { + frameSize.height = screenSize.height; + } + if (frameSize.width > screenSize.width) { + frameSize.width = screenSize.width; + } + f.setLocation((screenSize.width - frameSize.width) / 2, + (screenSize.height - frameSize.height) / 2); + } + + /** + * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent) + */ + public void actionPerformed(ActionEvent evt) { + if (CLOSE.equals(evt.getActionCommand())) { + System.out.println("The Toolbox is closed."); + System.exit(0); + } else if (ABOUT.equals(evt.getActionCommand())) { + System.out + .println("The iText Toolbox is part of iText, a Free Java-PDF Library.\nVisit http://www.lowagie.com/iText/toolbox.html for more info."); + try { + Executable + .launchBrowser("http://www.lowagie.com/iText/toolbox.html"); + } catch (IOException ioe) { + JOptionPane + .showMessageDialog( + this, + "The iText Toolbox is part of iText, a Free Java-PDF Library.\nVisit http://www.lowagie.com/iText/toolbox.html for more info."); + } + } else if (VERSION.equals(evt.getActionCommand())) { + JFrame f = new Versions(); + centerFrame(f); + f.setVisible(true); + } else { + try { + createFrame(evt.getActionCommand()); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + /** + * A Class that redirects output to System.out and System.err. + */ + public class Console { + PipedInputStream piOut; + PipedInputStream piErr; + PipedOutputStream poOut; + PipedOutputStream poErr; + JTextArea textArea = new JTextArea(); + + /** + * Creates a new Console object. + * @param columns + * @param rows + * @throws IOException + */ + public Console() throws IOException { + // Set up System.out + piOut = new PipedInputStream(); + poOut = new PipedOutputStream(piOut); + System.setOut(new PrintStream(poOut, true)); + + // Set up System.err + piErr = new PipedInputStream(); + poErr = new PipedOutputStream(piErr); + System.setErr(new PrintStream(poErr, true)); + + // Add a scrolling text area + textArea.setEditable(false); + + // Create reader threads + new ReaderThread(piOut).start(); + new ReaderThread(piErr).start(); + } + + class ReaderThread extends Thread { + PipedInputStream pi; + + ReaderThread(PipedInputStream pi) { + this.pi = pi; + } + + /** + * @see java.lang.Thread#run() + */ + public void run() { + final byte[] buf = new byte[1024]; + + while (true) { + try { + final int len = pi.read(buf); + if (len == -1) { + break; + } + textArea.append(new String(buf, 0, len)); + textArea.setCaretPosition(textArea.getDocument().getLength()); + + } catch (IOException e) { + } + } + } + } + } + +} diff --git a/src/main/java/com/lowagie/tools/Versions.java b/src/main/java/com/lowagie/tools/Versions.java new file mode 100644 index 0000000..dd0796f --- /dev/null +++ b/src/main/java/com/lowagie/tools/Versions.java @@ -0,0 +1,143 @@ +/* + * $Id: Versions.java,v 1.7 2006/02/17 11:54:42 psoares33 Exp $ + * $Name: $ + * + * Copyright 2005 by Carsten Hammer. + * + * The contents of this file are subject to the Mozilla Public License Version 1.1 + * (the "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the License. + * + * The Original Code is 'iText, a free JAVA-PDF library'. + * + * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by + * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie. + * All Rights Reserved. + * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer + * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved. + * + * Contributor(s): all the names of the contributors are added in the source code + * where applicable. + * + * Alternatively, the contents of this file may be used under the terms of the + * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the + * provisions of LGPL are applicable instead of those above. If you wish to + * allow use of your version of this file only under the terms of the LGPL + * License and not to allow others to use your version of this file under + * the MPL, indicate your decision by deleting the provisions above and + * replace them with the notice and other provisions required by the LGPL. + * If you do not delete the provisions above, a recipient may use your version + * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE. + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the MPL as stated above or under the terms of the GNU + * Library General Public License as published by the Free Software Foundation; + * either version 2 of the License, or any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more + * details. + * + * If you didn't download this code from the following link, you should check if + * you aren't using an obsolete version: + * http://www.lowagie.com/iText/ + */ +package com.lowagie.tools; + +import java.awt.*; +import javax.swing.*; +import java.awt.BorderLayout; +import com.lowagie.text.Document; +import com.lowagie.tools.plugins.*; +import java.util.Iterator; +import java.util.TreeSet; +import java.awt.Dimension; +import java.util.Properties; + +/** + * JFrame that shows the versions of all the plugins. + */ +public class Versions extends JFrame { + /** + * Constructs a JFrame. + */ + public Versions() { + super("Plugins and their version"); + try { + jbInit(); + } catch (Exception ex) { + ex.printStackTrace(); + } + } + + /** + * Main method (test purposes only) + * + * @param args + */ + public static void main(String[] args) { + Versions untitled1 = new Versions(); + } + + private void jbInit() throws Exception { + this.getContentPane().setLayout(borderLayout1); + StringBuffer sb = new StringBuffer(); + sb.append(""); + + Iterator it = new TreeSet(AbstractTool.versionsarray).iterator(); + + while (it.hasNext()) { + sb.append("

"); + sb.append((String) it.next()); + sb.append("

"); + } + + sb.append(""); + jLabel1.setText(sb.toString()); + jScrollPane1.setMinimumSize(new Dimension(160, 140)); + this.getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER); + Properties properties = System.getProperties(); + Runtime runtime = Runtime.getRuntime(); + sb = new StringBuffer(); + sb.append(""); + sb.append("

iText version: " + + Document.getVersion() + "

"); + sb.append("

java.version: " + + properties.getProperty("java.version") + "

"); + sb.append("

java.vendor: " + + properties.getProperty("java.vendor") + "

"); + sb.append("

java.home: " + properties.getProperty("java.home") + + "

"); + sb.append("

java.freeMemory: " + + String.valueOf(runtime.freeMemory()) + " bytes" + "

"); + sb.append("

java.totalMemory: " + + String.valueOf(runtime.totalMemory()) + " bytes" + "

"); + sb.append("

user.home: " + properties.getProperty("user.home") + + "

"); + sb.append("

os.name: " + properties.getProperty("os.name") + + "

"); + sb.append("

os.arch: " + properties.getProperty("os.arch") + + "

"); + sb.append("

os.version: " + properties.getProperty("os.version") + + "

"); + sb.append(""); + jLabel2.setText(sb.toString()); + jScrollPane1.setViewportView(jLabel1); + this.getContentPane().add(jLabel2, java.awt.BorderLayout.NORTH); + pack(); + } + + JLabel jLabel1 = new JLabel(); + + BorderLayout borderLayout1 = new BorderLayout(); + + JLabel jLabel2 = new JLabel(); + + JScrollPane jScrollPane1 = new JScrollPane(); +} + diff --git a/src/main/java/com/lowagie/tools/arguments/BitsetArgument.java b/src/main/java/com/lowagie/tools/arguments/BitsetArgument.java new file mode 100644 index 0000000..63d79af --- /dev/null +++ b/src/main/java/com/lowagie/tools/arguments/BitsetArgument.java @@ -0,0 +1,136 @@ +/* + * $Id: BitsetArgument.java,v 1.5 2006/05/30 09:13:00 blowagie Exp $ + * $Name: $ + * + * Copyright 2005 by Bruno Lowagie. + * + * The contents of this file are subject to the Mozilla Public License Version 1.1 + * (the "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the License. + * + * The Original Code is 'iText, a free JAVA-PDF library'. + * + * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by + * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie. + * All Rights Reserved. + * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer + * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved. + * + * Contributor(s): all the names of the contributors are added in the source code + * where applicable. + * + * Alternatively, the contents of this file may be used under the terms of the + * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the + * provisions of LGPL are applicable instead of those above. If you wish to + * allow use of your version of this file only under the terms of the LGPL + * License and not to allow others to use your version of this file under + * the MPL, indicate your decision by deleting the provisions above and + * replace them with the notice and other provisions required by the LGPL. + * If you do not delete the provisions above, a recipient may use your version + * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE. + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the MPL as stated above or under the terms of the GNU + * Library General Public License as published by the Free Software Foundation; + * either version 2 of the License, or any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more + * details. + * + * If you didn't download this code from the following link, you should check if + * you aren't using an obsolete version: + * http://www.lowagie.com/iText/ + */ +package com.lowagie.tools.arguments; + +import java.awt.event.ActionEvent; + +import javax.swing.JCheckBox; +import javax.swing.JOptionPane; + +import com.lowagie.tools.plugins.AbstractTool; + +/** + * Argument that results in a set of "1" and "0" values. + */ +public class BitsetArgument extends ToolArgument { + /** These are the different options that can be true or false. */ + private JCheckBox[] options; + + /** + * Constructs an BitsetArgument. + * @param tool the tool that needs this argument + * @param name the name of the argument + * @param description the description of the argument + * @param options the different options that can be true or false + */ + public BitsetArgument(AbstractTool tool, String name, String description, String[] options) { + super(tool, name, description, String.class.getName()); + this.options = new JCheckBox[options.length]; + for (int i = 0; i < options.length; i++) { + this.options[i] = new JCheckBox(options[i]); + } + } + + /** + * Gets the argument as an object. + * @return an object + * @throws InstantiationException + */ + public Object getArgument() throws InstantiationException { + return value; + } + + /** + * @see com.lowagie.tools.arguments.ToolArgument#getUsage() + */ + public String getUsage() { + StringBuffer buf = new StringBuffer(super.getUsage()); + buf.append(" possible options:\n"); + for (int i = 0; i < options.length; i++) { + buf.append(" - "); + buf.append(options[i].getText()); + buf.append("\n"); + } + return buf.toString(); + } + + /** + * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent) + */ + public void actionPerformed(ActionEvent evt) { + Object[] message = new Object[1 + options.length]; + message[0] = "Check the options you need:"; + for(int i = 0; i < options.length; i++ ) { + message[i+1] = options[i]; + } + int result = JOptionPane.showOptionDialog( + tool.getInternalFrame(), + message, + description, + JOptionPane.OK_CANCEL_OPTION, + JOptionPane.QUESTION_MESSAGE, + null, + null, + null + ); + if (result == 0) { + StringBuffer buf = new StringBuffer(); + for (int i = 0; i < options.length; i++) { + if (options[i].isSelected()) { + buf.append("1"); + } + else { + buf.append("0"); + } + } + setValue(buf.toString()); + } + } +} diff --git a/src/main/java/com/lowagie/tools/arguments/DirFilter.java b/src/main/java/com/lowagie/tools/arguments/DirFilter.java new file mode 100644 index 0000000..def64a4 --- /dev/null +++ b/src/main/java/com/lowagie/tools/arguments/DirFilter.java @@ -0,0 +1,65 @@ +/* + * Copyright 2005 by Johannes Schindelin. + * + * The contents of this file are subject to the Mozilla Public License Version 1.1 + * (the "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the License. + * + * The Original Code is 'iText, a free JAVA-PDF library'. + * + * + * Alternatively, the contents of this file may be used under the terms of the + * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the + * provisions of LGPL are applicable instead of those above. If you wish to + * allow use of your version of this file only under the terms of the LGPL + * License and not to allow others to use your version of this file under + * the MPL, indicate your decision by deleting the provisions above and + * replace them with the notice and other provisions required by the LGPL. + * If you do not delete the provisions above, a recipient may use your version + * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE. + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the MPL as stated above or under the terms of the GNU + * Library General Public License as published by the Free Software Foundation; + * either version 2 of the License, or any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more + * details. + * + * If you didn't download this code from the following link, you should check if + * you aren't using an obsolete version: + * http://www.lowagie.com/iText/ + */ +package com.lowagie.tools.arguments; + +import java.io.File; + +import javax.swing.filechooser.FileFilter; + +/** + * Filters directories in a JFileChooser. + */ +public class DirFilter extends FileFilter { + + /** + * @see javax.swing.filechooser.FileFilter#accept(java.io.File) + */ + public boolean accept(File f) { + if (f.isDirectory()) return true; + return false; + } + + /** + * @see javax.swing.filechooser.FileFilter#getDescription() + */ + public String getDescription() { + return "directories"; + } + +} diff --git a/src/main/java/com/lowagie/tools/arguments/FileArgument.java b/src/main/java/com/lowagie/tools/arguments/FileArgument.java new file mode 100644 index 0000000..f110bbe --- /dev/null +++ b/src/main/java/com/lowagie/tools/arguments/FileArgument.java @@ -0,0 +1,165 @@ +/* + * $Id: FileArgument.java,v 1.7 2006/05/30 09:13:00 blowagie Exp $ + * $Name: $ + * + * Copyright 2005 by Bruno Lowagie. + * + * The contents of this file are subject to the Mozilla Public License Version 1.1 + * (the "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the License. + * + * The Original Code is 'iText, a free JAVA-PDF library'. + * + * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by + * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie. + * All Rights Reserved. + * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer + * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved. + * + * Contributor(s): all the names of the contributors are added in the source code + * where applicable. + * + * Alternatively, the contents of this file may be used under the terms of the + * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the + * provisions of LGPL are applicable instead of those above. If you wish to + * allow use of your version of this file only under the terms of the LGPL + * License and not to allow others to use your version of this file under + * the MPL, indicate your decision by deleting the provisions above and + * replace them with the notice and other provisions required by the LGPL. + * If you do not delete the provisions above, a recipient may use your version + * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE. + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the MPL as stated above or under the terms of the GNU + * Library General Public License as published by the Free Software Foundation; + * either version 2 of the License, or any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more + * details. + * + * If you didn't download this code from the following link, you should check if + * you aren't using an obsolete version: + * http://www.lowagie.com/iText/ + */ +package com.lowagie.tools.arguments; + +import java.awt.event.ActionEvent; +import java.io.File; + +import javax.swing.JFileChooser; +import javax.swing.filechooser.FileFilter; + +import com.lowagie.tools.plugins.AbstractTool; + +/** + * ToolArgument class if the argument is a java.io.File. + */ +public class FileArgument extends ToolArgument { + /** a filter to put on the FileChooser. */ + private FileFilter filter; + /** indicates if the argument has to point to a new or an existing file. */ + private boolean newFile; + /** the label */ + LabelAccessory label = null; +// final static String PROPERTYFILENAME="inputfilename"; + /** + * Constructs a FileArgument. + * @param tool the tool that needs this argument + * @param name the name of the argument + * @param description the description of the argument + * @param newFile makes the difference between an Open or Save dialog + * @param filter + */ + public FileArgument(AbstractTool tool, String name, String description, boolean newFile, FileFilter filter) { + super(tool, name, description, File.class.getName()); + this.newFile = newFile; + this.filter = filter; + } + /** + * Constructs a FileArgument. + * @param tool the tool that needs this argument + * @param name the name of the argument + * @param description the description of the argument + * @param newFile makes the difference between an Open or Save dialog + */ + public FileArgument(AbstractTool tool, String name, String description, boolean newFile) { + this(tool, name, description, newFile, null); + } + + /** + * Gets the argument as an object. + * @return an object + * @throws InstantiationException + */ + public Object getArgument() throws InstantiationException { + if (value == null) return null; + try { + return new File(value); + } catch (Exception e) { + throw new InstantiationException(e.getMessage()); + } + } + + /** + * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent) + */ + public void actionPerformed(ActionEvent e) { + JFileChooser fc = new JFileChooser(); + + if (filter != null) { + fc.setFileFilter(filter); + if (filter instanceof DirFilter) + fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); + } + if (label != null) { + fc.setAccessory(label); + fc.addPropertyChangeListener( + JFileChooser.SELECTED_FILE_CHANGED_PROPERTY, label); + } + if (newFile) { + fc.showSaveDialog(tool.getInternalFrame()); + } + else { + fc.showOpenDialog(tool.getInternalFrame()); + } + try { + setValue(fc.getSelectedFile().getAbsolutePath()); + } + catch(NullPointerException npe) { + } + } + + /** + * @return Returns the filter. + */ + public FileFilter getFilter() { + return filter; + } + /** + * @param filter The filter to set. + */ + public void setFilter(FileFilter filter) { + this.filter = filter; + } + /** + * @return Returns the label. + */ + public LabelAccessory getLabel() { + return label; + } + /** + * @param label The label to set. + */ + public void setLabel(LabelAccessory label) { + this.label = label; + } + + + +} diff --git a/src/main/java/com/lowagie/tools/arguments/ImageArgument.java b/src/main/java/com/lowagie/tools/arguments/ImageArgument.java new file mode 100644 index 0000000..063d347 --- /dev/null +++ b/src/main/java/com/lowagie/tools/arguments/ImageArgument.java @@ -0,0 +1,117 @@ +/* + * $Id: ImageArgument.java,v 1.4 2006/05/30 09:13:00 blowagie Exp $ + * $Name: $ + * + * Copyright 2005 by Bruno Lowagie. + * + * The contents of this file are subject to the Mozilla Public License Version 1.1 + * (the "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the License. + * + * The Original Code is 'iText, a free JAVA-PDF library'. + * + * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by + * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie. + * All Rights Reserved. + * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer + * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved. + * + * Contributor(s): all the names of the contributors are added in the source code + * where applicable. + * + * Alternatively, the contents of this file may be used under the terms of the + * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the + * provisions of LGPL are applicable instead of those above. If you wish to + * allow use of your version of this file only under the terms of the LGPL + * License and not to allow others to use your version of this file under + * the MPL, indicate your decision by deleting the provisions above and + * replace them with the notice and other provisions required by the LGPL. + * If you do not delete the provisions above, a recipient may use your version + * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE. + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the MPL as stated above or under the terms of the GNU + * Library General Public License as published by the Free Software Foundation; + * either version 2 of the License, or any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more + * details. + * + * If you didn't download this code from the following link, you should check if + * you aren't using an obsolete version: + * http://www.lowagie.com/iText/ + */ +package com.lowagie.tools.arguments; + +import java.awt.event.ActionEvent; + +import javax.swing.JFileChooser; +import javax.swing.filechooser.FileFilter; + +import com.lowagie.text.Image; +import com.lowagie.tools.plugins.AbstractTool; + +/** + * ToolArgument class if the argument is a com.lowagie.text.Image. + */ +public class ImageArgument extends ToolArgument { + /** a filter to put on the FileChooser. */ + private FileFilter filter; + + /** + * Constructs a FileArgument. + * @param tool the tool that needs this argument + * @param name the name of the argument + * @param description the description of the argument + * @param filter a custom filter + */ + public ImageArgument(AbstractTool tool, String name, String description, FileFilter filter) { + super(tool, name, description, Image.class.getName()); + this.filter = filter; + } + + /** + * Constructs a FileArgument. + * @param tool the tool that needs this argument + * @param name the name of the argument + * @param description the description of the argument + */ + public ImageArgument(AbstractTool tool, String name, String description) { + this(tool, name, description, new ImageFilter()); + } + + /** + * Gets the argument as an object. + * @return an object + * @throws InstantiationException + */ + public Object getArgument() throws InstantiationException { + if (value == null) return null; + try { + return Image.getInstance(value); + } catch (Exception e) { + throw new InstantiationException(e.getMessage()); + } + } + + /** + * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent) + */ + public void actionPerformed(ActionEvent e) { + JFileChooser fc = new JFileChooser(); + if (filter != null) fc.setFileFilter(filter); + fc.showOpenDialog(tool.getInternalFrame()); + try { + setValue(fc.getSelectedFile().getAbsolutePath()); + } + catch(NullPointerException npe) { + } + } + +} diff --git a/src/main/java/com/lowagie/tools/arguments/ImageFilter.java b/src/main/java/com/lowagie/tools/arguments/ImageFilter.java new file mode 100644 index 0000000..24c1d64 --- /dev/null +++ b/src/main/java/com/lowagie/tools/arguments/ImageFilter.java @@ -0,0 +1,135 @@ +/* + * $Id: ImageFilter.java,v 1.2 2006/02/02 18:50:07 psoares33 Exp $ + * $Name: $ + * + * Copyright 2005 by Bruno Lowagie. + * + * The contents of this file are subject to the Mozilla Public License Version 1.1 + * (the "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the License. + * + * The Original Code is 'iText, a free JAVA-PDF library'. + * + * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by + * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie. + * All Rights Reserved. + * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer + * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved. + * + * Contributor(s): all the names of the contributors are added in the source code + * where applicable. + * + * Alternatively, the contents of this file may be used under the terms of the + * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the + * provisions of LGPL are applicable instead of those above. If you wish to + * allow use of your version of this file only under the terms of the LGPL + * License and not to allow others to use your version of this file under + * the MPL, indicate your decision by deleting the provisions above and + * replace them with the notice and other provisions required by the LGPL. + * If you do not delete the provisions above, a recipient may use your version + * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE. + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the MPL as stated above or under the terms of the GNU + * Library General Public License as published by the Free Software Foundation; + * either version 2 of the License, or any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more + * details. + * + * If you didn't download this code from the following link, you should check if + * you aren't using an obsolete version: + * http://www.lowagie.com/iText/ + */ +package com.lowagie.tools.arguments; + +import java.io.File; + +import javax.swing.filechooser.FileFilter; + +/** + * Filters images in a FileChooser. + */ +public class ImageFilter extends FileFilter { + + /** Array with all kinds of image extensions. */ + public static final String[] IMAGES = new String[8]; + static { + IMAGES[0] = ".jpg"; + IMAGES[1] = ".jpeg"; + IMAGES[2] = ".png"; + IMAGES[3] = ".gif"; + IMAGES[4] = ".bmp"; + IMAGES[5] = ".wmf"; + IMAGES[6] = ".tif"; + IMAGES[7] = ".tiff"; + } + + /** array that enables you to filter on imagetypes. */ + public boolean[] filter = new boolean[8]; + + /** + * Constructs an ImageFilter allowing all images. + */ + public ImageFilter() { + for (int i = 0; i < filter.length; i++) { + filter[i] = true; + } + } + + /** + * Constructs an ImageFilter allowing some images. + * @param jpeg indicates if jpegs are allowed + * @param png indicates if pngs are allowed + * @param gif indicates if gifs are allowed + * @param bmp indicates if bmps are allowed + * @param wmf indicates if wmfs are allowed + * @param tiff indicates if tiffs are allowed + */ + public ImageFilter(boolean jpeg, boolean png, boolean gif, boolean bmp, boolean wmf, boolean tiff) { + if (jpeg) { + filter[0] = true; + filter[1] = true; + } + if (png) { + filter[2] = true; + } + if (gif) { + filter[3] = true; + } + if (bmp) { + filter[4] = true; + } + if (wmf) { + filter[5] = true; + } + if (tiff) { + filter[6] = true; + filter[7] = true; + } + } + + /** + * @see javax.swing.filechooser.FileFilter#accept(java.io.File) + */ + public boolean accept(File f) { + if (f.isDirectory()) return true; + for (int i = 0; i < IMAGES.length; i++) { + if (filter[i] && f.getName().toLowerCase().endsWith(IMAGES[i])) return true; + } + return false; + } + + /** + * @see javax.swing.filechooser.FileFilter#getDescription() + */ + public String getDescription() { + return "Image files"; + } +} \ No newline at end of file diff --git a/src/main/java/com/lowagie/tools/arguments/LabelAccessory.java b/src/main/java/com/lowagie/tools/arguments/LabelAccessory.java new file mode 100644 index 0000000..dff5adc --- /dev/null +++ b/src/main/java/com/lowagie/tools/arguments/LabelAccessory.java @@ -0,0 +1,158 @@ +/* + * $Id: LabelAccessory.java,v 1.4 2005/12/19 07:51:35 blowagie Exp $ + * $Name: $ + * + * Copyright 2005 by Carsten Hammer. + * + * The contents of this file are subject to the Mozilla Public License Version 1.1 + * (the "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the License. + * + * The Original Code is 'iText, a free JAVA-PDF library'. + * + * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by + * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie. + * All Rights Reserved. + * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer + * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved. + * + * Contributor(s): all the names of the contributors are added in the source code + * where applicable. + * + * Alternatively, the contents of this file may be used under the terms of the + * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the + * provisions of LGPL are applicable instead of those above. If you wish to + * allow use of your version of this file only under the terms of the LGPL + * License and not to allow others to use your version of this file under + * the MPL, indicate your decision by deleting the provisions above and + * replace them with the notice and other provisions required by the LGPL. + * If you do not delete the provisions above, a recipient may use your version + * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE. + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the MPL as stated above or under the terms of the GNU + * Library General Public License as published by the Free Software Foundation; + * either version 2 of the License, or any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more + * details. + * + * If you didn't download this code from the following link, you should check if + * you aren't using an obsolete version: + * http://www.lowagie.com/iText/ + */ +package com.lowagie.tools.arguments; + +import java.beans.*; +import java.io.*; + +import java.awt.*; +import javax.swing.*; + +import com.lowagie.text.pdf.*; +import java.util.HashMap; +import java.util.TimeZone; +import java.util.Calendar; +import java.util.Locale; +import java.util.Date; + +/** + * Label for the FileChooser + */ +public class LabelAccessory + extends JPanel implements PropertyChangeListener { + + String filename = ""; + + BorderLayout borderLayout1 = new BorderLayout(); + + JLabel jLabel1 = new JLabel(); + + JPanel jPanel2 = new JPanel(); + + BorderLayout borderLayout2 = new BorderLayout(); + JScrollPane jScrollPane1 = new JScrollPane(); + + public LabelAccessory() { + try { + this.setLayout(borderLayout1); + jLabel1.setHorizontalAlignment(SwingConstants.CENTER); + jPanel2.setLayout(borderLayout2); + this.add(jPanel2, java.awt.BorderLayout.CENTER); + jScrollPane1.setPreferredSize(new Dimension(200, 200)); + jPanel2.add(jScrollPane1, java.awt.BorderLayout.CENTER); + jScrollPane1.setViewportView(jLabel1); + } + catch (Exception ex) { + ex.printStackTrace(); + } + } + + public void createTextFromPDF(File file) { + if (file.exists()) { + int page = 1; + PdfReader reader = null; + + try { + reader = new PdfReader(file.getAbsolutePath()); + HashMap pdfinfo = reader.getInfo(); + + StringBuffer sb = new StringBuffer(); + sb.append("=== Document Information ===

"); + sb.append(reader.getCropBox(page).height() + "*" + + reader.getCropBox(page).width() + "

"); + sb.append("PDF Version: " + reader.getPdfVersion() + "

"); + sb.append("Number of pages: " + reader.getNumberOfPages() + + "

"); + sb.append("Number of PDF objects: " + reader.getXrefSize() + + "

"); + sb.append("File length: " + reader.getFileLength() + "

"); + sb.append("Encrypted= " + reader.isEncrypted() + "

"); + if (pdfinfo.get("Title") != null) { + sb.append("Title= " + pdfinfo.get("Title") + "

"); + } + if (pdfinfo.get("Author") != null) { + sb.append("Author= " + pdfinfo.get("Author") + "

"); + } + if (pdfinfo.get("Subject") != null) { + sb.append("Subject= " + pdfinfo.get("Subject") + "

"); + } + if (pdfinfo.get("Producer") != null) { + sb.append("Producer= " + pdfinfo.get("Producer") + "

"); + } + if (pdfinfo.get("ModDate") != null) { + sb.append("ModDate= " + + PdfDate.decode(pdfinfo.get("ModDate").toString()).getTime() + + "

"); + } + if (pdfinfo.get("CreationDate") != null) { + sb.append("CreationDate= " + + PdfDate.decode(pdfinfo.get("CreationDate").toString()).getTime() + + "

"); + } + sb.append(""); + jLabel1.setText(sb.toString()); + } + catch (IOException ex) { + jLabel1.setText(""); + } + } + } + + public void propertyChange(PropertyChangeEvent evt) { + filename = evt.getPropertyName(); + if (filename.equals(JFileChooser.SELECTED_FILE_CHANGED_PROPERTY)) { + File file = (File) evt.getNewValue(); + if (file != null) { + this.createTextFromPDF(file); + this.repaint(); + } + } + } +} diff --git a/src/main/java/com/lowagie/tools/arguments/OptionArgument.java b/src/main/java/com/lowagie/tools/arguments/OptionArgument.java new file mode 100644 index 0000000..28d4eda --- /dev/null +++ b/src/main/java/com/lowagie/tools/arguments/OptionArgument.java @@ -0,0 +1,210 @@ +/* + * $Id: OptionArgument.java,v 1.8 2006/05/30 09:13:00 blowagie Exp $ + * $Name: $ + * + * Copyright 2005 by Bruno Lowagie. + * + * The contents of this file are subject to the Mozilla Public License Version 1.1 + * (the "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the License. + * + * The Original Code is 'iText, a free JAVA-PDF library'. + * + * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by + * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie. + * All Rights Reserved. + * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer + * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved. + * + * Contributor(s): all the names of the contributors are added in the source code + * where applicable. + * + * Alternatively, the contents of this file may be used under the terms of the + * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the + * provisions of LGPL are applicable instead of those above. If you wish to + * allow use of your version of this file only under the terms of the LGPL + * License and not to allow others to use your version of this file under + * the MPL, indicate your decision by deleting the provisions above and + * replace them with the notice and other provisions required by the LGPL. + * If you do not delete the provisions above, a recipient may use your version + * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE. + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the MPL as stated above or under the terms of the GNU + * Library General Public License as published by the Free Software Foundation; + * either version 2 of the License, or any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more + * details. + * + * If you didn't download this code from the following link, you should check if + * you aren't using an obsolete version: + * http://www.lowagie.com/iText/ + */ +package com.lowagie.tools.arguments; + +import java.awt.event.ActionEvent; +import java.util.Iterator; +import java.util.TreeMap; + +import javax.swing.JComboBox; +import javax.swing.JOptionPane; + +import com.lowagie.tools.plugins.AbstractTool; + +/** + * Argument that can be one of several options. + */ +public class OptionArgument extends ToolArgument { + + /** + * An Entry that can be chosen as option. + */ + public class Entry { + /** Describes the option. */ + private Object description; + /** Holds the actual value of the option. */ + private Object value; + /** + * Constructs an entry. + * @param value the value of the entry (that wil be identical to the description) + */ + public Entry(Object value) { + this.value = value; + this.description = value; + } + /** + * Constructs an entry. + * @param description the description of the entry + * @param value the value of the entry + */ + public Entry(Object description, Object value) { + this.description = description; + this.value = value; + } + /** + * String representation of the Entry. + * @return a description of the entry + */ + public String toString() { + return description.toString(); + } + /** + * Gets the value of the String. + * @return the toString of the value + */ + public String getValueToString() { + return value.toString(); + } + /** + * @return Returns the description. + */ + public Object getDescription() { + return description; + } + /** + * @param description The description to set. + */ + public void setDescription(Object description) { + this.description = description; + } + /** + * @return Returns the value. + */ + public Object getValue() { + return value; + } + /** + * @param value The value to set. + */ + public void setValue(Object value) { + this.value = value; + } + } + + private TreeMap options = new TreeMap(); + + /** + * Constructs an OptionArgument. + * @param tool the tool that needs this argument + * @param name the name of the argument + * @param description the description of the argument + */ + public OptionArgument(AbstractTool tool, String name, String description) { + super(tool, name, description, Entry.class.getName()); + } + + /** + * Adds an Option. + * @param description the description of the option + * @param value the value of the option + */ + public void addOption(Object description, Object value) { + options.put(value.toString(), new Entry(description, value)); + } + + /** + * Gets the argument as an object. + * @return an object + * @throws InstantiationException + */ + public Object getArgument() throws InstantiationException { + if (value == null) return null; + try { + return ((Entry)options.get(value)).getValue(); + } catch (Exception e) { + throw new InstantiationException(e.getMessage()); + } + } + + /** + * @see com.lowagie.tools.arguments.ToolArgument#getUsage() + */ + public String getUsage() { + StringBuffer buf = new StringBuffer(super.getUsage()); + buf.append(" possible options:\n"); + Entry entry; + for (Iterator i = options.values().iterator(); i.hasNext(); ) { + entry = (Entry)i.next(); + buf.append(" - "); + buf.append(entry.getValueToString()); + buf.append(": "); + buf.append(entry.toString()); + buf.append("\n"); + } + return buf.toString(); + } + + /** + * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent) + */ + public void actionPerformed(ActionEvent evt) { + Object[] message = new Object[2]; + message[0] = "Choose one of the following options:"; + JComboBox cb = new JComboBox(); + for(Iterator i = options.values().iterator(); i.hasNext(); ) { + cb.addItem(i.next()); + } + message[1] = cb; + int result = JOptionPane.showOptionDialog( + tool.getInternalFrame(), + message, + description, + JOptionPane.OK_CANCEL_OPTION, + JOptionPane.QUESTION_MESSAGE, + null, + null, + null + ); + if (result == 0) { + Entry entry = (Entry)cb.getSelectedItem(); + setValue(entry.getValueToString()); + } + } +} diff --git a/src/main/java/com/lowagie/tools/arguments/PageSelectionTableDialog.java b/src/main/java/com/lowagie/tools/arguments/PageSelectionTableDialog.java new file mode 100644 index 0000000..cf14442 --- /dev/null +++ b/src/main/java/com/lowagie/tools/arguments/PageSelectionTableDialog.java @@ -0,0 +1,243 @@ +package com.lowagie.tools.arguments; + +import java.awt.*; +import javax.swing.*; +import javax.swing.table.*; +import java.awt.BorderLayout; +import javax.swing.event.ListSelectionEvent; +import javax.swing.event.ListSelectionListener; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +/** + *

Title:

+ * + *

Description:

+ * + *

Copyright: Copyright (c) 2005

+ * + *

Company:

+ * + * @author not attributable + * @version 1.0 + */ +public class PageSelectionTableDialog + extends JDialog { + JPanel panel1 = new JPanel(); + BorderLayout borderLayout1 = new BorderLayout(); + ListSelectionModel listSelectionModel1; + JTable jTable1 = new JTable(); + JScrollPane jScrollPane1 = new JScrollPane(); + String selectionstring = ""; + JLabel jLabel1 = new JLabel(); + BorderLayout borderLayout2 = new BorderLayout(); + JPanel jPanel1 = new JPanel(); + JButton alljButton1 = new JButton(); + JButton oddjButton2 = new JButton(); + JButton evenjButton3 = new JButton(); + JToggleButton jToggleButton1 = new JToggleButton(); + JButton none = new JButton(); + + public PageSelectionTableDialog(JInternalFrame owner, String title, boolean modal) { + super(new Frame(), title, modal); +// super( title); + try { + setDefaultCloseOperation(DISPOSE_ON_CLOSE); + jbInit(); + pack(); + } + catch (Exception exception) { + exception.printStackTrace(); + } + } + + public PageSelectionTableDialog(JInternalFrame jinternalframe) { + this(jinternalframe, "", false); + } + + private void jbInit() throws Exception { + panel1.setLayout(borderLayout1); + this.getContentPane().setLayout(borderLayout2); + alljButton1.setText("all"); + alljButton1.addActionListener(new + PageSelectionTableDialog_jButton1_actionAdapter(this)); + oddjButton2.setText("odd"); + oddjButton2.addActionListener(new + PageSelectionTableDialog_jButton2_actionAdapter(this)); + evenjButton3.setText("even"); + evenjButton3.addActionListener(new + PageSelectionTableDialog_jButton3_actionAdapter(this)); + jToggleButton1.setText("swap"); + jToggleButton1.addActionListener(new + PageSelectionTableDialog_jToggleButton1_actionAdapter(this)); + none.setText("none"); + none.addActionListener(new PageSelectionTableDialog_none_actionAdapter(this)); + + panel1.add(jScrollPane1, java.awt.BorderLayout.CENTER); + panel1.add(jLabel1, java.awt.BorderLayout.SOUTH); + this.getContentPane().add(jPanel1, java.awt.BorderLayout.SOUTH); + jPanel1.add(none); + jPanel1.add(jToggleButton1); + jPanel1.add(evenjButton3); + jPanel1.add(oddjButton2); + jPanel1.add(alljButton1); + this.getContentPane().add(panel1, java.awt.BorderLayout.CENTER); + jScrollPane1.setViewportView(jTable1); + listSelectionModel1 = jTable1.getSelectionModel(); + listSelectionModel1.addListSelectionListener(new + PageSelectionTableDialog_listSelectionModel1_listSelectionAdapter(this)); + } + + public void setDataModel(TableModel dataModel) { + TableSorter sorter = new TableSorter(dataModel); + jTable1.setModel(sorter); + sorter.addMouseListenerToHeaderInTable(jTable1); + this.repaint(); + } + + public void listSelectionModel1_valueChanged(ListSelectionEvent e) { + if (!e.getValueIsAdjusting()) { + pulllistselectionmodel(); + } + } + + private void pulllistselectionmodel() { + TableSorter mysorter = (TableSorter) jTable1.getModel(); + int[] values = jTable1.getSelectedRows(); + int max = jTable1.getSelectedRowCount(); + int[] swappedvalues = new int[max]; + + if (jToggleButton1.getModel().isSelected()) { + for (int i = 0; i < max; i+=2) { + int second=(i+1)columnIndex and rowIndex. + * + * @param rowIndex the row whose value is to be queried + * @param columnIndex the column whose value is to be queried + * @return the value Object at the specified cell + * @todo Implement this javax.swing.table.TableModel method + */ + public Object getValueAt(int rowIndex, int columnIndex) { + Rectangle rec = reader.getPageSizeWithRotation(rowIndex + 1); + switch (columnIndex) { + case 0: + return myFormatter.format(rowIndex + 1); + case 1: + return new Float(rec.width()); + case 2: + return new Float(rec.height()); + case 3: + return new Float(rec.getRotation()); + } + return null; + } + + public String getColumnName(int column) { + String name = new Integer(column + 1).toString(); + switch (column) { + case 0: + name = "Pagenr

" + name + ""; + break; + case 1: + name = "Weidth

" + name + ""; + break; + case 2: + name = "Height

" + name + ""; + break; + case 3: + name = "Rotation

" + name + ""; + break; + + default: + name = "-

" + name + ""; + break; + } + return name; + } + +} diff --git a/src/main/java/com/lowagie/tools/arguments/PdfFilter.java b/src/main/java/com/lowagie/tools/arguments/PdfFilter.java new file mode 100644 index 0000000..5b0b8b4 --- /dev/null +++ b/src/main/java/com/lowagie/tools/arguments/PdfFilter.java @@ -0,0 +1,77 @@ +/* + * $Id: PdfFilter.java,v 1.2 2005/10/24 06:33:31 blowagie Exp $ + * $Name: $ + * + * Copyright 2005 by Bruno Lowagie. + * + * The contents of this file are subject to the Mozilla Public License Version 1.1 + * (the "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the License. + * + * The Original Code is 'iText, a free JAVA-PDF library'. + * + * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by + * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie. + * All Rights Reserved. + * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer + * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved. + * + * Contributor(s): all the names of the contributors are added in the source code + * where applicable. + * + * Alternatively, the contents of this file may be used under the terms of the + * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the + * provisions of LGPL are applicable instead of those above. If you wish to + * allow use of your version of this file only under the terms of the LGPL + * License and not to allow others to use your version of this file under + * the MPL, indicate your decision by deleting the provisions above and + * replace them with the notice and other provisions required by the LGPL. + * If you do not delete the provisions above, a recipient may use your version + * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE. + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the MPL as stated above or under the terms of the GNU + * Library General Public License as published by the Free Software Foundation; + * either version 2 of the License, or any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more + * details. + * + * If you didn't download this code from the following link, you should check if + * you aren't using an obsolete version: + * http://www.lowagie.com/iText/ + */ +package com.lowagie.tools.arguments; + +import java.io.File; + +import javax.swing.filechooser.FileFilter; + +/** + * Filters PDF files in a JFileChooser. + */ +public class PdfFilter extends FileFilter { + + /** + * @see javax.swing.filechooser.FileFilter#accept(java.io.File) + */ + public boolean accept(File f) { + if (f.isDirectory()) return true; + if (f.getName().toLowerCase().endsWith(".pdf")) return true; + return false; + } + + /** + * @see javax.swing.filechooser.FileFilter#getDescription() + */ + public String getDescription() { + return "*.pdf PDF files"; + } + +} diff --git a/src/main/java/com/lowagie/tools/arguments/TableMap.java b/src/main/java/com/lowagie/tools/arguments/TableMap.java new file mode 100644 index 0000000..58308ee --- /dev/null +++ b/src/main/java/com/lowagie/tools/arguments/TableMap.java @@ -0,0 +1,89 @@ +package com.lowagie.tools.arguments; +/* + * @(#)TableMap.java 1.4 97/12/17 + * + * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved. + * + * This software is the confidential and proprietary information of Sun + * Microsystems, Inc. ("Confidential Information"). You shall not + * disclose such Confidential Information and shall use it only in + * accordance with the terms of the license agreement you entered into + * with Sun. + * + * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE + * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE + * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR + * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES + * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING + * THIS SOFTWARE OR ITS DERIVATIVES. + * + */ + +/** + * In a chain of data manipulators some behaviour is common. TableMap + * provides most of this behavour and can be subclassed by filters + * that only need to override a handful of specific methods. TableMap + * implements TableModel by routing all requests to its model, and + * TableModelListener by routing all events to its listeners. Inserting + * a TableMap which has not been subclassed into a chain of table filters + * should have no effect. + * + * @version 1.4 12/17/97 + * @author Philip Milne */ + +import javax.swing.table.*; +import javax.swing.event.TableModelListener; +import javax.swing.event.TableModelEvent; + +public class TableMap extends AbstractTableModel implements TableModelListener +{ + protected TableModel model; + + public TableModel getModel() { + return model; + } + + public void setModel(TableModel model) { + this.model = model; + model.addTableModelListener(this); + } + + // By default, Implement TableModel by forwarding all messages + // to the model. + + public Object getValueAt(int aRow, int aColumn) { + return model.getValueAt(aRow, aColumn); + } + + public void setValueAt(Object aValue, int aRow, int aColumn) { + model.setValueAt(aValue, aRow, aColumn); + } + + public int getRowCount() { + return (model == null) ? 0 : model.getRowCount(); + } + + public int getColumnCount() { + return (model == null) ? 0 : model.getColumnCount(); + } + + public String getColumnName(int aColumn) { + return model.getColumnName(aColumn); + } + + public Class getColumnClass(int aColumn) { + return model.getColumnClass(aColumn); + } + + public boolean isCellEditable(int row, int column) { + return model.isCellEditable(row, column); + } +// +// Implementation of the TableModelListener interface, +// + + // By default forward all events to all the listeners. + public void tableChanged(TableModelEvent e) { + fireTableChanged(e); + } +} diff --git a/src/main/java/com/lowagie/tools/arguments/TableSorter.java b/src/main/java/com/lowagie/tools/arguments/TableSorter.java new file mode 100644 index 0000000..a921516 --- /dev/null +++ b/src/main/java/com/lowagie/tools/arguments/TableSorter.java @@ -0,0 +1,367 @@ +/** Code contributed by Anonymous; looks like code that was reused from another application. */ +package com.lowagie.tools.arguments; + +import java.awt.event.InputEvent; + +// Imports for picking up mouse events from the JTable. +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; + +/* + * @(#)TableSorter.java 1.5 97/12/17 + * + * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved. + * + * This software is the confidential and proprietary information of Sun + * Microsystems, Inc. ("Confidential Information"). You shall not + * disclose such Confidential Information and shall use it only in + * accordance with the terms of the license agreement you entered into + * with Sun. + * + * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE + * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE + * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR + * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES + * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING + * THIS SOFTWARE OR ITS DERIVATIVES. + * + */ + +/** + * A sorter for TableModels. The sorter has a model (conforming to TableModel) + * and itself implements TableModel. TableSorter does not store or copy + * the data in the TableModel, instead it maintains an array of + * integers which it keeps the same size as the number of rows in its + * model. When the model changes it notifies the sorter that something + * has changed eg. "rowsAdded" so that its internal array of integers + * can be reallocated. As requests are made of the sorter (like + * getValueAt(row, col) it redirects them to its model via the mapping + * array. That way the TableSorter appears to hold another copy of the table + * with the rows in a different order. The sorting algorthm used is stable + * which means that it does not move around rows when its comparison + * function returns 0 to denote that they are equivalent. + * + * @version 1.5 12/17/97 + * @author Philip Milne + */ +import java.util.*; + +import javax.swing.JTable; +import javax.swing.event.TableModelEvent; +import javax.swing.table.JTableHeader; +import javax.swing.table.TableColumnModel; +import javax.swing.table.TableModel; + +public class TableSorter + extends TableMap { + int[] indexes; + Vector sortingColumns = new Vector(); + boolean ascending = true; + int compares; + + public TableSorter() { + indexes = new int[0]; // For consistency. + } + + public TableSorter(TableModel model) { + setModel(model); + } + + public void setModel(TableModel model) { + super.setModel(model); + reallocateIndexes(); + } + + public int compareRowsByColumn(int row1, int row2, int column) { + Class type = model.getColumnClass(column); + TableModel data = model; + + // Check for nulls + Object o1 = data.getValueAt(row1, column); + Object o2 = data.getValueAt(row2, column); + + // If both values are null return 0 + if ( (o1 == null) && (o2 == null)) { + return 0; + } + else if (o1 == null) { // Define null less than everything. + + return -1; + } + else if (o2 == null) { + return 1; + } + + /* We copy all returned values from the getValue call in case + an optimised model is reusing one object to return many values. + The Number subclasses in the JDK are immutable and so will not be used in + this way but other subclasses of Number might want to do this to save + space and avoid unnecessary heap allocation. + */ + if (type.getSuperclass() == java.lang.Number.class) { + Number n1 = (Number) data.getValueAt(row1, column); + double d1 = n1.doubleValue(); + Number n2 = (Number) data.getValueAt(row2, column); + double d2 = n2.doubleValue(); + + if (d1 < d2) { + return -1; + } + else if (d1 > d2) { + return 1; + } + else { + return 0; + } + } + else if (type == java.util.Date.class) { + Date d1 = (Date) data.getValueAt(row1, column); + long n1 = d1.getTime(); + Date d2 = (Date) data.getValueAt(row2, column); + long n2 = d2.getTime(); + + if (n1 < n2) { + return -1; + } + else if (n1 > n2) { + return 1; + } + else { + return 0; + } + } + else if (type == String.class) { + String s1 = (String) data.getValueAt(row1, column); + String s2 = (String) data.getValueAt(row2, column); + int result = s1.compareTo(s2); + + if (result < 0) { + return -1; + } + else if (result > 0) { + return 1; + } + else { + return 0; + } + } + else if (type == Boolean.class) { + Boolean bool1 = (Boolean) data.getValueAt(row1, column); + boolean b1 = bool1.booleanValue(); + Boolean bool2 = (Boolean) data.getValueAt(row2, column); + boolean b2 = bool2.booleanValue(); + + if (b1 == b2) { + return 0; + } + else if (b1) { // Define false < true + + return 1; + } + else { + return -1; + } + } + else { + Object v1 = data.getValueAt(row1, column); + String s1 = v1.toString(); + Object v2 = data.getValueAt(row2, column); + String s2 = v2.toString(); + int result = s1.compareTo(s2); + + if (result < 0) { + return -1; + } + else if (result > 0) { + return 1; + } + else { + return 0; + } + } + } + + public int compare(int row1, int row2) { + compares++; + + for (int level = 0; level < sortingColumns.size(); level++) { + Integer column = (Integer) sortingColumns.elementAt(level); + int result = compareRowsByColumn(row1, row2, column.intValue()); + + if (result != 0) { + return ascending ? result : ( -result); + } + } + + return 0; + } + + public void reallocateIndexes() { + int rowCount = model.getRowCount(); + + // Set up a new array of indexes with the right number of elements + // for the new data model. + indexes = new int[rowCount]; + + // Initialise with the identity mapping. + for (int row = 0; row < rowCount; row++) { + indexes[row] = row; + } + } + + public void tableChanged(TableModelEvent e) { + //System.out.println("Sorter: tableChanged"); + reallocateIndexes(); + super.tableChanged(e); + } + + public void checkModel() { + if (indexes.length != model.getRowCount()) { + System.err.println("Sorter not informed of a change in model."); + } + } + + public void sort(Object sender) { + checkModel(); + compares = 0; + + // n2sort(); + // qsort(0, indexes.length-1); + shuttlesort( (int[]) indexes.clone(), indexes, 0, indexes.length); + + //System.out.println("Compares: "+compares); + } + + public void n2sort() { + for (int i = 0; i < getRowCount(); i++) { + for (int j = i + 1; j < getRowCount(); j++) { + if (compare(indexes[i], indexes[j]) == -1) { + swap(i, j); + } + } + } + } + + // This is a home-grown implementation which we have not had time + // to research - it may perform poorly in some circumstances. It + // requires twice the space of an in-place algorithm and makes + // NlogN assigments shuttling the values between the two + // arrays. The number of compares appears to vary between N-1 and + // NlogN depending on the initial order but the main reason for + // using it here is that, unlike qsort, it is stable. + public void shuttlesort(int[] from, int[] to, int low, int high) { + if ( (high - low) < 2) { + return; + } + + int middle = (low + high) / 2; + shuttlesort(to, from, low, middle); + shuttlesort(to, from, middle, high); + + int p = low; + int q = middle; + + /* This is an optional short-cut; at each recursive call, + check to see if the elements in this subset are already + ordered. If so, no further comparisons are needed; the + sub-array can just be copied. The array must be copied rather + than assigned otherwise sister calls in the recursion might + get out of sinc. When the number of elements is three they + are partitioned so that the first set, [low, mid), has one + element and and the second, [mid, high), has two. We skip the + optimisation when the number of elements is three or less as + the first compare in the normal merge will produce the same + sequence of steps. This optimisation seems to be worthwhile + for partially ordered lists but some analysis is needed to + find out how the performance drops to Nlog(N) as the initial + order diminishes - it may drop very quickly. */ + if ( ( (high - low) >= 4) && (compare(from[middle - 1], from[middle]) <= 0)) { + for (int i = low; i < high; i++) { + to[i] = from[i]; + } + + return; + } + + // A normal merge. + for (int i = low; i < high; i++) { + if ( (q >= high) || ( (p < middle) && (compare(from[p], from[q]) <= 0))) { + to[i] = from[p++]; + } + else { + to[i] = from[q++]; + } + } + } + + public void swap(int i, int j) { + int tmp = indexes[i]; + indexes[i] = indexes[j]; + indexes[j] = tmp; + } + + // The mapping only affects the contents of the data rows. + // Pass all requests to these rows through the mapping array: "indexes". + public Object getValueAt(int aRow, int aColumn) { + checkModel(); + + return model.getValueAt(indexes[aRow], aColumn); + } + + public void setValueAt(Object aValue, int aRow, int aColumn) { + checkModel(); + model.setValueAt(aValue, indexes[aRow], aColumn); + } + + public void sortByColumn(int column) { + sortByColumn(column, true); + } + + public void sortByColumn(int column, boolean ascending) { + this.ascending = ascending; + sortingColumns.removeAllElements(); + sortingColumns.addElement(new Integer(column)); + sort(this); + super.tableChanged(new TableModelEvent(this)); + } + + // There is no-where else to put this. + // Add a mouse listener to the Table to trigger a table sort + // when a column heading is clicked in the JTable. + public void addMouseListenerToHeaderInTable(JTable table) { + final TableSorter sorter = this; + final JTable tableView = table; + tableView.setColumnSelectionAllowed(false); + + MouseAdapter listMouseListener = new MouseAdapter() { + public void mouseClicked(MouseEvent e) { + TableColumnModel columnModel = tableView.getColumnModel(); + int viewColumn = columnModel.getColumnIndexAtX(e.getX()); + int column = tableView.convertColumnIndexToModel(viewColumn); + + if ( (e.getClickCount() == 1) && (column != -1)) { + //System.out.println("Sorting ..."); + int shiftPressed = e.getModifiers() & InputEvent.SHIFT_MASK; + boolean ascending = (shiftPressed == 0); + sorter.sortByColumn(column, ascending); + } + } + }; + + JTableHeader th = tableView.getTableHeader(); + th.addMouseListener(listMouseListener); + } + + public int getModelrow(int viewrow) { + return indexes[viewrow]; + } + + public int getjTablerow(int modelrow) { + int i = 0; + while (indexes[i] != modelrow) { + i++; + } + return i; + } + +} diff --git a/src/main/java/com/lowagie/tools/arguments/ToolArgument.java b/src/main/java/com/lowagie/tools/arguments/ToolArgument.java new file mode 100644 index 0000000..203c298 --- /dev/null +++ b/src/main/java/com/lowagie/tools/arguments/ToolArgument.java @@ -0,0 +1,281 @@ +/* + * $Id: ToolArgument.java,v 1.6 2006/05/30 09:13:00 blowagie Exp $ + * $Name: $ + * + * Copyright 2005 by Bruno Lowagie. + * + * The contents of this file are subject to the Mozilla Public License Version 1.1 + * (the "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the License. + * + * The Original Code is 'iText, a free JAVA-PDF library'. + * + * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by + * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie. + * All Rights Reserved. + * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer + * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved. + * + * Contributor(s): all the names of the contributors are added in the source code + * where applicable. + * + * Alternatively, the contents of this file may be used under the terms of the + * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the + * provisions of LGPL are applicable instead of those above. If you wish to + * allow use of your version of this file only under the terms of the LGPL + * License and not to allow others to use your version of this file under + * the MPL, indicate your decision by deleting the provisions above and + * replace them with the notice and other provisions required by the LGPL. + * If you do not delete the provisions above, a recipient may use your version + * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE. + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the MPL as stated above or under the terms of the GNU + * Library General Public License as published by the Free Software Foundation; + * either version 2 of the License, or any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more + * details. + * + * If you didn't download this code from the following link, you should check if + * you aren't using an obsolete version: + * http://www.lowagie.com/iText/ + */ +package com.lowagie.tools.arguments; + +import java.awt.Color; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.beans.PropertyChangeEvent; +import java.beans.PropertyChangeListener; +import java.io.File; +import java.util.Vector; + +import javax.swing.JColorChooser; +import javax.swing.JFileChooser; +import javax.swing.JOptionPane; + +import com.lowagie.text.Image; +import com.lowagie.tools.plugins.AbstractTool; + +/** + * This is an argument of one of the tools in the toolbox. + */ +public class ToolArgument + implements ActionListener, PropertyChangeListener { + /** reference to the internal frame */ + protected AbstractTool tool; + /** describes the argument. */ + protected String description; + /** short name for the argument. */ + protected String name; + /** type of the argument. */ + protected String classname; + /** value of the argument. */ + protected String value = null; + + /** Constructs a ToolArgument. */ + public ToolArgument() {} + + /** + * Constructs a ToolArgument. + * @param tool the tool that needs this argument + * @param name the name of the argument + * @param description the description of the argument + * @param classname the type of the argument + */ + public ToolArgument(AbstractTool tool, String name, String description, + String classname) { + this.tool = tool; + this.name = name; + this.description = description; + this.classname = classname; + } + + /** + * Gets the argument as an object. + * @return an object + * @throws InstantiationException + */ + public Object getArgument() throws InstantiationException { + if (value == null) { + return null; + } + try { + if (String.class.getName().equals(classname)) { + return value; + } + if (Image.class.getName().equals(classname)) { + return Image.getInstance(value); + } + if (File.class.getName().equals(classname)) { + return new File(value); + } + if (Color.class.getName().equals(classname)) { + return Color.decode(value); + } + } + catch (Exception e) { + throw new InstantiationException(e.getMessage()); + } + return value; + } + + /** + * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent) + */ + public void actionPerformed(ActionEvent e) { + if (String.class.getName().equals(classname)) { + setValue(JOptionPane.showInputDialog(tool.getInternalFrame(), + "Enter a value for " + name + ":")); + } + if (Image.class.getName().equals(classname)) { + JFileChooser fc = new JFileChooser(); + fc.showOpenDialog(tool.getInternalFrame()); + setValue(fc.getSelectedFile().getAbsolutePath()); + } + if (File.class.getName().equals(classname)) { + JFileChooser fc = new JFileChooser(); + fc.showOpenDialog(tool.getInternalFrame()); + setValue(fc.getSelectedFile().getAbsolutePath()); + } + if (Color.class.getName().equals(classname)) { + Color initialColor = new Color(0xFF, 0xFF, 0xFF); + if (value != null) { + initialColor = Color.decode(value); + } + Color newColor = JColorChooser.showDialog(tool.getInternalFrame(), + "Choose Color", initialColor); + setValue("0x" + + Integer.toHexString( (newColor.getRed() << 16) | + (newColor.getGreen() << 8) | + (newColor.getBlue() << 0)).toUpperCase()); + } + } + + /** + * Give you a String that can be used in a usage description. + * @return a String + */ + public String getUsage() { + StringBuffer buf = new StringBuffer(" "); + buf.append(name); + buf.append(" - "); + buf.append(description); + buf.append("\n"); + return buf.toString(); + } + + /** + * @return Returns the classname. + */ + public String getClassname() { + return classname; + } + + /** + * @param classname The classname to set. + */ + public void setClassname(String classname) { + this.classname = classname; + } + + /** + * @return Returns the description. + */ + public String getDescription() { + return description; + } + + /** + * @param description The description to set. + */ + public void setDescription(String description) { + this.description = description; + } + + /** + * @return Returns the name. + */ + public String getName() { + return name; + } + + /** + * @param name The name to set. + */ + public void setName(String name) { + this.name = name; + } + + /** + * @return Returns the value. + */ + public String getValue() { + return value; + } + + /** + * @param value The value to set. + */ + public void setValue(String value) { + Object oldvalue = this.value; + this.value = value; + tool.valueHasChanged(this); + this.firePropertyChange(new PropertyChangeEvent(this, name, + oldvalue, this.value)); + } + public void setValue(String value, String propertyname) { + Object oldvalue = this.value; + this.value = value; + tool.valueHasChanged(this); + this.firePropertyChange(new PropertyChangeEvent(this, propertyname, + oldvalue, this.value)); + } + transient Vector propertyChangeListeners; + public synchronized void addPropertyChangeListener(PropertyChangeListener l) { + + Vector v = propertyChangeListeners == null ? new Vector(2) : + (Vector) propertyChangeListeners.clone(); + if (!v.contains(l)) { + v.addElement(l); + propertyChangeListeners = v; + } + } + + public synchronized void removePropertyChangeListener(PropertyChangeListener + l) { + if (propertyChangeListeners != null && propertyChangeListeners.contains(l)) { + Vector v = (Vector) propertyChangeListeners.clone(); + v.removeElement(l); + propertyChangeListeners = v; + } + + } + + protected void firePropertyChange(PropertyChangeEvent evt) { + if (propertyChangeListeners != null) { + Vector listeners = propertyChangeListeners; + int count = listeners.size(); + for (int i = 0; i < count; i++) { + ( (PropertyChangeListener) listeners.elementAt(i)).propertyChange(evt); + } + } + } + + /** + * This method gets called when a bound property is changed. + * + * @param evt A PropertyChangeEvent object describing the event source and the property that has + * changed. + * @todo Implement this java.beans.PropertyChangeListener method + */ + public void propertyChange(PropertyChangeEvent evt) { + } +} diff --git a/src/main/java/com/lowagie/tools/concat_pdf.java b/src/main/java/com/lowagie/tools/concat_pdf.java new file mode 100644 index 0000000..e9929a0 --- /dev/null +++ b/src/main/java/com/lowagie/tools/concat_pdf.java @@ -0,0 +1,96 @@ +/* + * $Id: concat_pdf.java,v 1.19 2006/04/28 16:26:38 psoares33 Exp $ + * $Name: $ + * + * This code is free software. It may only be copied or modified + * if you include the following copyright notice: + * + * This class by Mark Thompson. Copyright (c) 2002 Mark Thompson. + * + * This code is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * + * itext@lowagie.com + */ + +/** + * This class demonstrates copying a PDF file using iText. + * @author Mark Thompson + */ +package com.lowagie.tools; + +import java.io.*; + +import com.lowagie.text.Document; +import com.lowagie.text.pdf.*; +import java.util.List; +import java.util.ArrayList; + +/** + * Tool that can be used to concatenate existing PDF files. + */ +public class concat_pdf { + + /** + * This class can be used to concatenate existing PDF files. + * (This was an example known as PdfCopy.java) + * @param args the command line arguments + */ + public static void main(String args[]) { + if (args.length < 2) { + System.err.println("arguments: file1 [file2 ...] destfile"); + } + else { + try { + int pageOffset = 0; + ArrayList master = new ArrayList(); + int f = 0; + String outFile = args[args.length-1]; + Document document = null; + PdfCopy writer = null; + while (f < args.length-1) { + // we create a reader for a certain document + PdfReader reader = new PdfReader(args[f]); + reader.consolidateNamedDestinations(); + // we retrieve the total number of pages + int n = reader.getNumberOfPages(); + List bookmarks = SimpleBookmark.getBookmark(reader); + if (bookmarks != null) { + if (pageOffset != 0) + SimpleBookmark.shiftPageNumbers(bookmarks, pageOffset, null); + master.addAll(bookmarks); + } + pageOffset += n; + System.out.println("There are " + n + " pages in " + args[f]); + + if (f == 0) { + // step 1: creation of a document-object + document = new Document(reader.getPageSizeWithRotation(1)); + // step 2: we create a writer that listens to the document + writer = new PdfCopy(document, new FileOutputStream(outFile)); + // step 3: we open the document + document.open(); + } + // step 4: we add content + PdfImportedPage page; + for (int i = 0; i < n; ) { + ++i; + page = writer.getImportedPage(reader, i); + writer.addPage(page); + System.out.println("Processed page " + i); + } + writer.freeReader(reader); + f++; + } + if (master.size() > 0) + writer.setOutlines(master); + // step 5: we close the document + document.close(); + } + catch(Exception e) { + e.printStackTrace(); + } + } + } +} diff --git a/src/main/java/com/lowagie/tools/encrypt_pdf.java b/src/main/java/com/lowagie/tools/encrypt_pdf.java new file mode 100644 index 0000000..e6a0215 --- /dev/null +++ b/src/main/java/com/lowagie/tools/encrypt_pdf.java @@ -0,0 +1,99 @@ +/* + * $Id: encrypt_pdf.java,v 1.18 2004/12/14 10:59:06 blowagie Exp $ + * $Name: $ + * + * This code is free software. It may only be copied or modified + * if you include the following copyright notice: + * + * This class by Paulo Soares. Copyright (c) 2002 Paulo Soares. + * + * This code is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * + * itext@lowagie.com + */ + +/** + * This class demonstrates how to encrypt a pdf file + * @author Paulo Soares + */ +package com.lowagie.tools; + +import com.lowagie.text.pdf.PdfEncryptor; +import com.lowagie.text.pdf.PdfReader; +import com.lowagie.text.pdf.PdfWriter; + +import java.io.FileOutputStream; +import java.util.HashMap; + +/** + * Encrypts a PDF document. It needs iText (http://www.lowagie.com/iText). + * @author Paulo Soares (psoares@consiste.pt) + */ +public class encrypt_pdf { + + private final static int INPUT_FILE = 0; + private final static int OUTPUT_FILE = 1; + private final static int USER_PASSWORD = 2; + private final static int OWNER_PASSWORD = 3; + private final static int PERMISSIONS = 4; + private final static int STRENGTH = 5; + private final static int MOREINFO = 6; + private final static int permit[] = { + PdfWriter.AllowPrinting, + PdfWriter.AllowModifyContents, + PdfWriter.AllowCopy, + PdfWriter.AllowModifyAnnotations, + PdfWriter.AllowFillIn, + PdfWriter.AllowScreenReaders, + PdfWriter.AllowAssembly, + PdfWriter.AllowDegradedPrinting}; + + private static void usage() { + System.out.println("usage: input_file output_file user_password owner_password permissions 128|40 [new info string pairs]"); + System.out.println("permissions is 8 digit long 0 or 1. Each digit has a particular security function:"); + System.out.println(); + System.out.println("AllowPrinting"); + System.out.println("AllowModifyContents"); + System.out.println("AllowCopy"); + System.out.println("AllowModifyAnnotations"); + System.out.println("AllowFillIn (128 bit only)"); + System.out.println("AllowScreenReaders (128 bit only)"); + System.out.println("AllowAssembly (128 bit only)"); + System.out.println("AllowDegradedPrinting (128 bit only)"); + System.out.println("Example permissions to copy and print would be: 10100000"); + } + + /** + * Encrypts a PDF document. + * + * @param args input_file output_file user_password owner_password permissions 128|40 [new info string pairs] + */ + public static void main (String args[]) { + System.out.println("PDF document encryptor"); + if (args.length <= STRENGTH || args[PERMISSIONS].length() != 8) { + usage(); + return; + } + try { + int permissions = 0; + String p = args[PERMISSIONS]; + for (int k = 0; k < p.length(); ++k) { + permissions |= (p.charAt(k) == '0' ? 0 : permit[k]); + } + System.out.println("Reading " + args[INPUT_FILE]); + PdfReader reader = new PdfReader(args[INPUT_FILE]); + System.out.println("Writing " + args[OUTPUT_FILE]); + HashMap moreInfo = new HashMap(); + for (int k = MOREINFO; k < args.length - 1; k += 2) + moreInfo.put(args[k], args[k + 1]); + PdfEncryptor.encrypt(reader, new FileOutputStream(args[OUTPUT_FILE]), + args[USER_PASSWORD].getBytes(), args[OWNER_PASSWORD].getBytes(), permissions, args[STRENGTH].equals("128"), moreInfo); + System.out.println("Done."); + } + catch (Exception e) { + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/src/main/java/com/lowagie/tools/handout_pdf.java b/src/main/java/com/lowagie/tools/handout_pdf.java new file mode 100644 index 0000000..3ae590f --- /dev/null +++ b/src/main/java/com/lowagie/tools/handout_pdf.java @@ -0,0 +1,122 @@ +/* + * $Id: handout_pdf.java,v 1.17 2004/12/14 10:59:06 blowagie Exp $ + * $Name: $ + * + * This code is free software. It may only be copied or modified + * if you include the following copyright notice: + * + * This class by Bruno Lowagie. Copyright (c) 2002 Bruno Lowagie. + * + * This code is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * + * itext@lowagie.com + */ + +/** + * This class demonstrates how to splot a PDF file using iText. + * @author Bruno Lowagie + */ +package com.lowagie.tools; + +import java.io.*; + +import com.lowagie.text.*; +import com.lowagie.text.pdf.*; + +/** + * Takes an existing PDF file and makes handouts. + */ +public class handout_pdf extends java.lang.Object { + + /** + * Makes handouts based on an existing PDF file. + * @param args the command line arguments + */ + public static void main (String args[]) { + if (args.length != 3) { + System.err.println("arguments: srcfile destfile pages"); + } + else { + try { + int pages = Integer.parseInt(args[2]); + if (pages < 2 || pages > 8) { + throw new DocumentException("You can't have " + pages + " pages on one page (minimum 2; maximum 8)."); + } + + float x1 = 30f; + float x2 = 280f; + float x3 = 320f; + float x4 = 565f; + + float[] y1 = new float[pages]; + float[] y2 = new float[pages]; + + float height = (778f - (20f * (pages - 1))) / pages; + y1[0] = 812f; + y2[0] = 812f - height; + + for (int i = 1; i < pages; i++) { + y1[i] = y2[i - 1] - 20f; + y2[i] = y1[i] - height; + } + + // we create a reader for a certain document + PdfReader reader = new PdfReader(args[0]); + // we retrieve the total number of pages + int n = reader.getNumberOfPages(); + System.out.println("There are " + n + " pages in the original file."); + + // step 1: creation of a document-object + Document document = new Document(PageSize.A4); + // step 2: we create a writer that listens to the document + PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(args[1])); + // step 3: we open the document + document.open(); + PdfContentByte cb = writer.getDirectContent(); + PdfImportedPage page; + int rotation; + int i = 0; + int p = 0; + // step 4: we add content + while (i < n) { + i++; + Rectangle rect = reader.getPageSizeWithRotation(i); + float factorx = (x2 - x1) / rect.width(); + float factory = (y1[p] - y2[p]) / rect.height(); + float factor = (factorx < factory ? factorx : factory); + float dx = (factorx == factor ? 0f : ((x2 - x1) - rect.width() * factor) / 2f); + float dy = (factory == factor ? 0f : ((y1[p] - y2[p]) - rect.height() * factor) / 2f); + page = writer.getImportedPage(reader, i); + rotation = reader.getPageRotation(i); + if (rotation == 90 || rotation == 270) { + cb.addTemplate(page, 0, -factor, factor, 0, x1 + dx, y2[p] + dy + rect.height() * factor); + } + else { + cb.addTemplate(page, factor, 0, 0, factor, x1 + dx, y2[p] + dy); + } + cb.setRGBColorStroke(0xC0, 0xC0, 0xC0); + cb.rectangle(x3 - 5f, y2[p] - 5f, x4 - x3 + 10f, y1[p] - y2[p] + 10f); + for (float l = y1[p] - 19; l > y2[p]; l -= 16) { + cb.moveTo(x3, l); + cb.lineTo(x4, l); + } + cb.rectangle(x1 + dx, y2[p] + dy, rect.width() * factor, rect.height() * factor); + cb.stroke(); + System.out.println("Processed page " + i); + p++; + if (p == pages) { + p = 0; + document.newPage(); + } + } + // step 5: we close the document + document.close(); + } + catch(Exception e) { + System.err.println(e.getClass().getName() + ": " + e.getMessage()); + } + } + } +} \ No newline at end of file diff --git a/src/main/java/com/lowagie/tools/plugins/AbstractTool.java b/src/main/java/com/lowagie/tools/plugins/AbstractTool.java new file mode 100644 index 0000000..5e57892 --- /dev/null +++ b/src/main/java/com/lowagie/tools/plugins/AbstractTool.java @@ -0,0 +1,341 @@ +/* + * $Id: AbstractTool.java,v 1.10 2006/05/30 09:13:19 blowagie Exp $ + * $Name: $ + * + * Copyright 2005 by Bruno Lowagie. + * + * The contents of this file are subject to the Mozilla Public License Version 1.1 + * (the "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the License. + * + * The Original Code is 'iText, a free JAVA-PDF library'. + * + * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by + * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie. + * All Rights Reserved. + * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer + * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved. + * + * Contributor(s): all the names of the contributors are added in the source code + * where applicable. + * + * Alternatively, the contents of this file may be used under the terms of the + * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the + * provisions of LGPL are applicable instead of those above. If you wish to + * allow use of your version of this file only under the terms of the LGPL + * License and not to allow others to use your version of this file under + * the MPL, indicate your decision by deleting the provisions above and + * replace them with the notice and other provisions required by the LGPL. + * If you do not delete the provisions above, a recipient may use your version + * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE. + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the MPL as stated above or under the terms of the GNU + * Library General Public License as published by the Free Software Foundation; + * either version 2 of the License, or any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more + * details. + * + * If you didn't download this code from the following link, you should check if + * you aren't using an obsolete version: + * http://www.lowagie.com/iText/ + */ +package com.lowagie.tools.plugins; + +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.KeyEvent; +import java.io.File; +import java.util.ArrayList; +import java.util.Iterator; + +import javax.swing.JInternalFrame; +import javax.swing.JMenu; +import javax.swing.JMenuBar; +import javax.swing.JMenuItem; +import javax.swing.JOptionPane; + +import com.lowagie.tools.Executable; +import com.lowagie.tools.ToolMenuItems; +import com.lowagie.tools.arguments.ToolArgument; + +/** + * Every iText tool has to implement this interface. + */ +public abstract class AbstractTool implements ToolMenuItems, ActionListener { + + /** An array with the versions of the tool. */ + public static ArrayList versionsarray = new ArrayList(); + + /** The internal frame of the tool. */ + protected JInternalFrame internalFrame = null; + /** The list of arguments needed by the tool. */ + protected ArrayList arguments = new ArrayList(); + /** Execute menu options */ + protected int menuoptions = MENU_EXECUTE; + /** a menu option */ + public static final int MENU_EXECUTE = 1; + /** a menu option */ + public static final int MENU_EXECUTE_SHOW = 2; + /** a menu option */ + public static final int MENU_EXECUTE_PRINT = 4; + /** a menu option */ + public static final int MENU_EXECUTE_PRINT_SILENT = 8; + + /** + * Sets the arguments. + * @param arguments The arguments to set. + */ + public void setArguments(ArrayList arguments) { + this.arguments = arguments; + } + + /** + * Sets the arguments. + * @param args the arguments as String-array. + */ + public void setArguments(String[] args) { + int counter = 0; + ToolArgument argument; + for (Iterator i = arguments.iterator(); i.hasNext(); ) { + argument = (ToolArgument) i.next(); + if (args.length > counter) { + argument.setValue(args[counter]); + } + else { + break; + } + counter++; + } + } + + /** + * Gets the arguments. + * @return Returns the arguments. + */ + public ArrayList getArguments() { + return arguments; + } + + /** + * Gets the value of a given argument. + * @param name the name of the argument + * @return the value of an argument as an Object. + * @throws InstantiationException + */ + public Object getValue(String name) throws InstantiationException { + ToolArgument argument; + for (Iterator i = arguments.iterator(); i.hasNext(); ) { + argument = (ToolArgument) i.next(); + if (name.equals(argument.getName())) { + return argument.getArgument(); + } + } + return null; + } + + /** + * Sets the internal frame. + * @param internalFrame The internalFrame to set. + */ + public void setInternalFrame(JInternalFrame internalFrame) { + this.internalFrame = internalFrame; + } + + /** + * Returns the internal frame. Creates one if it's null. + * @return Returns the internalFrame. + */ + public JInternalFrame getInternalFrame() { + if (internalFrame == null) { + createFrame(); + } + return internalFrame; + } + + /** + * Gets the menubar. + * @return a menubar for this tool + */ + public JMenuBar getMenubar() { + JMenuBar menubar = new JMenuBar(); + JMenu tool = new JMenu(TOOL); + tool.setMnemonic(KeyEvent.VK_F); + JMenuItem usage = new JMenuItem(USAGE); + usage.setMnemonic(KeyEvent.VK_U); + usage.addActionListener(this); + tool.add(usage); + JMenuItem args = new JMenuItem(ARGUMENTS); + args.setMnemonic(KeyEvent.VK_A); + args.addActionListener(this); + tool.add(args); + if ((menuoptions & MENU_EXECUTE) > 0) { + JMenuItem execute = new JMenuItem(EXECUTE); + execute.setMnemonic(KeyEvent.VK_E); + execute.addActionListener(this); + tool.add(execute); + } + if ((menuoptions & MENU_EXECUTE_SHOW) > 0) { + JMenuItem execute = new JMenuItem(EXECUTESHOW); + execute.addActionListener(this); + tool.add(execute); + } + if ((menuoptions & MENU_EXECUTE_PRINT) > 0) { + JMenuItem execute = new JMenuItem(EXECUTEPRINT); + execute.addActionListener(this); + tool.add(execute); + } + if ((menuoptions & MENU_EXECUTE_PRINT_SILENT) > 0) { + JMenuItem execute = new JMenuItem(EXECUTEPRINTSILENT); + execute.addActionListener(this); + tool.add(execute); + } + JMenuItem close = new JMenuItem(CLOSE); + close.setMnemonic(KeyEvent.VK_C); + close.addActionListener(this); + tool.add(close); + menubar.add(tool); + if (arguments.size() > 0) { + JMenu params = new JMenu(ARGUMENTS); + tool.setMnemonic(KeyEvent.VK_T); + JMenuItem item; + ToolArgument argument; + for (Iterator i = arguments.iterator(); i.hasNext(); ) { + argument = (ToolArgument)i.next(); + item = new JMenuItem(argument.getName()); + item.setToolTipText(argument.getDescription()); + item.addActionListener(argument); + params.add(item); + } + menubar.add(params); + } + return menubar; + } + + /** + * Gets the usage of the tool. + * @return a String describing how to use the tool. + */ + public String getUsage() { + StringBuffer buf = new StringBuffer("java "); + buf.append(getClass().getName()); + ToolArgument argument; + for (Iterator i = arguments.iterator(); i.hasNext(); ) { + argument = (ToolArgument) i.next(); + buf.append(" "); + buf.append(argument.getName()); + } + buf.append("\n"); + for (Iterator i = arguments.iterator(); i.hasNext(); ) { + argument = (ToolArgument) i.next(); + buf.append(argument.getUsage()); + } + return buf.toString(); + } + + /** + * Gets the current arguments of the tool. + * @return a String with the list of arguments and their values. + */ + public String getArgs() { + StringBuffer buf = new StringBuffer("Current arguments:\n"); + ToolArgument argument; + for (Iterator i = arguments.iterator(); i.hasNext(); ) { + argument = (ToolArgument) i.next(); + buf.append(" "); + buf.append(argument.getName()); + if (argument.getValue() == null) { + buf.append(" = null\n"); + } + else { + buf.append(" = '"); + buf.append(argument.getValue()); + buf.append("'\n"); + } + } + return buf.toString(); + } + + /** + * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent) + */ + public void actionPerformed(ActionEvent evt) { + if (CLOSE.equals(evt.getActionCommand())) { + System.out.println("=== " + getInternalFrame().getTitle() + " CLOSED ==="); + internalFrame.dispose(); + } + if (USAGE.equals(evt.getActionCommand())) { + JOptionPane.showMessageDialog(internalFrame, getUsage()); + } + if (ARGUMENTS.equals(evt.getActionCommand())) { + JOptionPane.showMessageDialog(internalFrame, getArgs()); + } + if (EXECUTE.equals(evt.getActionCommand())) { + this.execute(); + } + if (EXECUTESHOW.equals(evt.getActionCommand())) { + this.execute(); + try { + Executable.openDocument(getDestPathPDF()); + } catch (Exception e) { + System.err.println(e.getMessage()); + } + } + if (EXECUTEPRINT.equals(evt.getActionCommand())) { + this.execute(); + try { + Executable.printDocument(getDestPathPDF()); + } catch (Exception e) { + System.err.println(e.getMessage()); + } + } + if (EXECUTEPRINTSILENT.equals(evt.getActionCommand())) { + this.execute(); + try { + Executable.printDocumentSilent(getDestPathPDF()); + } catch (Exception e) { + System.err.println(e.getMessage()); + } + } + } + + /** + * Gets the PDF file that should be generated (or null if the output isn't a PDF file). + * @return the PDF file that should be generated + * @throws InstantiationException + */ + protected abstract File getDestPathPDF() throws InstantiationException; + + /** + * Creates the internal frame. + */ + protected abstract void createFrame(); + + /** + * Executes the tool (in most cases this generates a PDF file). + */ + public abstract void execute(); + + /** + * Indicates that the value of an argument has changed. + * @param arg the argument that has changed + */ + public abstract void valueHasChanged(ToolArgument arg); + + /** + * Add the version of the plugin to the versions array. + * @param version the version to add. + */ + protected static void addVersion(String version) { + version = version.substring(5, version.length() - 7); + version = version.substring(0, version.lastIndexOf(" ")); + versionsarray.add(version); + } +} diff --git a/src/main/java/com/lowagie/tools/plugins/Bookmarks2XML.java b/src/main/java/com/lowagie/tools/plugins/Bookmarks2XML.java new file mode 100644 index 0000000..444a775 --- /dev/null +++ b/src/main/java/com/lowagie/tools/plugins/Bookmarks2XML.java @@ -0,0 +1,148 @@ +/* + * $Id: Bookmarks2XML.java,v 1.1 2006/02/02 15:56:53 blowagie Exp $ + * $Name: $ + * + * Copyright 2005 by Hans-Werner Hilse. + * + * The contents of this file are subject to the Mozilla Public License Version 1.1 + * (the "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the License. + * + * The Original Code is 'iText, a free JAVA-PDF library'. + * + * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by + * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie. + * All Rights Reserved. + * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer + * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved. + * + * Contributor(s): all the names of the contributors are added in the source code + * where applicable. + * + * Alternatively, the contents of this file may be used under the terms of the + * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the + * provisions of LGPL are applicable instead of those above. If you wish to + * allow use of your version of this file only under the terms of the LGPL + * License and not to allow others to use your version of this file under + * the MPL, indicate your decision by deleting the provisions above and + * replace them with the notice and other provisions required by the LGPL. + * If you do not delete the provisions above, a recipient may use your version + * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE. + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the MPL as stated above or under the terms of the GNU + * Library General Public License as published by the Free Software Foundation; + * either version 2 of the License, or any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more + * details. + * + * If you didn't download this code from the following link, you should check if + * you aren't using an obsolete version: + * http://www.lowagie.com/iText/ + */ +package com.lowagie.tools.plugins; + +import java.io.File; +import java.io.FileOutputStream; +import java.util.List; + +import javax.swing.JInternalFrame; +import javax.swing.JOptionPane; + +import com.lowagie.text.pdf.PdfReader; +import com.lowagie.text.pdf.SimpleBookmark; +import com.lowagie.tools.arguments.FileArgument; +import com.lowagie.tools.arguments.PdfFilter; +import com.lowagie.tools.arguments.ToolArgument; + +/** + * Allows you to add bookmarks to an existing PDF file + */ +public class Bookmarks2XML extends AbstractTool { + + static { + addVersion("$Id: Bookmarks2XML.java,v 1.1 2006/02/02 15:56:53 blowagie Exp $"); + } + + /** + * Constructs an Bookmarks2XML object. + */ + public Bookmarks2XML() { + arguments.add(new FileArgument(this, "pdffile", "the PDF from which you want to extract bookmarks", false, new PdfFilter())); + arguments.add(new FileArgument(this, "xmlfile", "the resulting bookmarks file in XML", true)); + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#createFrame() + */ + protected void createFrame() { + internalFrame = new JInternalFrame("Bookmarks2XML", true, true, true); + internalFrame.setSize(300, 80); + internalFrame.setJMenuBar(getMenubar()); + System.out.println("=== Bookmarks2XML OPENED ==="); + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#execute() + */ + public void execute() { + try { + if (getValue("xmlfile") == null) throw new InstantiationException("You need to choose an xml file"); + if (getValue("pdffile") == null) throw new InstantiationException("You need to choose a source PDF file"); + PdfReader reader = new PdfReader(((File)getValue("pdffile")).getAbsolutePath()); + reader.consolidateNamedDestinations(); + List bookmarks = SimpleBookmark.getBookmark( reader ); + // save them in XML format + FileOutputStream bmWriter = new FileOutputStream( (File)getValue("xmlfile") ); + SimpleBookmark.exportToXML(bookmarks, bmWriter, "UTF-8", false); + bmWriter.close(); + } + catch(Exception e) { + e.printStackTrace(); + JOptionPane.showMessageDialog(internalFrame, + e.getMessage(), + e.getClass().getName(), + JOptionPane.ERROR_MESSAGE); + System.err.println(e.getMessage()); + } + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#valueHasChanged(com.lowagie.tools.arguments.ToolArgument) + */ + public void valueHasChanged(ToolArgument arg) { + if (internalFrame == null) { + // if the internal frame is null, the tool was called from the commandline + return; + } + // represent the changes of the argument in the internal frame + } + + /** + * Allows you to generate an index file in HTML containing Bookmarks to an existing PDF file. + * @param args + */ + public static void main(String[] args) { + Bookmarks2XML tool = new Bookmarks2XML(); + if (args.length < 2) { + System.err.println(tool.getUsage()); + } + tool.setArguments(args); + tool.execute(); + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#getDestPathPDF() + */ + protected File getDestPathPDF() throws InstantiationException { + throw new InstantiationException("There is no file to show."); + } + +} diff --git a/src/main/java/com/lowagie/tools/plugins/Burst.java b/src/main/java/com/lowagie/tools/plugins/Burst.java new file mode 100644 index 0000000..1aca12a --- /dev/null +++ b/src/main/java/com/lowagie/tools/plugins/Burst.java @@ -0,0 +1,140 @@ +/* + * $Id: Burst.java,v 1.8 2005/11/29 21:05:02 blowagie Exp $ + * $Name: $ + * + * This code is free software. It may only be copied or modified + * if you include the following copyright notice: + * + * This class by Mark Thompson. Copyright (c) 2002 Mark Thompson. + * + * This code is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * + * itext-questions@list.sourceforge.net + */ +package com.lowagie.tools.plugins; + +import java.io.File; +import java.io.FileOutputStream; + +import javax.swing.JInternalFrame; + +import com.lowagie.text.Document; +import com.lowagie.text.pdf.PdfContentByte; +import com.lowagie.text.pdf.PdfImportedPage; +import com.lowagie.text.pdf.PdfReader; +import com.lowagie.text.pdf.PdfWriter; +import com.lowagie.tools.arguments.FileArgument; +import com.lowagie.tools.arguments.LabelAccessory; +import com.lowagie.tools.arguments.PdfFilter; +import com.lowagie.tools.arguments.ToolArgument; + +/** + * This tool lets you split a PDF in several separate PDF files (1 per page). + */ +public class Burst extends AbstractTool { + + static { + addVersion("$Id: Burst.java,v 1.8 2005/11/29 21:05:02 blowagie Exp $"); + } + + /** + * Constructs a Burst object. + */ + public Burst() { + FileArgument f = new FileArgument(this, "srcfile", "The file you want to split", false, new PdfFilter()); + f.setLabel(new LabelAccessory()); + arguments.add(f); + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#createFrame() + */ + protected void createFrame() { + internalFrame = new JInternalFrame("Burst", true, false, true); + internalFrame.setSize(300, 80); + internalFrame.setJMenuBar(getMenubar()); + System.out.println("=== Burst OPENED ==="); + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#execute() + */ + public void execute() { + try { + if (getValue("srcfile") == null) throw new InstantiationException("You need to choose a sourcefile"); + File src = (File)getValue("srcfile"); + File directory = src.getParentFile(); + String name = src.getName(); + name = name.substring(0, name.lastIndexOf(".")); + // we create a reader for a certain document + PdfReader reader = new PdfReader(src.getAbsolutePath()); + // we retrieve the total number of pages + int n = reader.getNumberOfPages(); + int digits = 1 + (n / 10); + System.out.println("There are " + n + " pages in the original file."); + Document document; + int pagenumber; + String filename; + for (int i = 0; i < n; i++) { + pagenumber = i + 1; + filename = String.valueOf(pagenumber); + while (filename.length() < digits) filename = "0" + filename; + filename = "_" + filename + ".pdf"; + // step 1: creation of a document-object + document = new Document(reader.getPageSizeWithRotation(pagenumber)); + // step 2: we create a writer that listens to the document + PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(new File(directory, name + filename))); + // step 3: we open the document + document.open(); + PdfContentByte cb = writer.getDirectContent(); + PdfImportedPage page = writer.getImportedPage(reader, pagenumber); + int rotation = reader.getPageRotation(pagenumber); + if (rotation == 90 || rotation == 270) { + cb.addTemplate(page, 0, -1f, 1f, 0, 0, reader.getPageSizeWithRotation(pagenumber).height()); + } + else { + cb.addTemplate(page, 1f, 0, 0, 1f, 0, 0); + } + // step 5: we close the document + document.close(); + } + } + catch(Exception e) { + e.printStackTrace(); + } + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#valueHasChanged(com.lowagie.tools.arguments.ToolArgument) + */ + public void valueHasChanged(ToolArgument arg) { + if (internalFrame == null) { + // if the internal frame is null, the tool was called from the commandline + return; + } + // represent the changes of the argument in the internal frame + } + + + /** + * Divide PDF file into pages. + * @param args + */ + public static void main(String[] args) { + Burst tool = new Burst(); + if (args.length < 1) { + System.err.println(tool.getUsage()); + } + tool.setArguments(args); + tool.execute(); + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#getDestPathPDF() + */ + protected File getDestPathPDF() throws InstantiationException { + throw new InstantiationException("There is more than one destfile."); + } +} diff --git a/src/main/java/com/lowagie/tools/plugins/Concat.java b/src/main/java/com/lowagie/tools/plugins/Concat.java new file mode 100644 index 0000000..b6eb369 --- /dev/null +++ b/src/main/java/com/lowagie/tools/plugins/Concat.java @@ -0,0 +1,185 @@ +/* + * $Id: Concat.java,v 1.7 2005/11/29 21:05:02 blowagie Exp $ + * $Name: $ + * + * Copyright 2005 by Bruno Lowagie. + * + * The contents of this file are subject to the Mozilla Public License Version 1.1 + * (the "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the License. + * + * The Original Code is 'iText, a free JAVA-PDF library'. + * + * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by + * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie. + * All Rights Reserved. + * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer + * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved. + * + * Contributor(s): all the names of the contributors are added in the source code + * where applicable. + * + * Alternatively, the contents of this file may be used under the terms of the + * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the + * provisions of LGPL are applicable instead of those above. If you wish to + * allow use of your version of this file only under the terms of the LGPL + * License and not to allow others to use your version of this file under + * the MPL, indicate your decision by deleting the provisions above and + * replace them with the notice and other provisions required by the LGPL. + * If you do not delete the provisions above, a recipient may use your version + * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE. + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the MPL as stated above or under the terms of the GNU + * Library General Public License as published by the Free Software Foundation; + * either version 2 of the License, or any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more + * details. + * + * If you didn't download this code from the following link, you should check if + * you aren't using an obsolete version: + * http://www.lowagie.com/iText/ + */ +package com.lowagie.tools.plugins; + +import java.io.File; +import java.io.FileOutputStream; +import java.util.ArrayList; +import java.util.List; + +import javax.swing.JInternalFrame; + +import com.lowagie.text.Document; +import com.lowagie.text.pdf.PdfCopy; +import com.lowagie.text.pdf.PdfImportedPage; +import com.lowagie.text.pdf.PdfReader; +import com.lowagie.text.pdf.SimpleBookmark; +import com.lowagie.tools.arguments.FileArgument; +import com.lowagie.tools.arguments.PdfFilter; +import com.lowagie.tools.arguments.ToolArgument; + +/** + * Concatenates two PDF files + */ +public class Concat extends AbstractTool { + + static { + addVersion("$Id: Concat.java,v 1.7 2005/11/29 21:05:02 blowagie Exp $"); + } + /** + * Constructs a Concat object. + */ + public Concat() { + menuoptions = MENU_EXECUTE | MENU_EXECUTE_SHOW; + arguments.add(new FileArgument(this, "srcfile1", "The first PDF file", false, new PdfFilter())); + arguments.add(new FileArgument(this, "srcfile2", "The second PDF file", false, new PdfFilter())); + arguments.add(new FileArgument(this, "destfile", "The file to which the concatenated PDF has to be written", true, new PdfFilter())); + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#createFrame() + */ + protected void createFrame() { + internalFrame = new JInternalFrame("Concatenate 2 PDF files", true, false, true); + internalFrame.setSize(300, 80); + internalFrame.setJMenuBar(getMenubar()); + System.out.println("=== Concat OPENED ==="); + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#execute() + */ + public void execute() { + try { + String[] files = new String[2]; + if (getValue("srcfile1") == null) throw new InstantiationException("You need to choose a first sourcefile"); + files[0] = ((File)getValue("srcfile1")).getAbsolutePath(); + if (getValue("srcfile2") == null) throw new InstantiationException("You need to choose a second sourcefile"); + files[1] = ((File)getValue("srcfile2")).getAbsolutePath(); + if (getValue("destfile") == null) throw new InstantiationException("You need to choose a destination file"); + File pdf_file = (File)getValue("destfile"); + int pageOffset = 0; + ArrayList master = new ArrayList(); + Document document = null; + PdfCopy writer = null; + for (int i = 0; i < 2; i++) { + // we create a reader for a certain document + PdfReader reader = new PdfReader(files[i]); + reader.consolidateNamedDestinations(); + // we retrieve the total number of pages + int n = reader.getNumberOfPages(); + List bookmarks = SimpleBookmark.getBookmark(reader); + if (bookmarks != null) { + if (pageOffset != 0) + SimpleBookmark.shiftPageNumbers(bookmarks, pageOffset, null); + master.addAll(bookmarks); + } + pageOffset += n; + System.out.println("There are " + n + " pages in " + files[i]); + if (i == 0) { + // step 1: creation of a document-object + document = new Document(reader.getPageSizeWithRotation(1)); + // step 2: we create a writer that listens to the document + writer = new PdfCopy(document, new FileOutputStream(pdf_file)); + // step 3: we open the document + document.open(); + } + // step 4: we add content + PdfImportedPage page; + for (int p = 0; p < n; ) { + ++p; + page = writer.getImportedPage(reader, p); + writer.addPage(page); + System.out.println("Processed page " + p); + } + } + if (master.size() > 0) + writer.setOutlines(master); + // step 5: we close the document + document.close(); + } + catch(Exception e) { + e.printStackTrace(); + } + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#valueHasChanged(com.lowagie.tools.arguments.ToolArgument) + */ + public void valueHasChanged(ToolArgument arg) { + if (internalFrame == null) { + // if the internal frame is null, the tool was called from the commandline + return; + } + // represent the changes of the argument in the internal frame + } + + + /** + * Concatenates two PDF files. + * @param args + */ + public static void main(String[] args) { + Concat tool = new Concat(); + if (args.length < 2) { + System.err.println(tool.getUsage()); + } + tool.setArguments(args); + tool.execute(); + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#getDestPathPDF() + */ + protected File getDestPathPDF() throws InstantiationException { + return (File)getValue("destfile"); + } + +} diff --git a/src/main/java/com/lowagie/tools/plugins/Decrypt.java b/src/main/java/com/lowagie/tools/plugins/Decrypt.java new file mode 100644 index 0000000..a94b84f --- /dev/null +++ b/src/main/java/com/lowagie/tools/plugins/Decrypt.java @@ -0,0 +1,148 @@ +/* + * $Id: Decrypt.java,v 1.4 2005/11/29 21:05:02 blowagie Exp $ + * $Name: $ + * + * Copyright 2005 by Bruno Lowagie. + * + * The contents of this file are subject to the Mozilla Public License Version 1.1 + * (the "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the License. + * + * The Original Code is 'iText, a free JAVA-PDF library'. + * + * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by + * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie. + * All Rights Reserved. + * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer + * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved. + * + * Contributor(s): all the names of the contributors are added in the source code + * where applicable. + * + * Alternatively, the contents of this file may be used under the terms of the + * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the + * provisions of LGPL are applicable instead of those above. If you wish to + * allow use of your version of this file only under the terms of the LGPL + * License and not to allow others to use your version of this file under + * the MPL, indicate your decision by deleting the provisions above and + * replace them with the notice and other provisions required by the LGPL. + * If you do not delete the provisions above, a recipient may use your version + * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE. + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the MPL as stated above or under the terms of the GNU + * Library General Public License as published by the Free Software Foundation; + * either version 2 of the License, or any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more + * details. + * + * If you didn't download this code from the following link, you should check if + * you aren't using an obsolete version: + * http://www.lowagie.com/iText/ + */ +package com.lowagie.tools.plugins; + +import java.io.File; +import java.io.FileOutputStream; + +import javax.swing.JInternalFrame; +import javax.swing.JOptionPane; + +import com.lowagie.text.pdf.PdfReader; +import com.lowagie.text.pdf.PdfStamper; +import com.lowagie.tools.arguments.FileArgument; +import com.lowagie.tools.arguments.PdfFilter; +import com.lowagie.tools.arguments.ToolArgument; + +/** + * Allows you to decrypt an existing PDF file. + */ +public class Decrypt extends AbstractTool { + + static { + addVersion("$Id: Decrypt.java,v 1.4 2005/11/29 21:05:02 blowagie Exp $"); + } + + + /** + * Constructs an Decrypt object. + */ + public Decrypt() { + arguments.add(new FileArgument(this, "srcfile", "The file you want to decrypt", false, new PdfFilter())); + arguments.add(new FileArgument(this, "destfile", "The file to which the decrypted PDF has to be written", true, new PdfFilter())); + arguments.add(new ToolArgument(this, "ownerpassword", "The ownerpassword you want to add to the PDF file", String.class.getName())); + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#createFrame() + */ + protected void createFrame() { + internalFrame = new JInternalFrame("Decrypt", true, false, true); + internalFrame.setSize(300, 80); + internalFrame.setJMenuBar(getMenubar()); + System.out.println("=== Decrypt OPENED ==="); + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#execute() + */ + public void execute() { + try { + if (getValue("srcfile") == null) throw new InstantiationException("You need to choose a sourcefile"); + if (getValue("destfile") == null) throw new InstantiationException("You need to choose a destination file"); + byte[] ownerpassword = null; + if (getValue("ownerpassword") != null) { + ownerpassword = ((String)getValue("ownerpassword")).getBytes(); + } + PdfReader reader = new PdfReader(((File)getValue("srcfile")).getAbsolutePath(), ownerpassword); + PdfStamper stamper = new PdfStamper(reader, new FileOutputStream((File)getValue("destfile"))); + stamper.close(); + } + catch(Exception e) { + JOptionPane.showMessageDialog(internalFrame, + e.getMessage(), + e.getClass().getName(), + JOptionPane.ERROR_MESSAGE); + System.err.println(e.getMessage()); + } + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#valueHasChanged(com.lowagie.tools.arguments.ToolArgument) + */ + public void valueHasChanged(ToolArgument arg) { + if (internalFrame == null) { + // if the internal frame is null, the tool was called from the commandline + return; + } + // represent the changes of the argument in the internal frame + } + + /** + * Decrypts an existing PDF file. + * @param args + */ + public static void main(String[] args) { + Decrypt tool = new Decrypt(); + if (args.length < 2) { + System.err.println(tool.getUsage()); + } + tool.setArguments(args); + tool.execute(); + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#getDestPathPDF() + */ + protected File getDestPathPDF() throws InstantiationException { + return (File)getValue("destfile"); + } + +} diff --git a/src/main/java/com/lowagie/tools/plugins/DvdCover.java b/src/main/java/com/lowagie/tools/plugins/DvdCover.java new file mode 100644 index 0000000..718ec6d --- /dev/null +++ b/src/main/java/com/lowagie/tools/plugins/DvdCover.java @@ -0,0 +1,201 @@ +/* + * $Id: DvdCover.java,v 1.11 2005/11/29 21:05:02 blowagie Exp $ + * $Name: $ + * + * Copyright 2005 by Bruno Lowagie. + * + * The contents of this file are subject to the Mozilla Public License Version 1.1 + * (the "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the License. + * + * The Original Code is 'iText, a free JAVA-PDF library'. + * + * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by + * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie. + * All Rights Reserved. + * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer + * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved. + * + * Contributor(s): all the names of the contributors are added in the source code + * where applicable. + * + * Alternatively, the contents of this file may be used under the terms of the + * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the + * provisions of LGPL are applicable instead of those above. If you wish to + * allow use of your version of this file only under the terms of the LGPL + * License and not to allow others to use your version of this file under + * the MPL, indicate your decision by deleting the provisions above and + * replace them with the notice and other provisions required by the LGPL. + * If you do not delete the provisions above, a recipient may use your version + * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE. + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the MPL as stated above or under the terms of the GNU + * Library General Public License as published by the Free Software Foundation; + * either version 2 of the License, or any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more + * details. + * + * If you didn't download this code from the following link, you should check if + * you aren't using an obsolete version: + * http://www.lowagie.com/iText/ + */ +package com.lowagie.tools.plugins; + +import java.awt.Color; +import java.io.File; +import java.io.FileOutputStream; + +import javax.swing.JInternalFrame; +import javax.swing.JOptionPane; + +import com.lowagie.text.Document; +import com.lowagie.text.DocumentException; +import com.lowagie.text.Element; +import com.lowagie.text.Image; +import com.lowagie.text.Rectangle; +import com.lowagie.text.pdf.BaseFont; +import com.lowagie.text.pdf.PdfContentByte; +import com.lowagie.text.pdf.PdfWriter; +import com.lowagie.tools.arguments.FileArgument; +import com.lowagie.tools.arguments.ImageArgument; +import com.lowagie.tools.arguments.PdfFilter; +import com.lowagie.tools.arguments.ToolArgument; + +/** + * This is a simple tool that generates a cover for a DVD. + */ +public class DvdCover extends AbstractTool { + + static { + addVersion("$Id: DvdCover.java,v 1.11 2005/11/29 21:05:02 blowagie Exp $"); + } + + /** + * Constructs a DvdCover object. + */ + public DvdCover() { + menuoptions = MENU_EXECUTE | MENU_EXECUTE_SHOW | MENU_EXECUTE_PRINT; + arguments.add(new FileArgument(this, "destfile", "The file to which the PDF has to be written", true, new PdfFilter())); + arguments.add(new ToolArgument(this, "title", "The title of the DVD", String.class.getName())); + arguments.add(new ToolArgument(this, "backgroundcolor", "The backgroundcolor of the DVD Cover (for instance 0xFFFFFF)", Color.class.getName())); + arguments.add(new ImageArgument(this, "front", "The front image of the DVD Cover")); + arguments.add(new ImageArgument(this, "back", "The back image of the DVD Cover")); + arguments.add(new ImageArgument(this, "side", "The side image of the DVD Cover")); + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#createFrame() + */ + protected void createFrame() { + internalFrame = new JInternalFrame("Make your own DVD Cover", true, false, true); + internalFrame.setSize(300, 80); + internalFrame.setJMenuBar(getMenubar()); + System.out.println("=== DvdCover OPENED ==="); + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#execute() + */ + public void execute() { + try { + // step 1: creation of a document-object + Rectangle pageSize = new Rectangle(780, 525); + if (getValue("backgroundcolor") != null) pageSize.setBackgroundColor((Color)getValue("backgroundcolor")); + Document document = new Document(pageSize); + // step 2: + // we create a writer that listens to the document + // and directs a PDF-stream to a file + if (getValue("destfile") == null) throw new DocumentException("You must provide a destination file!"); + PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream((File)getValue("destfile"))); + + // step 3: we open the document + document.open(); + + // step 4: + PdfContentByte cb = writer.getDirectContent(); + if (getValue("title") != null) { + cb.setFontAndSize(BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, false), 24); + cb.beginText(); + if (getValue("front") == null) { + cb.showTextAligned(Element.ALIGN_CENTER, (String)getValue("title"), 595f, 262f, 0f); + } + if (getValue("side") == null) { + cb.showTextAligned(Element.ALIGN_CENTER, (String)getValue("title"), 385f, 262f, 270f); + } + cb.endText(); + } + cb.moveTo(370, 0); + cb.lineTo(370, 525); + cb.moveTo(410, 525); + cb.lineTo(410, 0); + cb.stroke(); + if (getValue("front") != null) { + Image front = (Image)getValue("front"); + front.scaleToFit(370, 525); + front.setAbsolutePosition(410f + (370f - front.scaledWidth()) / 2f, (525f - front.scaledHeight()) / 2f); + document.add(front); + } + if (getValue("back") != null) { + Image back = (Image)getValue("back"); + back.scaleToFit(370, 525); + back.setAbsolutePosition((370f - back.scaledWidth()) / 2f, (525f - back.scaledHeight()) / 2f); + document.add(back); + } + if (getValue("side") != null) { + Image side = (Image)getValue("side"); + side.scaleToFit(40, 525); + side.setAbsolutePosition(370 + (40f - side.scaledWidth()) / 2f, (525f - side.scaledHeight()) / 2f); + document.add(side); + } + + // step 5: we close the document + document.close(); + } + catch(Exception e) { + JOptionPane.showMessageDialog(internalFrame, + e.getMessage(), + e.getClass().getName(), + JOptionPane.ERROR_MESSAGE); + System.err.println(e.getMessage()); + } + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#valueHasChanged(com.lowagie.tools.arguments.ToolArgument) + */ + public void valueHasChanged(ToolArgument arg) { + if (internalFrame == null) { + // if the internal frame is null, the tool was called from the commandline + return; + } + // represent the changes of the argument in the internal frame + } + + /** + * Generates a DVD Cover in PDF. + * @param args an array containing [0] a filename [1] a title [2] a backgroundcolor [3] a front image [4] a back image [5] a side image + */ + public static void main(String[] args) { + DvdCover tool = new DvdCover(); + if (args.length == 0) { + System.err.println(tool.getUsage()); + } + tool.setArguments(args); + tool.execute(); + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#getDestPathPDF() + */ + protected File getDestPathPDF() throws InstantiationException { + return (File)getValue("destfile"); + } +} \ No newline at end of file diff --git a/src/main/java/com/lowagie/tools/plugins/Encrypt.java b/src/main/java/com/lowagie/tools/plugins/Encrypt.java new file mode 100644 index 0000000..7821759 --- /dev/null +++ b/src/main/java/com/lowagie/tools/plugins/Encrypt.java @@ -0,0 +1,193 @@ +/* + * $Id: Encrypt.java,v 1.11 2005/11/29 21:05:02 blowagie Exp $ + * $Name: $ + * + * Copyright 2005 by Bruno Lowagie. + * + * The contents of this file are subject to the Mozilla Public License Version 1.1 + * (the "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the License. + * + * The Original Code is 'iText, a free JAVA-PDF library'. + * + * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by + * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie. + * All Rights Reserved. + * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer + * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved. + * + * Contributor(s): all the names of the contributors are added in the source code + * where applicable. + * + * Alternatively, the contents of this file may be used under the terms of the + * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the + * provisions of LGPL are applicable instead of those above. If you wish to + * allow use of your version of this file only under the terms of the LGPL + * License and not to allow others to use your version of this file under + * the MPL, indicate your decision by deleting the provisions above and + * replace them with the notice and other provisions required by the LGPL. + * If you do not delete the provisions above, a recipient may use your version + * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE. + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the MPL as stated above or under the terms of the GNU + * Library General Public License as published by the Free Software Foundation; + * either version 2 of the License, or any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more + * details. + * + * If you didn't download this code from the following link, you should check if + * you aren't using an obsolete version: + * http://www.lowagie.com/iText/ + */ +package com.lowagie.tools.plugins; + +import java.io.File; +import java.io.FileOutputStream; + +import javax.swing.JInternalFrame; +import javax.swing.JOptionPane; + +import com.lowagie.text.pdf.PdfEncryptor; +import com.lowagie.text.pdf.PdfReader; +import com.lowagie.text.pdf.PdfWriter; +import com.lowagie.tools.arguments.BitsetArgument; +import com.lowagie.tools.arguments.FileArgument; +import com.lowagie.tools.arguments.OptionArgument; +import com.lowagie.tools.arguments.PdfFilter; +import com.lowagie.tools.arguments.ToolArgument; + +/** + * Allows you to encrypt an existing PDF file. + */ +public class Encrypt extends AbstractTool { + + static { + addVersion("$Id: Encrypt.java,v 1.11 2005/11/29 21:05:02 blowagie Exp $"); + } + private final static int PERMISSIONS[] = { + PdfWriter.AllowPrinting, + PdfWriter.AllowModifyContents, + PdfWriter.AllowCopy, + PdfWriter.AllowModifyAnnotations, + PdfWriter.AllowFillIn, + PdfWriter.AllowScreenReaders, + PdfWriter.AllowAssembly, + PdfWriter.AllowDegradedPrinting}; + private final static String PERMISSION_OPTIONS[] = { + "AllowPrinting", + "AllowModifyContents", + "AllowCopy", + "AllowModifyAnnotations", + "AllowFillIn (128 bit only)", + "AllowScreenReaders (128 bit only)", + "AllowAssembly (128 bit only)", + "AllowDegradedPrinting (128 bit only)" + }; + + + /** + * Constructs an Encrypt object. + */ + public Encrypt() { + arguments.add(new FileArgument(this, "srcfile", "The file you want to encrypt", false, new PdfFilter())); + arguments.add(new FileArgument(this, "destfile", "The file to which the encrypted PDF has to be written", true, new PdfFilter())); + arguments.add(new ToolArgument(this, "ownerpassword", "The ownerpassword you want to add to the PDF file", String.class.getName())); + arguments.add(new ToolArgument(this, "userpassword", "The userpassword you want to add to the PDF file", String.class.getName())); + arguments.add(new BitsetArgument(this, "permissions", "Permissions on the file", PERMISSION_OPTIONS)); + OptionArgument oa = new OptionArgument(this, "strength", "Strength of the encryption"); + oa.addOption("40 bit encryption", "40"); + oa.addOption("128 bit encryption", "128"); + arguments.add(oa); + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#createFrame() + */ + protected void createFrame() { + internalFrame = new JInternalFrame("Encrypt", true, false, true); + internalFrame.setSize(300, 80); + internalFrame.setJMenuBar(getMenubar()); + System.out.println("=== Encrypt OPENED ==="); + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#execute() + */ + public void execute() { + try { + if (getValue("srcfile") == null) throw new InstantiationException("You need to choose a sourcefile"); + if (getValue("destfile") == null) throw new InstantiationException("You need to choose a destination file"); + int permissions = 0; + String p = (String)getValue("permissions"); + if (p != null) { + for (int k = 0; k < p.length(); ++k) { + permissions |= (p.charAt(k) == '0' ? 0 : PERMISSIONS[k]); + } + } + byte[] userpassword = null; + if (getValue("userpassword") != null) { + userpassword = ((String)getValue("userpassword")).getBytes(); + } + byte[] ownerpassword = null; + if (getValue("ownerpassword") != null) { + ownerpassword = ((String)getValue("ownerpassword")).getBytes(); + } + PdfReader reader = new PdfReader(((File)getValue("srcfile")).getAbsolutePath()); + PdfEncryptor.encrypt( + reader, + new FileOutputStream((File)getValue("destfile")), + userpassword, + ownerpassword, + permissions, + "128".equals(getValue("strength")) + ); + } + catch(Exception e) { + JOptionPane.showMessageDialog(internalFrame, + e.getMessage(), + e.getClass().getName(), + JOptionPane.ERROR_MESSAGE); + System.err.println(e.getMessage()); + } + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#valueHasChanged(com.lowagie.tools.arguments.ToolArgument) + */ + public void valueHasChanged(ToolArgument arg) { + if (internalFrame == null) { + // if the internal frame is null, the tool was called from the commandline + return; + } + // represent the changes of the argument in the internal frame + } + + /** + * Encrypts an existing PDF file. + * @param args + */ + public static void main(String[] args) { + Encrypt tool = new Encrypt(); + if (args.length < 2) { + System.err.println(tool.getUsage()); + } + tool.setArguments(args); + tool.execute(); + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#getDestPathPDF() + */ + protected File getDestPathPDF() throws InstantiationException { + return (File)getValue("destfile"); + } + +} diff --git a/src/main/java/com/lowagie/tools/plugins/ExtractAttachments.java b/src/main/java/com/lowagie/tools/plugins/ExtractAttachments.java new file mode 100644 index 0000000..455f3a2 --- /dev/null +++ b/src/main/java/com/lowagie/tools/plugins/ExtractAttachments.java @@ -0,0 +1,227 @@ +/* + * $Id: ExtractAttachments.java,v 1.5 2006/03/01 11:41:16 blowagie Exp $ + * $Name: $ + * + * Copyright 2005 by Paulo Soares and Anonymous. + * + * The contents of this file are subject to the Mozilla Public License Version 1.1 + * (the "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the License. + * + * The Original Code is 'iText, a free JAVA-PDF library'. + * + * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by + * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie. + * All Rights Reserved. + * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer + * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved. + * + * Contributor(s): all the names of the contributors are added in the source code + * where applicable. + * + * Alternatively, the contents of this file may be used under the terms of the + * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the + * provisions of LGPL are applicable instead of those above. If you wish to + * allow use of your version of this file only under the terms of the LGPL + * License and not to allow others to use your version of this file under + * the MPL, indicate your decision by deleting the provisions above and + * replace them with the notice and other provisions required by the LGPL. + * If you do not delete the provisions above, a recipient may use your version + * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE. + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the MPL as stated above or under the terms of the GNU + * Library General Public License as published by the Free Software Foundation; + * either version 2 of the License, or any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more + * details. + * + * If you didn't download this code from the following link, you should check if + * you aren't using an obsolete version: + * http://www.lowagie.com/iText/ + */ +package com.lowagie.tools.plugins; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.HashMap; +import java.util.Iterator; + +import javax.swing.JInternalFrame; + +import com.lowagie.text.pdf.*; +import com.lowagie.tools.arguments.FileArgument; +import com.lowagie.tools.arguments.LabelAccessory; +import com.lowagie.tools.arguments.PdfFilter; +import com.lowagie.tools.arguments.ToolArgument; + +/** + * This tool lets you extract the attachements of a PDF. + */ +public class ExtractAttachments extends AbstractTool { + + static { + addVersion("$Id: ExtractAttachments.java,v 1.5 2006/03/01 11:41:16 blowagie Exp $"); + } + + /** + * Constructs a ExtractAttachements object. + */ + public ExtractAttachments() { + FileArgument f = new FileArgument(this, "srcfile", + "The file you want to operate on", false, new PdfFilter()); + f.setLabel(new LabelAccessory()); + arguments.add(f); + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#createFrame() + */ + protected void createFrame() { + internalFrame = new JInternalFrame("ExtractAttachments", true, false, + true); + internalFrame.setSize(300, 80); + internalFrame.setJMenuBar(getMenubar()); + System.out.println("=== ExtractAttachments OPENED ==="); + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#execute() + */ + public void execute() { + try { + if (getValue("srcfile") == null) + throw new InstantiationException( + "You need to choose a sourcefile"); + File src = (File) getValue("srcfile"); + + // we create a reader for a certain document + PdfReader reader = new PdfReader(src.getAbsolutePath()); + final File parentFile = src.getParentFile(); + final String outPath; + if (parentFile != null) { + outPath = parentFile.getAbsolutePath(); + } else { + outPath = ""; + } + PdfDictionary catalog = reader.getCatalog(); + PdfDictionary names = (PdfDictionary) PdfReader + .getPdfObject(catalog.get(PdfName.NAMES)); + if (names != null) { + PdfDictionary embFiles = (PdfDictionary) PdfReader + .getPdfObject(names.get(new PdfName("EmbeddedFiles"))); + if (embFiles != null) { + HashMap embMap = PdfNameTree.readTree(embFiles); + for (Iterator i = embMap.values().iterator(); i.hasNext();) { + PdfDictionary filespec = (PdfDictionary) PdfReader + .getPdfObject((PdfObject) i.next()); + unpackFile(reader, filespec, outPath); + } + } + } + for (int k = 1; k <= reader.getNumberOfPages(); ++k) { + PdfArray annots = (PdfArray) PdfReader.getPdfObject(reader + .getPageN(k).get(PdfName.ANNOTS)); + if (annots == null) + continue; + for (Iterator i = annots.listIterator(); i.hasNext();) { + PdfDictionary annot = (PdfDictionary) PdfReader + .getPdfObject((PdfObject) i.next()); + PdfName subType = (PdfName) PdfReader.getPdfObject(annot + .get(PdfName.SUBTYPE)); + if (!PdfName.FILEATTACHMENT.equals(subType)) + continue; + PdfDictionary filespec = (PdfDictionary) PdfReader + .getPdfObject(annot.get(PdfName.FS)); + unpackFile(reader, filespec, outPath); + } + } + + } catch (Exception e) { + e.printStackTrace(); + } + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#valueHasChanged(com.lowagie.tools.arguments.ToolArgument) + */ + public void valueHasChanged(ToolArgument arg) { + if (internalFrame == null) { + // if the internal frame is null, the tool was called from the + // commandline + return; + } + // represent the changes of the argument in the internal frame + } + + /** + * Extract the attachements of a PDF. + * + * @param args + */ + public static void main(String[] args) { + ExtractAttachments tool = new ExtractAttachments(); + if (args.length < 1) { + System.err.println(tool.getUsage()); + } + tool.setArguments(args); + tool.execute(); + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#getDestPathPDF() + */ + protected File getDestPathPDF() throws InstantiationException { + throw new InstantiationException("There is more than one destfile."); + } + + /** + * Unpacks a file attachment. + * + * @param reader + * The object that reads the PDF document + * @param filespec + * The dictonary containing the file specifications + * @param outPath + * The path where the attachment has to be written + * @throws IOException + */ + public static void unpackFile(PdfReader reader, PdfDictionary filespec, + String outPath) throws IOException { + if (filespec == null) + return; + PdfName type = (PdfName) PdfReader.getPdfObject(filespec + .get(PdfName.TYPE)); + if (!PdfName.F.equals(type) && !PdfName.FILESPEC.equals(type)) + return; + PdfDictionary ef = (PdfDictionary) PdfReader.getPdfObject(filespec + .get(PdfName.EF)); + if (ef == null) + return; + PdfString fn = (PdfString) PdfReader.getPdfObject(filespec + .get(PdfName.F)); + System.out.println("Unpacking file '" + fn + "' to " + outPath); + if (fn == null) + return; + File fLast = new File(fn.toUnicodeString()); + File fullPath = new File(outPath, fLast.getName()); + if (fullPath.exists()) + return; + PRStream prs = (PRStream) PdfReader.getPdfObject(ef.get(PdfName.F)); + if (prs == null) + return; + byte b[] = PdfReader.getStreamBytes(prs); + FileOutputStream fout = new FileOutputStream(fullPath); + fout.write(b); + fout.close(); + } + +} \ No newline at end of file diff --git a/src/main/java/com/lowagie/tools/plugins/Handouts.java b/src/main/java/com/lowagie/tools/plugins/Handouts.java new file mode 100644 index 0000000..fed8788 --- /dev/null +++ b/src/main/java/com/lowagie/tools/plugins/Handouts.java @@ -0,0 +1,232 @@ +/* + * $Id: Handouts.java,v 1.7 2005/11/29 21:05:02 blowagie Exp $ + * $Name: $ + * + * Copyright 2005 by Bruno Lowagie. + * + * The contents of this file are subject to the Mozilla Public License Version 1.1 + * (the "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the License. + * + * The Original Code is 'iText, a free JAVA-PDF library'. + * + * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by + * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie. + * All Rights Reserved. + * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer + * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved. + * + * Contributor(s): all the names of the contributors are added in the source code + * where applicable. + * + * Alternatively, the contents of this file may be used under the terms of the + * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the + * provisions of LGPL are applicable instead of those above. If you wish to + * allow use of your version of this file only under the terms of the LGPL + * License and not to allow others to use your version of this file under + * the MPL, indicate your decision by deleting the provisions above and + * replace them with the notice and other provisions required by the LGPL. + * If you do not delete the provisions above, a recipient may use your version + * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE. + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the MPL as stated above or under the terms of the GNU + * Library General Public License as published by the Free Software Foundation; + * either version 2 of the License, or any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more + * details. + * + * If you didn't download this code from the following link, you should check if + * you aren't using an obsolete version: + * http://www.lowagie.com/iText/ + */ +package com.lowagie.tools.plugins; + +import java.io.File; +import java.io.FileOutputStream; + +import javax.swing.JInternalFrame; +import javax.swing.JOptionPane; + +import com.lowagie.text.Document; +import com.lowagie.text.PageSize; +import com.lowagie.text.Rectangle; +import com.lowagie.text.pdf.PdfContentByte; +import com.lowagie.text.pdf.PdfImportedPage; +import com.lowagie.text.pdf.PdfReader; +import com.lowagie.text.pdf.PdfWriter; +import com.lowagie.tools.arguments.FileArgument; +import com.lowagie.tools.arguments.OptionArgument; +import com.lowagie.tools.arguments.PdfFilter; +import com.lowagie.tools.arguments.ToolArgument; + +/** + * Generates a PDF file that is useable as Handout. + */ +public class Handouts extends AbstractTool { + + static { + addVersion("$Id: Handouts.java,v 1.7 2005/11/29 21:05:02 blowagie Exp $"); + } + + /** + * Constructs a Handouts object. + */ + public Handouts() { + arguments.add(new FileArgument(this, "srcfile", "The file you want to convert", false, new PdfFilter())); + arguments.add(new FileArgument(this, "destfile", "The file to which the Handout has to be written", true, new PdfFilter())); + OptionArgument oa = new OptionArgument(this, "pages", "The number of pages you want on one handout page"); + oa.addOption("2 pages on 1", "2"); + oa.addOption("3 pages on 1", "3"); + oa.addOption("4 pages on 1", "4"); + oa.addOption("5 pages on 1", "5"); + oa.addOption("6 pages on 1", "6"); + oa.addOption("7 pages on 1", "7"); + oa.addOption("8 pages on 1", "8"); + arguments.add(oa); + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#createFrame() + */ + protected void createFrame() { + internalFrame = new JInternalFrame("Handouts", true, false, true); + internalFrame.setSize(300, 80); + internalFrame.setJMenuBar(getMenubar()); + System.out.println("=== Handouts OPENED ==="); + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#execute() + */ + public void execute() { + try { + if (getValue("srcfile") == null) throw new InstantiationException("You need to choose a sourcefile"); + File src = (File)getValue("srcfile"); + if (getValue("destfile") == null) throw new InstantiationException("You need to choose a destination file"); + File dest = (File)getValue("destfile"); + int pages; + try { + pages = Integer.parseInt((String) getValue("pages")); + } + catch(Exception e) { + pages = 4; + } + + float x1 = 30f; + float x2 = 280f; + float x3 = 320f; + float x4 = 565f; + + float[] y1 = new float[pages]; + float[] y2 = new float[pages]; + + float height = (778f - (20f * (pages - 1))) / pages; + y1[0] = 812f; + y2[0] = 812f - height; + + for (int i = 1; i < pages; i++) { + y1[i] = y2[i - 1] - 20f; + y2[i] = y1[i] - height; + } + + // we create a reader for a certain document + PdfReader reader = new PdfReader(src.getAbsolutePath()); + // we retrieve the total number of pages + int n = reader.getNumberOfPages(); + System.out.println("There are " + n + " pages in the original file."); + + // step 1: creation of a document-object + Document document = new Document(PageSize.A4); + // step 2: we create a writer that listens to the document + PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest)); + // step 3: we open the document + document.open(); + PdfContentByte cb = writer.getDirectContent(); + PdfImportedPage page; + int rotation; + int i = 0; + int p = 0; + // step 4: we add content + while (i < n) { + i++; + Rectangle rect = reader.getPageSizeWithRotation(i); + float factorx = (x2 - x1) / rect.width(); + float factory = (y1[p] - y2[p]) / rect.height(); + float factor = (factorx < factory ? factorx : factory); + float dx = (factorx == factor ? 0f : ((x2 - x1) - rect.width() * factor) / 2f); + float dy = (factory == factor ? 0f : ((y1[p] - y2[p]) - rect.height() * factor) / 2f); + page = writer.getImportedPage(reader, i); + rotation = reader.getPageRotation(i); + if (rotation == 90 || rotation == 270) { + cb.addTemplate(page, 0, -factor, factor, 0, x1 + dx, y2[p] + dy + rect.height() * factor); + } + else { + cb.addTemplate(page, factor, 0, 0, factor, x1 + dx, y2[p] + dy); + } + cb.setRGBColorStroke(0xC0, 0xC0, 0xC0); + cb.rectangle(x3 - 5f, y2[p] - 5f, x4 - x3 + 10f, y1[p] - y2[p] + 10f); + for (float l = y1[p] - 19; l > y2[p]; l -= 16) { + cb.moveTo(x3, l); + cb.lineTo(x4, l); + } + cb.rectangle(x1 + dx, y2[p] + dy, rect.width() * factor, rect.height() * factor); + cb.stroke(); + System.out.println("Processed page " + i); + p++; + if (p == pages) { + p = 0; + document.newPage(); + } + } + // step 5: we close the document + document.close(); + } + catch(Exception e) { + JOptionPane.showMessageDialog(internalFrame, + e.getMessage(), + e.getClass().getName(), + JOptionPane.ERROR_MESSAGE); + System.err.println(e.getMessage()); + } + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#valueHasChanged(com.lowagie.tools.arguments.ToolArgument) + */ + public void valueHasChanged(ToolArgument arg) { + if (internalFrame == null) { + // if the internal frame is null, the tool was called from the commandline + return; + } + // represent the changes of the argument in the internal frame + } + + + /** + * Converts a PDF file to a PDF file useable as Handout. + * @param args + */ + public static void main(String[] args) { + Handouts tool = new Handouts(); + if (args.length < 2) { + System.err.println(tool.getUsage()); + } + tool.setArguments(args); + tool.execute(); + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#getDestPathPDF() + */ + protected File getDestPathPDF() throws InstantiationException { + return (File)getValue("destfile"); + } +} diff --git a/src/main/java/com/lowagie/tools/plugins/HtmlBookmarks.java b/src/main/java/com/lowagie/tools/plugins/HtmlBookmarks.java new file mode 100644 index 0000000..aa8966f --- /dev/null +++ b/src/main/java/com/lowagie/tools/plugins/HtmlBookmarks.java @@ -0,0 +1,288 @@ +/* + * $Id: HtmlBookmarks.java,v 1.12 2005/11/29 21:05:02 blowagie Exp $ + * $Name: $ + * + * Copyright 2005 by Bruno Lowagie. + * + * The contents of this file are subject to the Mozilla Public License Version 1.1 + * (the "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the License. + * + * The Original Code is 'iText, a free JAVA-PDF library'. + * + * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by + * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie. + * All Rights Reserved. + * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer + * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved. + * + * Contributor(s): all the names of the contributors are added in the source code + * where applicable. + * + * Alternatively, the contents of this file may be used under the terms of the + * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the + * provisions of LGPL are applicable instead of those above. If you wish to + * allow use of your version of this file only under the terms of the LGPL + * License and not to allow others to use your version of this file under + * the MPL, indicate your decision by deleting the provisions above and + * replace them with the notice and other provisions required by the LGPL. + * If you do not delete the provisions above, a recipient may use your version + * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE. + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the MPL as stated above or under the terms of the GNU + * Library General Public License as published by the Free Software Foundation; + * either version 2 of the License, or any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more + * details. + * + * If you didn't download this code from the following link, you should check if + * you aren't using an obsolete version: + * http://www.lowagie.com/iText/ + */ +package com.lowagie.tools.plugins; + +import java.io.File; +import java.io.FileOutputStream; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.StringTokenizer; + +import javax.swing.JInternalFrame; +import javax.swing.JOptionPane; + +import com.lowagie.text.Anchor; +import com.lowagie.text.Chapter; +import com.lowagie.text.Chunk; +import com.lowagie.text.Document; +import com.lowagie.text.Header; +import com.lowagie.text.Paragraph; +import com.lowagie.text.Section; +import com.lowagie.text.html.HtmlTags; +import com.lowagie.text.html.HtmlWriter; +import com.lowagie.text.markup.MarkupTags; +import com.lowagie.text.pdf.PdfReader; +import com.lowagie.text.pdf.SimpleBookmark; +import com.lowagie.tools.Executable; +import com.lowagie.tools.arguments.FileArgument; +import com.lowagie.tools.arguments.PdfFilter; +import com.lowagie.tools.arguments.ToolArgument; + +/** + * Allows you to generate an index file in HTML containing Bookmarks to an existing PDF file. + */ +public class HtmlBookmarks extends AbstractTool { + + static { + addVersion("$Id: HtmlBookmarks.java,v 1.12 2005/11/29 21:05:02 blowagie Exp $"); + } + + /** + * Constructs an HtmlBookmarks object. + */ + public HtmlBookmarks() { + arguments.add(new FileArgument(this, "srcfile", "The file you want to inspect", false, new PdfFilter())); + arguments.add(new ToolArgument(this, "ownerpassword", "The owner password if the file is encrypt", String.class.getName())); + arguments.add(new ToolArgument(this, "css", "The path to a CSS file", String.class.getName())); + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#createFrame() + */ + protected void createFrame() { + internalFrame = new JInternalFrame("Html Bookmarks", true, true, true); + internalFrame.setSize(300, 80); + internalFrame.setJMenuBar(getMenubar()); + System.out.println("=== Html Bookmarks OPENED ==="); + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#execute() + */ + public void execute() { + try { + if (getValue("srcfile") == null) throw new InstantiationException("You need to choose a sourcefile"); + File src = (File)getValue("srcfile"); + PdfReader reader; + if (getValue("ownerpassword") == null) { + reader = new PdfReader(src.getAbsolutePath()); + } + else { + reader = new PdfReader(src.getAbsolutePath(), ((String)getValue("ownerpassword")).getBytes()); + } + File directory = src.getParentFile(); + String name = src.getName(); + name = name.substring(0, name.lastIndexOf(".")); + File html = new File(directory, name + "_index.html"); + Document document = new Document(); + HtmlWriter.getInstance(document, new FileOutputStream(html)); + Object css = getValue("css"); + if (css != null) { + document.add(new Header(HtmlTags.STYLESHEET, css.toString())); + } + Object title = reader.getInfo().get("Title"); + if (title == null) + document.addTitle("Index for " + src.getName()); + else + document.addKeywords("Index for '" + title + "'"); + Object keywords = reader.getInfo().get("Keywords"); + if (keywords != null) + document.addKeywords((String)keywords); + Object description = reader.getInfo().get("Subject"); + if (keywords != null) + document.addSubject((String)description); + document.open(); + Paragraph t; + if (title == null) + t = new Paragraph("Index for " + src.getName()); + else + t = new Paragraph("Index for '" + title + "'"); + t.setMarkupAttribute(MarkupTags.HTML_ATTR_CSS_CLASS, "title"); + document.add(t); + if (description != null) { + Paragraph d = new Paragraph((String) description); + d.setMarkupAttribute(MarkupTags.HTML_ATTR_CSS_CLASS, "description"); + document.add(d); + } + List list = SimpleBookmark.getBookmark(reader); + if (list == null) { + document.add(new Paragraph("This document has no bookmarks.")); + } + else { + HashMap c; + for (Iterator i = list.iterator(); i.hasNext(); ) { + c = (HashMap) i.next(); + Chapter chapter = (Chapter)createBookmark(src.getName(), null, c); + ArrayList kids = (ArrayList) c.get("Kids"); + if (kids != null) { + for (Iterator k = kids.iterator(); k.hasNext(); ) { + addBookmark(src.getName(), chapter, (HashMap)k.next()); + } + } + document.add(chapter); + } + } + document.close(); + Executable.launchBrowser(html.getAbsolutePath()); + } + catch(Exception e) { + e.printStackTrace(); + JOptionPane.showMessageDialog(internalFrame, + e.getMessage(), + e.getClass().getName(), + JOptionPane.ERROR_MESSAGE); + System.err.println(e.getMessage()); + } + } + + /** + * Recursive method to write Bookmark titles to the System.out. + * @param pdf the path to the PDF file + * @param section the section to which the bookmarks should be added + * @param bookmark a HashMap containing a Bookmark (and possible kids) + */ + private static void addBookmark(String pdf, Section section, HashMap bookmark) { + Section s = createBookmark(pdf, section, bookmark); + ArrayList kids = (ArrayList) bookmark.get("Kids"); + if (kids == null) return; + for (Iterator i = kids.iterator(); i.hasNext(); ) { + addBookmark(pdf, s, (HashMap)i.next()); + } + } + + /** + * Adds a line with the title and an anchor. + * @param pdf the link to the PDF file + * @param section the section that gets the line + * @param bookmark the bookmark that has the data for the line + * @return a subsection of section + */ + private static Section createBookmark(String pdf, Section section, HashMap bookmark) { + Section s; + Paragraph title = new Paragraph((String)bookmark.get("Title")); + System.out.println((String)bookmark.get("Title")); + String action = (String)bookmark.get("Action"); + if ("GoTo".equals(action)) { + if (bookmark.get("Page") != null) { + String page = (String)bookmark.get("Page"); + StringTokenizer tokens = new StringTokenizer(page); + String token = tokens.nextToken(); + Anchor anchor = new Anchor(" page" + token); + anchor.setReference(pdf + "#page=" + token); + title.add(anchor); + } + } + else if ("URI".equals(action)) { + String url = (String)bookmark.get("URI"); + Anchor anchor = new Anchor(" Goto URL"); + anchor.setReference(url); + title.add(anchor); + } + else if ("GoToR".equals(action)) { + String remote = (String)bookmark.get("File"); + Anchor anchor = new Anchor(" goto " + remote); + if (bookmark.get("Named") != null) { + String named = (String)bookmark.get("Named"); + remote = remote + "#nameddest=" + named; + } + else if (bookmark.get("Page") != null) { + String page = (String)bookmark.get("Page"); + StringTokenizer tokens = new StringTokenizer(page); + String token = tokens.nextToken(); + anchor.add(new Chunk(" page " + token)); + remote = remote + "#page=" + token; + } + anchor.setReference(remote); + title.add(anchor); + } + if (section == null) { + s = new Chapter(title, 0); + } + else { + s = section.addSection(title); + } + s.setNumberDepth(0); + return s; + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#valueHasChanged(com.lowagie.tools.arguments.ToolArgument) + */ + public void valueHasChanged(ToolArgument arg) { + if (internalFrame == null) { + // if the internal frame is null, the tool was called from the commandline + return; + } + // represent the changes of the argument in the internal frame + } + + /** + * Allows you to generate an index file in HTML containing Bookmarks to an existing PDF file. + * @param args + */ + public static void main(String[] args) { + HtmlBookmarks tool = new HtmlBookmarks(); + if (args.length < 1) { + System.err.println(tool.getUsage()); + } + tool.setArguments(args); + tool.execute(); + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#getDestPathPDF() + */ + protected File getDestPathPDF() throws InstantiationException { + throw new InstantiationException("There is no file to show."); + } + +} diff --git a/src/main/java/com/lowagie/tools/plugins/ImageXRefViewer.java b/src/main/java/com/lowagie/tools/plugins/ImageXRefViewer.java new file mode 100644 index 0000000..34bbc50 --- /dev/null +++ b/src/main/java/com/lowagie/tools/plugins/ImageXRefViewer.java @@ -0,0 +1,256 @@ +/* + * $Id: ImageXRefViewer.java,v 1.3 2006/04/05 07:55:00 blowagie Exp $ + * $Name: $ + * + * Copyright 2005 by Carsten Hammer and Bruno Lowagie. + * + * The contents of this file are subject to the Mozilla Public License Version 1.1 + * (the "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the License. + * + * The Original Code is 'iText, a free JAVA-PDF library'. + * + * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by + * the Initial Developer are Copyright (C) 1999-2006 by Bruno Lowagie. + * All Rights Reserved. + * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer + * are Copyright (C) 2000-2006 by Paulo Soares. All Rights Reserved. + * + * Contributor(s): all the names of the contributors are added in the source code + * where applicable. + * + * Alternatively, the contents of this file may be used under the terms of the + * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the + * provisions of LGPL are applicable instead of those above. If you wish to + * allow use of your version of this file only under the terms of the LGPL + * License and not to allow others to use your version of this file under + * the MPL, indicate your decision by deleting the provisions above and + * replace them with the notice and other provisions required by the LGPL. + * If you do not delete the provisions above, a recipient may use your version + * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE. + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the MPL as stated above or under the terms of the GNU + * Library General Public License as published by the Free Software Foundation; + * either version 2 of the License, or any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more + * details. + * + * If you didn't download this code from the following link, you should check if + * you aren't using an obsolete version: + * http://www.lowagie.com/iText/ + */ +package com.lowagie.tools.plugins; + +import java.awt.BorderLayout; +import java.awt.CardLayout; +import java.awt.Toolkit; +import java.io.File; + +import javax.swing.BorderFactory; +import javax.swing.JInternalFrame; +import javax.swing.JLabel; +import javax.swing.JOptionPane; +import javax.swing.JPanel; +import javax.swing.JSpinner; +import javax.swing.SpinnerModel; +import javax.swing.SwingConstants; +import javax.swing.event.ChangeEvent; + +import com.lowagie.text.pdf.PRStream; +import com.lowagie.text.pdf.PdfName; +import com.lowagie.text.pdf.PdfObject; +import com.lowagie.text.pdf.PdfReader; +import com.lowagie.text.pdf.PdfStream; +import com.lowagie.tools.arguments.FileArgument; +import com.lowagie.tools.arguments.PdfFilter; +import com.lowagie.tools.arguments.ToolArgument; +import javax.swing.JScrollPane; +import com.lowagie.tools.SwingWorker; +import java.io.*; +import java.awt.Cursor; + +/** + * Allows you to inspect the Image XObjects inside a PDF file. + */ +public class ImageXRefViewer + extends AbstractTool { + static { + addVersion("$Id: ImageXRefViewer.java,v 1.3 2006/04/05 07:55:00 blowagie Exp $"); + } + + class ViewXRefImages_jSpinner1_propertyChangeAdapter + implements javax.swing.event.ChangeListener { + private ImageXRefViewer adaptee; + ViewXRefImages_jSpinner1_propertyChangeAdapter(ImageXRefViewer adaptee) { + this.adaptee = adaptee; + } + /** + * @see javax.swing.event.ChangeListener#stateChanged(javax.swing.event.ChangeEvent) + */ + public void stateChanged(ChangeEvent e) { + adaptee.jSpinner_propertyChange(e); + } + } + + JPanel jPanel1 = new JPanel(); + BorderLayout borderLayout1 = new BorderLayout(); + JLabel jLabel1 = new JLabel(); + int picturenumber = 0; + JPanel jPanel2 = new JPanel(); + BorderLayout borderLayout2 = new BorderLayout(); + CardLayout cardLayout1 = new CardLayout(); + JPanel jPanel3 = new JPanel(); + JSpinner jSpinner1 = new JSpinner(); + BorderLayout borderLayout3 = new BorderLayout(); + SpinnerModel spinnerModel1 = jSpinner1.getModel(); + JScrollPane jScrollPane1 = new JScrollPane(); + + + /** + * Creates a ViewImageXObjects object. + */ + public ImageXRefViewer() { + arguments.add(new FileArgument(this, "srcfile", "The file you want to inspect", false, new PdfFilter())); + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#getDestPathPDF() + */ + protected File getDestPathPDF() throws InstantiationException { + throw new InstantiationException("There is no file to show."); + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#createFrame() + */ + protected void createFrame() { + internalFrame = new JInternalFrame("View Image XObjects", true, false, true); + internalFrame.setSize(500, 300); + internalFrame.setJMenuBar(getMenubar()); + internalFrame.getContentPane().setLayout(borderLayout1); + jPanel1.setLayout(borderLayout2); + jLabel1.setHorizontalAlignment(SwingConstants.CENTER); + jLabel1.setText("images"); + jPanel2.setLayout(cardLayout1); + jPanel3.setLayout(borderLayout3); + jSpinner1.addChangeListener(new + ViewXRefImages_jSpinner1_propertyChangeAdapter(this)); + jPanel2.setBorder(BorderFactory.createEtchedBorder()); + internalFrame.getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER); + jPanel3.add(jSpinner1, java.awt.BorderLayout.CENTER); + jPanel3.add(jLabel1, java.awt.BorderLayout.NORTH); + jPanel1.add(jPanel3, java.awt.BorderLayout.NORTH); + jPanel1.add(jScrollPane1, java.awt.BorderLayout.CENTER); + jScrollPane1.setViewportView(jPanel2); + System.out.println("=== Image XObject Viewer OPENED ==="); + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#valueHasChanged(com.lowagie.tools.arguments.ToolArgument) + */ + public void valueHasChanged(ToolArgument arg) { + // do nothing + } + + /** + * Reflects the change event in the JSpinner object. + * @param evt + */ + public void jSpinner_propertyChange(ChangeEvent evt) { + int blatt = Integer.parseInt(jSpinner1.getValue().toString()); + if (blatt < 0) blatt = 0; + if (blatt >= picturenumber) blatt = picturenumber - 1; + this.cardLayout1.show(jPanel2, String.valueOf(blatt)); + jPanel2.repaint(); + } + /** + * Shows the images that are added to the PDF as Image XObjects. + * @param args + */ + public static void main(String[] args) { + InspectPDF tool = new InspectPDF(); + if (args.length < 1) { + System.err.println(tool.getUsage()); + } + tool.setArguments(args); + tool.execute(); + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#execute() + */ + public void execute() { + picturenumber = 0; + try { + if (getValue("srcfile") == null) throw new InstantiationException("You need to choose a sourcefile"); + + SwingWorker work= new SwingWorker(){ + + public Object construct() { + try { + PdfReader reader = new PdfReader( ( (File) + getValue("srcfile")).getAbsolutePath()); + for (int i = 0; i < reader.getXrefSize(); i++) { + PdfObject pdfobj = reader.getPdfObject(i); + if (pdfobj != null) { + if (pdfobj.isStream()) { + PdfStream pdfdict = (PdfStream) pdfobj; + PdfObject pdfsubtype = pdfdict.get(PdfName. + SUBTYPE); + if (pdfsubtype == null) { + continue; + } + if (!pdfsubtype.toString().equals(PdfName. + IMAGE.toString())) { + continue; + } + System.out.println("picturenumber: " + + picturenumber); + System.out.println("height:" + + pdfdict.get(PdfName.HEIGHT)); + System.out.println("width:" + + pdfdict.get(PdfName.WIDTH)); + System.out.println("bitspercomponent:" + + pdfdict.get(PdfName.BITSPERCOMPONENT)); + byte[] barr = PdfReader.getStreamBytesRaw( ( + PRStream) pdfdict); + java.awt.Image im = Toolkit. + getDefaultToolkit().createImage(barr); + javax.swing.ImageIcon ii = new javax.swing. + ImageIcon(im); + + JLabel jLabel1 = new JLabel(); + jLabel1.setIcon(ii); + jPanel2.add(jLabel1, "" + picturenumber++); + } + } + } + } + catch (InstantiationException ex) { + } + catch (IOException ex) { + } + internalFrame.setCursor(Cursor.getDefaultCursor()); + return null; + } + }; + internalFrame.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); + work.start(); + } + catch(Exception e) { + JOptionPane.showMessageDialog(internalFrame, + e.getMessage(), + e.getClass().getName(), + JOptionPane.ERROR_MESSAGE); + System.err.println(e.getMessage()); + } + } +} diff --git a/src/main/java/com/lowagie/tools/plugins/InspectPDF.java b/src/main/java/com/lowagie/tools/plugins/InspectPDF.java new file mode 100644 index 0000000..b017221 --- /dev/null +++ b/src/main/java/com/lowagie/tools/plugins/InspectPDF.java @@ -0,0 +1,173 @@ +/* + * $Id: InspectPDF.java,v 1.6 2005/11/29 21:05:02 blowagie Exp $ + * $Name: $ + * + * Copyright 2005 by Bruno Lowagie. + * + * The contents of this file are subject to the Mozilla Public License Version 1.1 + * (the "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the License. + * + * The Original Code is 'iText, a free JAVA-PDF library'. + * + * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by + * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie. + * All Rights Reserved. + * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer + * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved. + * + * Contributor(s): all the names of the contributors are added in the source code + * where applicable. + * + * Alternatively, the contents of this file may be used under the terms of the + * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the + * provisions of LGPL are applicable instead of those above. If you wish to + * allow use of your version of this file only under the terms of the LGPL + * License and not to allow others to use your version of this file under + * the MPL, indicate your decision by deleting the provisions above and + * replace them with the notice and other provisions required by the LGPL. + * If you do not delete the provisions above, a recipient may use your version + * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE. + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the MPL as stated above or under the terms of the GNU + * Library General Public License as published by the Free Software Foundation; + * either version 2 of the License, or any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more + * details. + * + * If you didn't download this code from the following link, you should check if + * you aren't using an obsolete version: + * http://www.lowagie.com/iText/ + */ +package com.lowagie.tools.plugins; + +import java.io.File; +import java.util.HashMap; +import java.util.Iterator; + +import javax.swing.JInternalFrame; +import javax.swing.JOptionPane; + +import com.lowagie.text.pdf.PdfEncryptor; +import com.lowagie.text.pdf.PdfReader; +import com.lowagie.tools.arguments.FileArgument; +import com.lowagie.tools.arguments.PdfFilter; +import com.lowagie.tools.arguments.ToolArgument; + +/** + * Allows you to inspect an existing PDF file. + */ +public class InspectPDF extends AbstractTool { + static { + addVersion("$Id: InspectPDF.java,v 1.6 2005/11/29 21:05:02 blowagie Exp $"); + } + + /** + * Constructs an InpectPDF object. + */ + public InspectPDF() { + arguments.add(new FileArgument(this, "srcfile", "The file you want to inspect", false, new PdfFilter())); + arguments.add(new ToolArgument(this, "ownerpassword", "The owner password if the file is encrypt", String.class.getName())); + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#createFrame() + */ + protected void createFrame() { + internalFrame = new JInternalFrame("Pdf Information", true, false, true); + internalFrame.setSize(300, 80); + internalFrame.setJMenuBar(getMenubar()); + System.out.println("=== Pdf Information OPENED ==="); + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#execute() + */ + public void execute() { + try { + if (getValue("srcfile") == null) throw new InstantiationException("You need to choose a sourcefile"); + PdfReader reader; + if (getValue("ownerpassword") == null) { + reader = new PdfReader(((File)getValue("srcfile")).getAbsolutePath()); + } + else { + reader = new PdfReader(((File)getValue("srcfile")).getAbsolutePath(), ((String)getValue("ownerpassword")).getBytes()); + } + // Some general document information and page size + System.out.println("=== Document Information ==="); + System.out.println("PDF Version: " + reader.getPdfVersion()); + System.out.println("Number of pages: " + reader.getNumberOfPages()); + System.out.println("Number of PDF objects: " + reader.getXrefSize()); + System.out.println("File length: " + reader.getFileLength()); + System.out.println("Encrypted? " + reader.isEncrypted()); + if (reader.isEncrypted()) { + System.out.println("Permissions: " + PdfEncryptor.getPermissionsVerbose(reader.getPermissions())); + System.out.println("128 bit? " + reader.is128Key()); + } + System.out.println("Rebuilt? " + (!reader.isRebuilt())); + // Some metadata + System.out.println("=== Metadata ==="); + HashMap info = reader.getInfo(); + String key; + String value; + for (Iterator i = info.keySet().iterator(); i.hasNext(); ) { + key = (String) i.next(); + value = (String) info.get(key); + System.out.println(key + ": " + value); + } + if (reader.getMetadata() == null) { + System.out.println("There is no XML Metadata in the file"); + } + else { + System.out.println("XML Metadata: " + new String(reader.getMetadata())); + } + } + catch(Exception e) { + JOptionPane.showMessageDialog(internalFrame, + e.getMessage(), + e.getClass().getName(), + JOptionPane.ERROR_MESSAGE); + System.err.println(e.getMessage()); + } + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#valueHasChanged(com.lowagie.tools.arguments.ToolArgument) + */ + public void valueHasChanged(ToolArgument arg) { + if (internalFrame == null) { + // if the internal frame is null, the tool was called from the commandline + return; + } + // represent the changes of the argument in the internal frame + } + + /** + * Inspects an existing PDF file. + * @param args + */ + public static void main(String[] args) { + InspectPDF tool = new InspectPDF(); + if (args.length < 1) { + System.err.println(tool.getUsage()); + } + tool.setArguments(args); + tool.execute(); + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#getDestPathPDF() + */ + protected File getDestPathPDF() throws InstantiationException { + throw new InstantiationException("There is no file to show."); + } + +} diff --git a/src/main/java/com/lowagie/tools/plugins/KnitTiff.java b/src/main/java/com/lowagie/tools/plugins/KnitTiff.java new file mode 100644 index 0000000..428bbb7 --- /dev/null +++ b/src/main/java/com/lowagie/tools/plugins/KnitTiff.java @@ -0,0 +1,178 @@ +/* + * $Id: KnitTiff.java,v 1.7 2005/11/29 21:05:02 blowagie Exp $ + * $Name: $ + * + * Copyright 2005 by Bruno Lowagie. + * + * The contents of this file are subject to the Mozilla Public License Version 1.1 + * (the "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the License. + * + * The Original Code is 'iText, a free JAVA-PDF library'. + * + * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by + * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie. + * All Rights Reserved. + * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer + * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved. + * + * Contributor(s): all the names of the contributors are added in the source code + * where applicable. + * + * Alternatively, the contents of this file may be used under the terms of the + * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the + * provisions of LGPL are applicable instead of those above. If you wish to + * allow use of your version of this file only under the terms of the LGPL + * License and not to allow others to use your version of this file under + * the MPL, indicate your decision by deleting the provisions above and + * replace them with the notice and other provisions required by the LGPL. + * If you do not delete the provisions above, a recipient may use your version + * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE. + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the MPL as stated above or under the terms of the GNU + * Library General Public License as published by the Free Software Foundation; + * either version 2 of the License, or any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more + * details. + * + * If you didn't download this code from the following link, you should check if + * you aren't using an obsolete version: + * http://www.lowagie.com/iText/ + */ +package com.lowagie.tools.plugins; + +import java.io.File; +import java.io.FileOutputStream; + +import javax.swing.JInternalFrame; + +import com.lowagie.text.Document; +import com.lowagie.text.Image; +import com.lowagie.text.Rectangle; +import com.lowagie.text.pdf.PdfContentByte; +import com.lowagie.text.pdf.PdfWriter; +import com.lowagie.text.pdf.RandomAccessFileOrArray; +import com.lowagie.text.pdf.codec.TiffImage; +import com.lowagie.tools.arguments.FileArgument; +import com.lowagie.tools.arguments.ImageFilter; +import com.lowagie.tools.arguments.PdfFilter; +import com.lowagie.tools.arguments.ToolArgument; + +/** + * Knits two TIFF files, one with the even pages and another with the odd pages, together. + */ +public class KnitTiff extends AbstractTool { + + static { + addVersion("$Id: KnitTiff.java,v 1.7 2005/11/29 21:05:02 blowagie Exp $"); + } + /** + * Constructs a KnitTiff object. + */ + public KnitTiff() { + menuoptions = MENU_EXECUTE | MENU_EXECUTE_SHOW; + arguments.add(new FileArgument(this, "odd", "The tiff file with the odd pages", false, new ImageFilter(false, false, false, false, false, true))); + arguments.add(new FileArgument(this, "even", "The tiff file with the even pages", false, new ImageFilter(false, false, false, false, false, true))); + arguments.add(new FileArgument(this, "destfile", "The file to which the converted TIFF has to be written", true, new PdfFilter())); + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#createFrame() + */ + protected void createFrame() { + internalFrame = new JInternalFrame("KnitTiff", true, false, true); + internalFrame.setSize(300, 80); + internalFrame.setJMenuBar(getMenubar()); + System.out.println("=== KnitTiff OPENED ==="); + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#execute() + */ + public void execute() { + try { + if (getValue("odd") == null) throw new InstantiationException("You need to choose a sourcefile for the odd pages"); + File odd_file = (File)getValue("odd"); + if (getValue("even") == null) throw new InstantiationException("You need to choose a sourcefile for the even pages"); + File even_file = (File)getValue("even"); + if (getValue("destfile") == null) throw new InstantiationException("You need to choose a destination file"); + File pdf_file = (File)getValue("destfile"); + RandomAccessFileOrArray odd = new RandomAccessFileOrArray(odd_file.getAbsolutePath()); + RandomAccessFileOrArray even = new RandomAccessFileOrArray(even_file.getAbsolutePath()); + Image img = TiffImage.getTiffImage(odd, 1); + Document document = new Document(new Rectangle(img.scaledWidth(), + img.scaledHeight())); + PdfWriter writer = PdfWriter.getInstance(document, + new FileOutputStream(pdf_file)); + document.open(); + PdfContentByte cb = writer.getDirectContent(); + int count = Math.max(TiffImage.getNumberOfPages(odd), TiffImage + .getNumberOfPages(even)); + for (int c = 0; c < count; ++c) { + try { + Image imgOdd = TiffImage.getTiffImage(odd, c + 1); + Image imgEven = TiffImage.getTiffImage(even, count - c); + document.setPageSize(new Rectangle(imgOdd.scaledWidth(), + imgOdd.scaledHeight())); + document.newPage(); + imgOdd.setAbsolutePosition(0, 0); + cb.addImage(imgOdd); + document.setPageSize(new Rectangle(imgEven.scaledWidth(), + imgEven.scaledHeight())); + document.newPage(); + imgEven.setAbsolutePosition(0, 0); + cb.addImage(imgEven); + + } catch (Exception e) { + System.out.println("Exception page " + (c + 1) + " " + + e.getMessage()); + } + } + odd.close(); + even.close(); + document.close(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#valueHasChanged(com.lowagie.tools.arguments.ToolArgument) + */ + public void valueHasChanged(ToolArgument arg) { + if (internalFrame == null) { + // if the internal frame is null, the tool was called from the commandline + return; + } + // represent the changes of the argument in the internal frame + } + + + /** + * Converts a tiff file to PDF. + * @param args + */ + public static void main(String[] args) { + KnitTiff tool = new KnitTiff(); + if (args.length < 3) { + System.err.println(tool.getUsage()); + } + tool.setArguments(args); + tool.execute(); + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#getDestPathPDF() + */ + protected File getDestPathPDF() throws InstantiationException { + return (File)getValue("destfile"); + } +} diff --git a/src/main/java/com/lowagie/tools/plugins/LPRClient.java b/src/main/java/com/lowagie/tools/plugins/LPRClient.java new file mode 100644 index 0000000..3214013 --- /dev/null +++ b/src/main/java/com/lowagie/tools/plugins/LPRClient.java @@ -0,0 +1,245 @@ +/* + * $Id: LPRClient.java,v 1.4 2005/11/29 21:05:02 blowagie Exp $ + * $Name: $ + * + * Copyright 2005 by Anonymous. + * + * The contents of this file are subject to the Mozilla Public License Version 1.1 + * (the "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the License. + * + * The Original Code is 'iText, a free JAVA-PDF library'. + * + * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by + * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie. + * All Rights Reserved. + * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer + * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved. + * + * Contributor(s): all the names of the contributors are added in the source code + * where applicable. + * + * Alternatively, the contents of this file may be used under the terms of the + * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the + * provisions of LGPL are applicable instead of those above. If you wish to + * allow use of your version of this file only under the terms of the LGPL + * License and not to allow others to use your version of this file under + * the MPL, indicate your decision by deleting the provisions above and + * replace them with the notice and other provisions required by the LGPL. + * If you do not delete the provisions above, a recipient may use your version + * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE. + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the MPL as stated above or under the terms of the GNU + * Library General Public License as published by the Free Software Foundation; + * either version 2 of the License, or any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more + * details. + * + * If you didn't download this code from the following link, you should check if + * you aren't using an obsolete version: + * http://www.lowagie.com/iText/ + */ +package com.lowagie.tools.plugins; + +import java.io.File; + +import javax.swing.JInternalFrame; +import javax.swing.JOptionPane; + +import com.lowagie.tools.arguments.FileArgument; +import com.lowagie.tools.arguments.PdfFilter; +import com.lowagie.tools.arguments.ToolArgument; +import com.lowagie.tools.LPR; + +/** + * Allows you to print an existing PDF file via lpr. + */ +public class LPRClient extends AbstractTool { + static { + addVersion("$Id: LPRClient.java,v 1.4 2005/11/29 21:05:02 blowagie Exp $"); + } + +String fallback="%!PS\n"+ + "/vpos 720 def\n"+ + "/newline\n"+ + " {\n"+ + "/vpos vpos 15 sub def\n"+ + "72 vpos moveto\n"+ + "} def \n"+ + "/printword\n"+ + "{\n"+ + "show\n"+ + "newline\n"+ + "vpos 100 le { \n"+ + "showpage\n"+ + "100 100 moveto\n"+ + "/vpos 720 def\n"+ + "/Helvetica findfont 15 scalefont setfont\n"+ + "} if\n"+ + "} def \n"+ + "/nstr 9 string def\n"+ + "/prt-n\n"+ + "{\n"+ + "nstr cvs printword\n"+ + "} def\n"+ + "100 100 moveto\n"+ + "/Helvetica findfont 15 scalefont setfont\n"+ + "(---) printword \n"+ + "(Postscript Engine Testpage) printword\n"+ + "() printword\n"+ + "() printword\n"+ + "(Defaultpagesize) printword\n"+ + "currentpagedevice /PageSize get\n"+ + "(Width: ) show \n"+ + "0 get prt-n\n"+ + "currentpagedevice /PageSize get\n"+ + "(Height: ) show \n"+ + "1 get prt-n\n"+ + "() printword\n"+ + "(Printerresolution) printword\n"+ + "currentpagedevice /HWResolution get\n"+ + "(X: ) show \n"+ + "0 get prt-n\n"+ + "currentpagedevice /HWResolution get\n"+ + "(Y: ) show \n"+ + "1 get prt-n\n"+ + "() printword\n"+ + "(Information about Postscriptengine) printword\n"+ + "(Postscriptengine Type: ) show\n"+ + "product printword\n"+ + "(Version: ) show\n"+ + "version printword\n"+ + "() printword \n"+ + "mark\n"+ + "(\n) \n"+ + "revision 10 mod \n"+ + "revision 100 mod 10 idiv (.)\n"+ + "revision 100 idiv \n"+ + "(Revision: )\n"+ + "(\n) \n"+ + "counttomark\n"+ + "{ 17 string cvs show\n"+ + "} repeat pop \n"+ + "() printword \n"+ + "(Postscript Languagelevel: ) show\n"+ + "/languagelevel where\n"+ + "{pop languagelevel}\n"+ + "{1}\n"+ + "ifelse\n"+ + "3 string cvs printword \n"+ + "usertime \n"+ + "prt-n \n"+ + "vmstatus\n"+ + "(Printerram Max.: ) show\n"+ + "prt-n\n"+ + "(Printerram Cur.: ) show\n"+ + "prt-n\n"+ + "() printword\n"+ + "showpage"; + /** + * Constructs an LPRClient object. + */ + public LPRClient() { + arguments.add(new FileArgument(this, "srcfile", + "The file you want to print", false, new PdfFilter())); + arguments.add(new ToolArgument(this, "hostname", + "The hostname of the lpr server", String.class.getName())); + arguments.add(new ToolArgument(this, "queuename", + "The queuename of the lpr server", String.class.getName())); + arguments.add(new ToolArgument(this, "copies", + "The number of copies to print", String.class.getName())); + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#createFrame() + */ + protected void createFrame() { + internalFrame = new JInternalFrame("LPR", true, false, true); + internalFrame.setSize(500, 300); + internalFrame.setJMenuBar(getMenubar()); + System.out.println("=== LPR OPENED ==="); + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#execute() + */ + public void execute() { + try { + String filename=null; + File pdffile=null; + if (getValue("srcfile") == null){ + filename=null; + }else{ + filename = getValue("srcfile").toString(); + pdffile = new File(filename); + } +// throw new InstantiationException( +// "You need to choose a sourcefile"); + if (getValue("hostname") == null) + throw new InstantiationException( + "You need to choose a hostname"); + if (getValue("queuename") == null) + throw new InstantiationException( + "You need to choose a queuename"); + if (getValue("copies") == null) + throw new InstantiationException( + "You need to choose the number of copies"); + LPR lpr = new LPR(getValue("hostname").toString(), System + .getProperty("user.name")); + lpr.setCopies(Integer.parseInt(getValue("copies").toString())); + if(filename==null){ + lpr.print(getValue("queuename").toString(), fallback, "Version"); + }else{ + lpr.print(getValue("queuename").toString(), pdffile, pdffile + .getName()); + } + + } catch (Exception e) { + JOptionPane.showMessageDialog(internalFrame, e.getMessage(), e + .getClass().getName(), JOptionPane.ERROR_MESSAGE); + System.err.println(e.getMessage()); + } + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#valueHasChanged(com.lowagie.tools.arguments.ToolArgument) + */ + public void valueHasChanged(ToolArgument arg) { + if (internalFrame == null) { + // if the internal frame is null, the tool was called from the + // commandline + return; + } + // represent the changes of the argument in the internal frame + } + + /** + * Prints a PDF file via lpr. + * + * @param args + */ + public static void main(String[] args) { + LPRClient tool = new LPRClient(); + if (args.length < tool.getArguments().size()) { + System.err.println(tool.getUsage()); + } + tool.setArguments(args); + tool.execute(); + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#getDestPathPDF() + */ + protected File getDestPathPDF() throws InstantiationException { + throw new InstantiationException("There is no file to show."); + } + +} diff --git a/src/main/java/com/lowagie/tools/plugins/NUp.java b/src/main/java/com/lowagie/tools/plugins/NUp.java new file mode 100644 index 0000000..7376213 --- /dev/null +++ b/src/main/java/com/lowagie/tools/plugins/NUp.java @@ -0,0 +1,165 @@ +/* + * $Id: NUp.java,v 1.6 2005/11/29 21:05:02 blowagie Exp $ + * $Name: $ + * + * This code is free software. It may only be copied or modified + * if you include the following copyright notice: + * + * This class by Mark Thompson. Copyright (c) 2002 Mark Thompson. + * + * This code is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * + * itext-questions@list.sourceforge.net + */ +package com.lowagie.tools.plugins; + +import java.io.File; +import java.io.FileOutputStream; + +import javax.swing.JInternalFrame; + +import com.lowagie.text.Document; +import com.lowagie.text.Rectangle; +import com.lowagie.text.pdf.PdfContentByte; +import com.lowagie.text.pdf.PdfImportedPage; +import com.lowagie.text.pdf.PdfReader; +import com.lowagie.text.pdf.PdfWriter; +import com.lowagie.tools.arguments.FileArgument; +import com.lowagie.tools.arguments.OptionArgument; +import com.lowagie.tools.arguments.PdfFilter; +import com.lowagie.tools.arguments.ToolArgument; + +/** + * This tool lets you generate a PDF that shows N pages on 1. + */ +public class NUp extends AbstractTool { + + static { + addVersion("$Id: NUp.java,v 1.6 2005/11/29 21:05:02 blowagie Exp $"); + } + + /** + * Constructs an NUp object. + */ + public NUp() { + menuoptions = MENU_EXECUTE | MENU_EXECUTE_SHOW; + arguments.add(new FileArgument(this, "srcfile", "The file you want to N-up", false, new PdfFilter())); + arguments.add(new FileArgument(this, "destfile", "The resulting PDF", true, new PdfFilter())); + OptionArgument oa = new OptionArgument(this, "pow2", "The number of pages you want to copy to 1 page"); + oa.addOption("2", "1"); + oa.addOption("4", "2"); + oa.addOption("8", "3"); + oa.addOption("16", "4"); + oa.addOption("32", "5"); + oa.addOption("64", "6"); + arguments.add(oa); + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#createFrame() + */ + protected void createFrame() { + internalFrame = new JInternalFrame("N-up", true, false, true); + internalFrame.setSize(300, 80); + internalFrame.setJMenuBar(getMenubar()); + System.out.println("=== N-up OPENED ==="); + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#execute() + */ + public void execute() { + try { + if (getValue("srcfile") == null) throw new InstantiationException("You need to choose a sourcefile"); + File src = (File)getValue("srcfile"); + if (getValue("destfile") == null) throw new InstantiationException("You need to choose a destination file"); + File dest = (File)getValue("destfile"); + int pow2; + try { + pow2 = Integer.parseInt((String) getValue("pow2")); + } + catch(Exception e) { + pow2 = 1; + } + // we create a reader for a certain document + PdfReader reader = new PdfReader(src.getAbsolutePath()); + // we retrieve the total number of pages and the page size + int total = reader.getNumberOfPages(); + System.out.println("There are " + total + " pages in the original file."); + Rectangle pageSize = reader.getPageSize(1); + Rectangle newSize = (pow2 % 2) == 0 ? new Rectangle(pageSize.width(), pageSize.height()) : new Rectangle(pageSize.height(), pageSize.width()); + Rectangle unitSize = new Rectangle(pageSize.width(), pageSize.height()); + Rectangle currentSize; + for (int i = 0; i < pow2; i++) { + unitSize = new Rectangle(unitSize.height() / 2, unitSize.width()); + } + int n = (int)Math.pow(2, pow2); + int r = (int)Math.pow(2, (int)pow2 / 2); + int c = n / r; + // step 1: creation of a document-object + Document document = new Document(newSize, 0, 0, 0, 0); + // step 2: we create a writer that listens to the document + PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest)); + // step 3: we open the document + document.open(); + // step 4: adding the content + PdfContentByte cb = writer.getDirectContent(); + PdfImportedPage page; + float offsetX, offsetY, factor; + int p; + for (int i = 0; i < total; i++) { + if (i % n == 0) { + document.newPage(); + } + p = i + 1; + offsetX = unitSize.width() * ((i % n) % c); + offsetY = newSize.height() - (unitSize.height() * (((i % n) / c) + 1)); + currentSize = reader.getPageSize(p); + factor = Math.min(unitSize.width() / currentSize.width(), unitSize.height() / currentSize.height()); + offsetX += (unitSize.width() - (currentSize.width() * factor)) / 2f; + offsetY += (unitSize.height() - (currentSize.height() * factor)) / 2f; + page = writer.getImportedPage(reader, p); + cb.addTemplate(page, factor, 0, 0, factor, offsetX, offsetY); + } + // step 5: we close the document + document.close(); + } + catch(Exception e) { + e.printStackTrace(); + } + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#valueHasChanged(com.lowagie.tools.arguments.ToolArgument) + */ + public void valueHasChanged(ToolArgument arg) { + if (internalFrame == null) { + // if the internal frame is null, the tool was called from the commandline + return; + } + // represent the changes of the argument in the internal frame + } + + + /** + * Generates an NUp version of an existing PDF file. + * @param args + */ + public static void main(String[] args) { + NUp tool = new NUp(); + if (args.length < 2) { + System.err.println(tool.getUsage()); + } + tool.setArguments(args); + tool.execute(); + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#getDestPathPDF() + */ + protected File getDestPathPDF() throws InstantiationException { + return (File)getValue("destfile"); + } +} diff --git a/src/main/java/com/lowagie/tools/plugins/PhotoAlbum.java b/src/main/java/com/lowagie/tools/plugins/PhotoAlbum.java new file mode 100644 index 0000000..26a95f3 --- /dev/null +++ b/src/main/java/com/lowagie/tools/plugins/PhotoAlbum.java @@ -0,0 +1,200 @@ +/* + * $Id: PhotoAlbum.java,v 1.8 2006/03/21 08:49:35 blowagie Exp $ + * $Name: $ + * + * Copyright 2005 by Bruno Lowagie. + * + * The contents of this file are subject to the Mozilla Public License Version 1.1 + * (the "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the License. + * + * The Original Code is 'iText, a free JAVA-PDF library'. + * + * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by + * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie. + * All Rights Reserved. + * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer + * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved. + * + * Contributor(s): all the names of the contributors are added in the source code + * where applicable. + * + * Alternatively, the contents of this file may be used under the terms of the + * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the + * provisions of LGPL are applicable instead of those above. If you wish to + * allow use of your version of this file only under the terms of the LGPL + * License and not to allow others to use your version of this file under + * the MPL, indicate your decision by deleting the provisions above and + * replace them with the notice and other provisions required by the LGPL. + * If you do not delete the provisions above, a recipient may use your version + * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE. + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the MPL as stated above or under the terms of the GNU + * Library General Public License as published by the Free Software Foundation; + * either version 2 of the License, or any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more + * details. + * + * If you didn't download this code from the following link, you should check if + * you aren't using an obsolete version: + * http://www.lowagie.com/iText/ + */ +package com.lowagie.tools.plugins; + +import java.io.File; +import java.io.FileOutputStream; +import java.util.Iterator; +import java.util.TreeSet; + +import javax.swing.JInternalFrame; +import javax.swing.JOptionPane; + +import com.lowagie.text.Document; +import com.lowagie.text.Image; +import com.lowagie.text.Rectangle; +import com.lowagie.text.pdf.PdfPageLabels; +import com.lowagie.text.pdf.PdfWriter; +import com.lowagie.tools.arguments.DirFilter; +import com.lowagie.tools.arguments.FileArgument; +import com.lowagie.tools.arguments.PdfFilter; +import com.lowagie.tools.arguments.ToolArgument; + +/** + * Converts a Tiff file to a PDF file. + * Inspired by a comp.text.pdf question by Sebastian Schubert + * and an answer by Hans-Werner Hilse. + */ +public class PhotoAlbum extends AbstractTool { + + static { + addVersion("$Id: PhotoAlbum.java,v 1.8 2006/03/21 08:49:35 blowagie Exp $"); + } + /** + * Constructs a PhotoAlbum object. + */ + public PhotoAlbum() { + menuoptions = MENU_EXECUTE | MENU_EXECUTE_SHOW; + arguments.add(new FileArgument(this, "srcdir", "The directory containing the image files", false, new DirFilter())); + arguments.add(new FileArgument(this, "destfile", "The file to which the converted TIFF has to be written", true, new PdfFilter())); + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#createFrame() + */ + protected void createFrame() { + internalFrame = new JInternalFrame("PhotoAlbum", true, false, true); + internalFrame.setSize(300, 80); + internalFrame.setJMenuBar(getMenubar()); + System.out.println("=== PhotoAlbum OPENED ==="); + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#execute() + */ + public void execute() { + try { + if (getValue("srcdir") == null) throw new InstantiationException("You need to choose a source directory"); + File directory = (File)getValue("srcdir"); + if (directory.isFile()) directory = directory.getParentFile(); + if (getValue("destfile") == null) throw new InstantiationException("You need to choose a destination file"); + File pdf_file = (File)getValue("destfile"); + Document document = new Document(); + PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(pdf_file)); + writer.setViewerPreferences(PdfWriter.PageModeUseThumbs); + PdfPageLabels pageLabels = new PdfPageLabels(); + int dpiX, dpiY; + float imgWidthPica, imgHeightPica; + TreeSet images = new TreeSet(); + File[] files = directory.listFiles(); + for (int i = 0; i < files.length; i++) { + if (files[i].isFile()) images.add(files[i]); + } + File image; + String label; + for (Iterator i = images.iterator(); i.hasNext(); ) { + image = (File) i.next(); + System.out.println("Testing image: " + image.getName()); + try { + Image img = Image.getInstance(image.getAbsolutePath()); + dpiX=img.getDpiX(); + if (dpiX == 0) dpiX=72; + dpiY=img.getDpiY(); + if (dpiY == 0) dpiY=72; + imgWidthPica=(72*img.plainWidth()) / dpiX; + imgHeightPica=(72*img.plainHeight()) / dpiY; + img.scaleAbsolute(imgWidthPica,imgHeightPica); + document.setPageSize(new Rectangle(imgWidthPica, imgHeightPica)); + if (document.isOpen()) { + document.newPage(); + } + else { + document.open(); + } + img.setAbsolutePosition(0, 0); + document.add(img); + label = image.getName(); + if (label.lastIndexOf(".") > 0); + label = label.substring(0, label.lastIndexOf(".")); + pageLabels.addPageLabel(writer.getPageNumber(), PdfPageLabels.EMPTY, label); + System.out.println("Added image: " + image.getName()); + } + catch(Exception e) { + System.err.println(e.getMessage()); + } + } + if (document.isOpen()) { + writer.setPageLabels(pageLabels); + document.close(); + } + else { + System.err.println("No images were found in directory " + directory.getAbsolutePath()); + } + } catch (Exception e) { + JOptionPane.showMessageDialog(internalFrame, + e.getMessage(), + e.getClass().getName(), + JOptionPane.ERROR_MESSAGE); + System.err.println(e.getMessage()); + } + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#valueHasChanged(com.lowagie.tools.arguments.ToolArgument) + */ + public void valueHasChanged(ToolArgument arg) { + if (internalFrame == null) { + // if the internal frame is null, the tool was called from the commandline + return; + } + // represent the changes of the argument in the internal frame + } + + + /** + * Converts a tiff file to PDF. + * @param args + */ + public static void main(String[] args) { + PhotoAlbum tool = new PhotoAlbum(); + if (args.length < 2) { + System.err.println(tool.getUsage()); + } + tool.setArguments(args); + tool.execute(); + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#getDestPathPDF() + */ + protected File getDestPathPDF() throws InstantiationException { + return (File)getValue("destfile"); + } +} diff --git a/src/main/java/com/lowagie/tools/plugins/RemoveLaunchApplication.java b/src/main/java/com/lowagie/tools/plugins/RemoveLaunchApplication.java new file mode 100644 index 0000000..fc082e7 --- /dev/null +++ b/src/main/java/com/lowagie/tools/plugins/RemoveLaunchApplication.java @@ -0,0 +1,189 @@ +/* + * $Id: RemoveLaunchApplication.java,v 1.4 2005/11/29 21:05:02 blowagie Exp $ + * $Name: $ + * + * Copyright 2005 by Bruno Lowagie + * + * The contents of this file are subject to the Mozilla Public License Version 1.1 + * (the "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the License. + * + * The Original Code is 'iText, a free JAVA-PDF library'. + * + * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by + * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie. + * All Rights Reserved. + * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer + * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved. + * + * Contributor(s): all the names of the contributors are added in the source code + * where applicable. + * + * Alternatively, the contents of this file may be used under the terms of the + * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the + * provisions of LGPL are applicable instead of those above. If you wish to + * allow use of your version of this file only under the terms of the LGPL + * License and not to allow others to use your version of this file under + * the MPL, indicate your decision by deleting the provisions above and + * replace them with the notice and other provisions required by the LGPL. + * If you do not delete the provisions above, a recipient may use your version + * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE. + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the MPL as stated above or under the terms of the GNU + * Library General Public License as published by the Free Software Foundation; + * either version 2 of the License, or any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more + * details. + * + * If you didn't download this code from the following link, you should check if + * you aren't using an obsolete version: + * http://www.lowagie.com/iText/ + */ +package com.lowagie.tools.plugins; + +import java.io.File; +import java.io.FileOutputStream; + +import javax.swing.JInternalFrame; + +import com.lowagie.text.pdf.PRIndirectReference; +import com.lowagie.text.pdf.PdfDictionary; +import com.lowagie.text.pdf.PdfName; +import com.lowagie.text.pdf.PdfObject; +import com.lowagie.text.pdf.PdfReader; +import com.lowagie.text.pdf.PdfStamper; +import com.lowagie.text.pdf.PdfString; +import com.lowagie.tools.arguments.FileArgument; +import com.lowagie.tools.arguments.PdfFilter; +import com.lowagie.tools.arguments.ToolArgument; + +/** + * This tool copies an existing PDF and removes potentially dangerous code that launches an application. + */ +public class RemoveLaunchApplication + extends AbstractTool { + + static { + addVersion( + "$Id: RemoveLaunchApplication.java,v 1.4 2005/11/29 21:05:02 blowagie Exp $"); + } + + /** + * Constructs a ReversePages object. + */ + public RemoveLaunchApplication() { + menuoptions = MENU_EXECUTE | MENU_EXECUTE_SHOW; + arguments.add(new FileArgument(this, "srcfile", + "The file from which you want to remove Launch Application actions", false, + new PdfFilter())); + arguments.add(new FileArgument(this, "destfile", + "The file to which the cleaned up version of the original PDF has to be written", true, + new PdfFilter())); + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#createFrame() + */ + protected void createFrame() { + internalFrame = new JInternalFrame("Remove Launch Applications", true, false, true); + internalFrame.setSize(300, 80); + internalFrame.setJMenuBar(getMenubar()); + System.out.println("=== Remove Launch Applications OPENED ==="); + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#execute() + */ + public void execute() { + try { + if (getValue("srcfile") == null) { + throw new InstantiationException("You need to choose a sourcefile"); + } + File src = (File) getValue("srcfile"); + if (getValue("destfile") == null) { + throw new InstantiationException( + "You need to choose a destination file"); + } + File dest = (File) getValue("destfile"); + + // we create a reader for a certain document + PdfReader reader = new PdfReader(src.getAbsolutePath()); + PdfObject o; + PdfDictionary d; + PdfDictionary l; + PdfName n; + for (int i = 1; i < reader.getXrefSize(); i++) { + o = reader.getPdfObject(i); + if (o instanceof PdfDictionary) { + d = (PdfDictionary)o; + o = d.get(PdfName.A); + if (o == null) continue; + if (o instanceof PdfDictionary) { + l = (PdfDictionary)o; + } + else { + PRIndirectReference r =(PRIndirectReference)o; + l = (PdfDictionary)reader.getPdfObject(r.getNumber()); + } + n = (PdfName)l.get(PdfName.S); + if (PdfName.LAUNCH.equals(n)) { + if (l.get(PdfName.F) != null) { + System.out.println("Removed: " + l.get(PdfName.F)); + l.remove(PdfName.F); + } + if (l.get(PdfName.WIN) != null) { + System.out.println("Removed: " + l.get(PdfName.WIN)); + l.remove(PdfName.WIN); + } + l.put(PdfName.S, PdfName.JAVASCRIPT); + l.put(PdfName.JS, new PdfString("app.alert('Launch Application Action removed by iText');\r")); + } + } + } + PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest)); + stamper.close(); + } + catch (Exception e) { + e.printStackTrace(); + } + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#valueHasChanged(com.lowagie.tools.arguments.ToolArgument) + */ + public void valueHasChanged(ToolArgument arg) { + if (internalFrame == null) { + // if the internal frame is null, the tool was called from the commandline + return; + } + // represent the changes of the argument in the internal frame + } + + /** + * Copy an existing PDF and replace the Launch Application Action with JavaScript alerts. + * @param args + */ + public static void main(String[] args) { + RemoveLaunchApplication tool = new RemoveLaunchApplication(); + if (args.length < 2) { + System.err.println(tool.getUsage()); + } + tool.setArguments(args); + tool.execute(); + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#getDestPathPDF() + */ + protected File getDestPathPDF() throws InstantiationException { + return (File) getValue("destfile"); + } +} \ No newline at end of file diff --git a/src/main/java/com/lowagie/tools/plugins/ReversePages.java b/src/main/java/com/lowagie/tools/plugins/ReversePages.java new file mode 100644 index 0000000..52b3053 --- /dev/null +++ b/src/main/java/com/lowagie/tools/plugins/ReversePages.java @@ -0,0 +1,180 @@ +/* + * $Id: ReversePages.java,v 1.3 2005/11/29 21:05:02 blowagie Exp $ + * $Name: $ + * + * Copyright 2005 by Bruno Lowagie and anonymous. + * + * The contents of this file are subject to the Mozilla Public License Version 1.1 + * (the "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the License. + * + * The Original Code is 'iText, a free JAVA-PDF library'. + * + * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by + * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie. + * All Rights Reserved. + * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer + * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved. + * + * Contributor(s): all the names of the contributors are added in the source code + * where applicable. + * + * Alternatively, the contents of this file may be used under the terms of the + * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the + * provisions of LGPL are applicable instead of those above. If you wish to + * allow use of your version of this file only under the terms of the LGPL + * License and not to allow others to use your version of this file under + * the MPL, indicate your decision by deleting the provisions above and + * replace them with the notice and other provisions required by the LGPL. + * If you do not delete the provisions above, a recipient may use your version + * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE. + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the MPL as stated above or under the terms of the GNU + * Library General Public License as published by the Free Software Foundation; + * either version 2 of the License, or any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more + * details. + * + * If you didn't download this code from the following link, you should check if + * you aren't using an obsolete version: + * http://www.lowagie.com/iText/ + */ +package com.lowagie.tools.plugins; + +import java.io.File; +import java.io.FileOutputStream; + +import javax.swing.JInternalFrame; + +import com.lowagie.text.Document; +import com.lowagie.text.pdf.PRAcroForm; +import com.lowagie.text.pdf.PdfCopy; +import com.lowagie.text.pdf.PdfImportedPage; +import com.lowagie.text.pdf.PdfReader; +import com.lowagie.tools.arguments.FileArgument; +import com.lowagie.tools.arguments.PdfFilter; +import com.lowagie.tools.arguments.ToolArgument; + +/** + * This tool lets you take pages from an existing PDF and copy them in reverse order into a new PDF. + */ +public class ReversePages + extends AbstractTool { + + static { + addVersion( + "$Id: ReversePages.java,v 1.3 2005/11/29 21:05:02 blowagie Exp $"); + } + + /** + * Constructs a ReversePages object. + */ + public ReversePages() { + menuoptions = MENU_EXECUTE | MENU_EXECUTE_SHOW; + arguments.add(new FileArgument(this, "srcfile", + "The file you want to reorder", false, + new PdfFilter())); + arguments.add(new FileArgument(this, "destfile", + "The file to which the reordered version of the original PDF has to be written", true, + new PdfFilter())); + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#createFrame() + */ + protected void createFrame() { + internalFrame = new JInternalFrame("ReversePages", true, false, true); + internalFrame.setSize(300, 80); + internalFrame.setJMenuBar(getMenubar()); + System.out.println("=== ReversePages OPENED ==="); + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#execute() + */ + public void execute() { + try { + if (getValue("srcfile") == null) { + throw new InstantiationException("You need to choose a sourcefile"); + } + File src = (File) getValue("srcfile"); + if (getValue("destfile") == null) { + throw new InstantiationException( + "You need to choose a destination file"); + } + File dest = (File) getValue("destfile"); + + // we create a reader for a certain document + PdfReader reader = new PdfReader(src.getAbsolutePath()); + System.out.println("The original file had " + reader.getNumberOfPages() + + " pages."); + int pages = reader.getNumberOfPages(); + java.util.ArrayList li=new java.util.ArrayList(); + for(int i=pages;i>0;i--){ + li.add(new Integer(i)); + } + reader.selectPages(li); + + System.err.println("The new file has " + pages + " pages."); + Document document = new Document(reader.getPageSizeWithRotation(1)); + PdfCopy copy = new PdfCopy(document, + new FileOutputStream(dest.getAbsolutePath())); + document.open(); + PdfImportedPage page; + for (int i = 0; i < pages; ) { + ++i; + System.out.println("Processed page " + i); + page = copy.getImportedPage(reader, i); + copy.addPage(page); + } + + PRAcroForm form = reader.getAcroForm(); + if (form != null) { + copy.copyAcroForm(reader); + } + document.close(); + } + catch (Exception e) { + e.printStackTrace(); + } + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#valueHasChanged(com.lowagie.tools.arguments.ToolArgument) + */ + public void valueHasChanged(ToolArgument arg) { + if (internalFrame == null) { + // if the internal frame is null, the tool was called from the commandline + return; + } + // represent the changes of the argument in the internal frame + } + + /** + * Take pages from an existing PDF and copy them in reverse order into a new PDF. + * @param args + */ + public static void main(String[] args) { + ReversePages tool = new ReversePages(); + if (args.length < 2) { + System.err.println(tool.getUsage()); + } + tool.setArguments(args); + tool.execute(); + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#getDestPathPDF() + */ + protected File getDestPathPDF() throws InstantiationException { + return (File) getValue("destfile"); + } +} \ No newline at end of file diff --git a/src/main/java/com/lowagie/tools/plugins/SelectedPages.java b/src/main/java/com/lowagie/tools/plugins/SelectedPages.java new file mode 100644 index 0000000..73b3b4f --- /dev/null +++ b/src/main/java/com/lowagie/tools/plugins/SelectedPages.java @@ -0,0 +1,150 @@ +/* + * $Id: SelectedPages.java,v 1.7 2005/11/29 21:05:02 blowagie Exp $ + * $Name: $ + * + * This code is free software. It may only be copied or modified + * if you include the following copyright notice: + * + * This class by Mark Thompson. Copyright (c) 2002 Mark Thompson. + * + * This code is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * + * itext-questions@list.sourceforge.net + */ +package com.lowagie.tools.plugins; + +import java.io.File; +import java.io.FileOutputStream; + +import javax.swing.JInternalFrame; + +import com.lowagie.text.Document; +import com.lowagie.text.pdf.PRAcroForm; +import com.lowagie.text.pdf.PdfCopy; +import com.lowagie.text.pdf.PdfImportedPage; +import com.lowagie.text.pdf.PdfReader; +import com.lowagie.tools.arguments.FileArgument; +import com.lowagie.tools.arguments.PageSelectorToolArgument; +import com.lowagie.tools.arguments.PdfFilter; +import com.lowagie.tools.arguments.ToolArgument; + +/** + * This tool lets you select pages from an existing PDF and copy them into a new PDF. + */ +public class SelectedPages + extends AbstractTool { + + static { + addVersion( + "$Id: SelectedPages.java,v 1.7 2005/11/29 21:05:02 blowagie Exp $"); + } + + /** + * Constructs a SelectedPages object. + */ + public SelectedPages() { + menuoptions = MENU_EXECUTE | MENU_EXECUTE_SHOW; + ToolArgument inputfile = new FileArgument(this, "srcfile", + "The file you want to split", false, + new PdfFilter()); + arguments.add(inputfile); + arguments.add(new FileArgument(this, "destfile", + "The file to which the first part of the original PDF has to be written", true, + new PdfFilter())); + ToolArgument spfpdf = new PageSelectorToolArgument(this, "selection", + "A selection of pages (see Help for more info)", String.class.getName()); + arguments.add(spfpdf); + inputfile.addPropertyChangeListener(spfpdf); + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#createFrame() + */ + protected void createFrame() { + internalFrame = new JInternalFrame("SelectedPages", true, false, true); + internalFrame.setSize(300, 80); + internalFrame.setJMenuBar(getMenubar()); + System.out.println("=== SelectedPages OPENED ==="); + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#execute() + */ + public void execute() { + try { + if (getValue("srcfile") == null) { + throw new InstantiationException("You need to choose a sourcefile"); + } + File src = (File) getValue("srcfile"); + if (getValue("destfile") == null) { + throw new InstantiationException( + "You need to choose a destination file for the first part of the PDF"); + } + File dest = (File) getValue("destfile"); + String selection = (String) getValue("selection"); + + // we create a reader for a certain document + PdfReader reader = new PdfReader(src.getAbsolutePath()); + System.out.println("The original file had " + reader.getNumberOfPages() + + " pages."); + reader.selectPages(selection); + int pages = reader.getNumberOfPages(); + System.err.println("The new file has " + pages + " pages."); + Document document = new Document(reader.getPageSizeWithRotation(1)); + PdfCopy copy = new PdfCopy(document, + new FileOutputStream(dest.getAbsolutePath())); + document.open(); + PdfImportedPage page; + for (int i = 0; i < pages; ) { + ++i; + System.out.println("Processed page " + i); + page = copy.getImportedPage(reader, i); + copy.addPage(page); + } + PRAcroForm form = reader.getAcroForm(); + if (form != null) { + copy.copyAcroForm(reader); + } + document.close(); + } + catch (Exception e) { + e.printStackTrace(); + } + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#valueHasChanged(com.lowagie.tools.arguments.ToolArgument) + */ + public void valueHasChanged(ToolArgument arg) { + if (internalFrame == null) { + // if the internal frame is null, the tool was called from the commandline + return; + } + // represent the changes of the argument in the internal frame + System.out.println("klasse:" + arg.getClassname()); + System.out.println("arg:" + arg.getValue()); + } + + /** + * Select pages from an existing PDF and copy them into a new PDF. + * @param args + */ + public static void main(String[] args) { + SelectedPages tool = new SelectedPages(); + if (args.length < 4) { + System.err.println(tool.getUsage()); + } + tool.setArguments(args); + tool.execute(); + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#getDestPathPDF() + */ + protected File getDestPathPDF() throws InstantiationException { + return (File) getValue("destfile"); + } + +} diff --git a/src/main/java/com/lowagie/tools/plugins/Split.java b/src/main/java/com/lowagie/tools/plugins/Split.java new file mode 100644 index 0000000..2c14651 --- /dev/null +++ b/src/main/java/com/lowagie/tools/plugins/Split.java @@ -0,0 +1,168 @@ +/* + * $Id: Split.java,v 1.9 2005/11/29 21:05:02 blowagie Exp $ + * $Name: $ + * + * This code is free software. It may only be copied or modified + * if you include the following copyright notice: + * + * This class by Mark Thompson. Copyright (c) 2002 Mark Thompson. + * + * This code is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * + * itext-questions@list.sourceforge.net + */ +package com.lowagie.tools.plugins; + +import java.io.File; +import java.io.FileOutputStream; + +import javax.swing.JInternalFrame; + +import com.lowagie.text.Document; +import com.lowagie.text.DocumentException; +import com.lowagie.text.pdf.PdfContentByte; +import com.lowagie.text.pdf.PdfImportedPage; +import com.lowagie.text.pdf.PdfReader; +import com.lowagie.text.pdf.PdfWriter; +import com.lowagie.tools.arguments.FileArgument; +import com.lowagie.tools.arguments.LabelAccessory; +import com.lowagie.tools.arguments.PdfFilter; +import com.lowagie.tools.arguments.ToolArgument; + +/** + * This tool lets you split a PDF in two separate PDF files. + */ +public class Split extends AbstractTool { + + static { + addVersion("$Id: Split.java,v 1.9 2005/11/29 21:05:02 blowagie Exp $"); + } + /** + * Constructs an Split object. + */ + public Split() { + FileArgument f = new FileArgument(this, "srcfile", "The file you want to split", false, new PdfFilter()); + f.setLabel(new LabelAccessory()); + arguments.add(f); + arguments.add(new FileArgument(this, "destfile1", "The file to which the first part of the original PDF has to be written", true, new PdfFilter())); + arguments.add(new FileArgument(this, "destfile2", "The file to which the second part of the original PDF has to be written", true, new PdfFilter())); + arguments.add(new ToolArgument(this, "pagenumber", "The pagenumber where you want to split", String.class.getName())); + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#createFrame() + */ + protected void createFrame() { + internalFrame = new JInternalFrame("Split", true, false, true); + internalFrame.setSize(300, 80); + internalFrame.setJMenuBar(getMenubar()); + System.out.println("=== Split OPENED ==="); + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#execute() + */ + public void execute() { + try { + if (getValue("srcfile") == null) throw new InstantiationException("You need to choose a sourcefile"); + File src = (File)getValue("srcfile"); + if (getValue("destfile1") == null) throw new InstantiationException("You need to choose a destination file for the first part of the PDF"); + File file1 = (File)getValue("destfile1"); + if (getValue("destfile2") == null) throw new InstantiationException("You need to choose a destination file for the second part of the PDF"); + File file2 = (File)getValue("destfile2"); + int pagenumber = Integer.parseInt((String)getValue("pagenumber")); + + // we create a reader for a certain document + PdfReader reader = new PdfReader(src.getAbsolutePath()); + // we retrieve the total number of pages + int n = reader.getNumberOfPages(); + System.out.println("There are " + n + " pages in the original file."); + + if (pagenumber < 2 || pagenumber > n) { + throw new DocumentException("You can't split this document at page " + pagenumber + "; there is no such page."); + } + + // step 1: creation of a document-object + Document document1 = new Document(reader.getPageSizeWithRotation(1)); + Document document2 = new Document(reader.getPageSizeWithRotation(pagenumber)); + // step 2: we create a writer that listens to the document + PdfWriter writer1 = PdfWriter.getInstance(document1, new FileOutputStream(file1)); + PdfWriter writer2 = PdfWriter.getInstance(document2, new FileOutputStream(file2)); + // step 3: we open the document + document1.open(); + PdfContentByte cb1 = writer1.getDirectContent(); + document2.open(); + PdfContentByte cb2 = writer2.getDirectContent(); + PdfImportedPage page; + int rotation; + int i = 0; + // step 4: we add content + while (i < pagenumber - 1) { + i++; + document1.setPageSize(reader.getPageSizeWithRotation(i)); + document1.newPage(); + page = writer1.getImportedPage(reader, i); + rotation = reader.getPageRotation(i); + if (rotation == 90 || rotation == 270) { + cb1.addTemplate(page, 0, -1f, 1f, 0, 0, reader.getPageSizeWithRotation(i).height()); + } + else { + cb1.addTemplate(page, 1f, 0, 0, 1f, 0, 0); + } + } + while (i < n) { + i++; + document2.setPageSize(reader.getPageSizeWithRotation(i)); + document2.newPage(); + page = writer2.getImportedPage(reader, i); + rotation = reader.getPageRotation(i); + if (rotation == 90 || rotation == 270) { + cb2.addTemplate(page, 0, -1f, 1f, 0, 0, reader.getPageSizeWithRotation(i).height()); + } + else { + cb2.addTemplate(page, 1f, 0, 0, 1f, 0, 0); + } + } + // step 5: we close the document + document1.close(); + document2.close(); + } + catch(Exception e) { + e.printStackTrace(); + } + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#valueHasChanged(com.lowagie.tools.arguments.ToolArgument) + */ + public void valueHasChanged(ToolArgument arg) { + if (internalFrame == null) { + // if the internal frame is null, the tool was called from the commandline + return; + } + // represent the changes of the argument in the internal frame + } + + + /** + * Split a PDF in two separate PDF files. + * @param args + */ + public static void main(String[] args) { + Split tool = new Split(); + if (args.length < 4) { + System.err.println(tool.getUsage()); + } + tool.setArguments(args); + tool.execute(); + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#getDestPathPDF() + */ + protected File getDestPathPDF() throws InstantiationException { + return (File)getValue("destfile1"); + } +} diff --git a/src/main/java/com/lowagie/tools/plugins/Tiff2Pdf.java b/src/main/java/com/lowagie/tools/plugins/Tiff2Pdf.java new file mode 100644 index 0000000..8455cdf --- /dev/null +++ b/src/main/java/com/lowagie/tools/plugins/Tiff2Pdf.java @@ -0,0 +1,192 @@ +/* + * $Id: Tiff2Pdf.java,v 1.9 2005/11/29 21:05:02 blowagie Exp $ + * $Name: $ + * + * Copyright 2005 by Bruno Lowagie. + * + * The contents of this file are subject to the Mozilla Public License Version 1.1 + * (the "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the License. + * + * The Original Code is 'iText, a free JAVA-PDF library'. + * + * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by + * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie. + * All Rights Reserved. + * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer + * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved. + * + * Contributor(s): all the names of the contributors are added in the source code + * where applicable. + * + * Alternatively, the contents of this file may be used under the terms of the + * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the + * provisions of LGPL are applicable instead of those above. If you wish to + * allow use of your version of this file only under the terms of the LGPL + * License and not to allow others to use your version of this file under + * the MPL, indicate your decision by deleting the provisions above and + * replace them with the notice and other provisions required by the LGPL. + * If you do not delete the provisions above, a recipient may use your version + * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE. + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the MPL as stated above or under the terms of the GNU + * Library General Public License as published by the Free Software Foundation; + * either version 2 of the License, or any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more + * details. + * + * If you didn't download this code from the following link, you should check if + * you aren't using an obsolete version: + * http://www.lowagie.com/iText/ + */ +package com.lowagie.tools.plugins; + +import java.io.File; +import java.io.FileOutputStream; + +import javax.swing.JInternalFrame; +import javax.swing.JOptionPane; + +import com.lowagie.text.Document; +import com.lowagie.text.Image; +import com.lowagie.text.PageSize; +import com.lowagie.text.Paragraph; +import com.lowagie.text.Rectangle; +import com.lowagie.text.pdf.PdfContentByte; +import com.lowagie.text.pdf.PdfWriter; +import com.lowagie.text.pdf.RandomAccessFileOrArray; +import com.lowagie.text.pdf.codec.TiffImage; +import com.lowagie.tools.arguments.FileArgument; +import com.lowagie.tools.arguments.ImageFilter; +import com.lowagie.tools.arguments.OptionArgument; +import com.lowagie.tools.arguments.PdfFilter; +import com.lowagie.tools.arguments.ToolArgument; + +/** + * Converts a Tiff file to a PDF file. + */ +public class Tiff2Pdf extends AbstractTool { + + static { + addVersion("$Id: Tiff2Pdf.java,v 1.9 2005/11/29 21:05:02 blowagie Exp $"); + } + /** + * Constructs a Tiff2Pdf object. + */ + public Tiff2Pdf() { + menuoptions = MENU_EXECUTE | MENU_EXECUTE_SHOW; + arguments.add(new FileArgument(this, "srcfile", "The file you want to convert", false, new ImageFilter(false, false, false, false, false, true))); + arguments.add(new FileArgument(this, "destfile", "The file to which the converted TIFF has to be written", true, new PdfFilter())); + OptionArgument oa = new OptionArgument(this, "pagesize", "Pagesize"); + oa.addOption("A4", "A4"); + oa.addOption("Letter", "LETTER"); + oa.addOption("Original format", "ORIGINAL"); + arguments.add(oa); + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#createFrame() + */ + protected void createFrame() { + internalFrame = new JInternalFrame("Tiff2Pdf", true, false, true); + internalFrame.setSize(550, 250); + internalFrame.setJMenuBar(getMenubar()); + System.out.println("=== Tiff2Pdf OPENED ==="); + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#execute() + */ + public void execute() { + try { + if (getValue("srcfile") == null) throw new InstantiationException("You need to choose a sourcefile"); + File tiff_file = (File)getValue("srcfile"); + if (getValue("destfile") == null) throw new InstantiationException("You need to choose a destination file"); + File pdf_file = (File)getValue("destfile"); + RandomAccessFileOrArray ra = new RandomAccessFileOrArray(tiff_file.getAbsolutePath()); + int comps = TiffImage.getNumberOfPages(ra); + boolean adjustSize = false; + Document document = new Document(PageSize.A4); + if ("ORIGINAL".equals(getValue("pagesize"))) { + Image img = TiffImage.getTiffImage(ra, 1); + document.setPageSize(new Rectangle(img.scaledWidth(), img.scaledHeight())); + adjustSize = true; + } + else if ("LETTER".equals(getValue("pagesize"))) { + document.setPageSize(PageSize.LETTER); + } + PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(pdf_file)); + document.open(); + PdfContentByte cb = writer.getDirectContent(); + for (int c = 0; c < comps; ++c) { + Image img = TiffImage.getTiffImage(ra, c + 1); + if (img != null) { + if (adjustSize) { + document.setPageSize(new Rectangle(img.scaledWidth(), + img.scaledHeight())); + document.newPage(); + img.setAbsolutePosition(0, 0); + } + else { + if (img.scaledWidth() > 500 || img.scaledHeight() > 700) { + img.scaleToFit(500, 700); + } + img.setAbsolutePosition(20, 20); + document.newPage(); + document.add(new Paragraph(tiff_file + " - page " + (c + 1))); + } + cb.addImage(img); + System.out.println("Finished page " + (c + 1)); + } + } + ra.close(); + document.close(); + } catch (Exception e) { + JOptionPane.showMessageDialog(internalFrame, + e.getMessage(), + e.getClass().getName(), + JOptionPane.ERROR_MESSAGE); + System.err.println(e.getMessage()); + } + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#valueHasChanged(com.lowagie.tools.arguments.ToolArgument) + */ + public void valueHasChanged(ToolArgument arg) { + if (internalFrame == null) { + // if the internal frame is null, the tool was called from the commandline + return; + } + // represent the changes of the argument in the internal frame + } + + + /** + * Converts a tiff file to PDF. + * @param args + */ + public static void main(String[] args) { + Tiff2Pdf tool = new Tiff2Pdf(); + if (args.length < 2) { + System.err.println(tool.getUsage()); + } + tool.setArguments(args); + tool.execute(); + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#getDestPathPDF() + */ + protected File getDestPathPDF() throws InstantiationException { + return (File)getValue("destfile"); + } +} \ No newline at end of file diff --git a/src/main/java/com/lowagie/tools/plugins/TreeViewPDF.java b/src/main/java/com/lowagie/tools/plugins/TreeViewPDF.java new file mode 100644 index 0000000..0b55b8c --- /dev/null +++ b/src/main/java/com/lowagie/tools/plugins/TreeViewPDF.java @@ -0,0 +1,160 @@ +/* + * $Id: TreeViewPDF.java,v 1.2 2006/05/29 10:31:17 blowagie Exp $ + * $Name: $ + * + * Copyright 2005 by Anonymous. + * + * The contents of this file are subject to the Mozilla Public License Version 1.1 + * (the "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the License. + * + * The Original Code is 'iText, a free JAVA-PDF library'. + * + * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by + * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie. + * All Rights Reserved. + * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer + * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved. + * + * Contributor(s): all the names of the contributors are added in the source code + * where applicable. + * + * Alternatively, the contents of this file may be used under the terms of the + * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the + * provisions of LGPL are applicable instead of those above. If you wish to + * allow use of your version of this file only under the terms of the LGPL + * License and not to allow others to use your version of this file under + * the MPL, indicate your decision by deleting the provisions above and + * replace them with the notice and other provisions required by the LGPL. + * If you do not delete the provisions above, a recipient may use your version + * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE. + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the MPL as stated above or under the terms of the GNU + * Library General Public License as published by the Free Software Foundation; + * either version 2 of the License, or any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more + * details. + * + * If you didn't download this code from the following link, you should check if + * you aren't using an obsolete version: + * http://www.lowagie.com/iText/ + */ +package com.lowagie.tools.plugins; + +import java.io.File; + +import javax.swing.JOptionPane; + +import com.lowagie.tools.arguments.FileArgument; +import com.lowagie.tools.arguments.LabelAccessory; +import com.lowagie.tools.arguments.PdfFilter; +import com.lowagie.tools.arguments.ToolArgument; +import com.lowagie.tools.plugins.treeview.TreeViewInternalFrame; +import com.lowagie.tools.Toolbox; +import java.beans.*; + +/** + * Allows you to inspect an existing PDF file. + */ +public class TreeViewPDF + extends AbstractTool { + static { + addVersion("$Id: TreeViewPDF.java,v 1.2 2006/05/29 10:31:17 blowagie Exp $"); + } + + TreeViewInternalFrame ul; + FileArgument inputfile; + /** + * Constructs an TreeViewPDF object. + */ + public TreeViewPDF() { + inputfile = new FileArgument(this, "srcfile", + "The file you want to inspect", false, + new PdfFilter()); + inputfile.setLabel(new LabelAccessory()); + arguments.add(inputfile); + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#createFrame() + */ + protected void createFrame() { + ul = new TreeViewInternalFrame("Pdf Analysis", true, false, true); + internalFrame = ul; + internalFrame.setSize(500, 300); + internalFrame.setJMenuBar(getMenubar()); + inputfile.addPropertyChangeListener(ul); + System.out.println("=== Pdf Analysis OPENED ==="); + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#execute() + */ + public void execute() { + try { + if (getValue("srcfile") == null) { + throw new InstantiationException("You need to choose a sourcefile"); + } + + } + catch (Exception e) { + JOptionPane.showMessageDialog(internalFrame, + e.getMessage(), + e.getClass().getName(), + JOptionPane.ERROR_MESSAGE); + System.err.println(e.getMessage()); + } + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#valueHasChanged(com.lowagie.tools.arguments.ToolArgument) + */ + public void valueHasChanged(ToolArgument arg) { + if (internalFrame == null) { + // if the internal frame is null, the tool was called from the commandline + return; + } + // represent the changes of the argument in the internal frame + } + + /** + * Inspects an existing PDF file. + * @param args + */ + public static void main(String[] args) { + try { + Toolbox toolbox = new Toolbox(); + AbstractTool tool = toolbox.createFrame("TreeViewPDF"); + if (args.length > 1) { + System.err.println(tool.getUsage()); + } + tool.setArguments(args); + tool.execute(); + } + catch (PropertyVetoException ex) { + } + catch (ClassNotFoundException ex) { + } + catch (IllegalAccessException ex) { + } + catch (InstantiationException ex) { + } +// throw new RuntimeException("GUI only Application!"); + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#getDestPathPDF() + */ + protected File getDestPathPDF() throws InstantiationException { + throw new InstantiationException("There is no file to show."); + } + +} diff --git a/src/main/java/com/lowagie/tools/plugins/Txt2Pdf.java b/src/main/java/com/lowagie/tools/plugins/Txt2Pdf.java new file mode 100644 index 0000000..8a377fd --- /dev/null +++ b/src/main/java/com/lowagie/tools/plugins/Txt2Pdf.java @@ -0,0 +1,169 @@ +/* + * $Id: Txt2Pdf.java,v 1.9 2005/11/29 21:05:02 blowagie Exp $ + * $Name: $ + * + * Copyright 2005 by Bruno Lowagie. + * + * The contents of this file are subject to the Mozilla Public License Version 1.1 + * (the "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the License. + * + * The Original Code is 'iText, a free JAVA-PDF library'. + * + * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by + * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie. + * All Rights Reserved. + * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer + * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved. + * + * Contributor(s): all the names of the contributors are added in the source code + * where applicable. + * + * Alternatively, the contents of this file may be used under the terms of the + * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the + * provisions of LGPL are applicable instead of those above. If you wish to + * allow use of your version of this file only under the terms of the LGPL + * License and not to allow others to use your version of this file under + * the MPL, indicate your decision by deleting the provisions above and + * replace them with the notice and other provisions required by the LGPL. + * If you do not delete the provisions above, a recipient may use your version + * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE. + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the MPL as stated above or under the terms of the GNU + * Library General Public License as published by the Free Software Foundation; + * either version 2 of the License, or any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more + * details. + * + * If you didn't download this code from the following link, you should check if + * you aren't using an obsolete version: + * http://www.lowagie.com/iText/ + */ +package com.lowagie.tools.plugins; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileOutputStream; +import java.io.FileReader; + +import javax.swing.JInternalFrame; +import javax.swing.JOptionPane; + +import com.lowagie.text.Document; +import com.lowagie.text.Font; +import com.lowagie.text.FontFactory; +import com.lowagie.text.Paragraph; +import com.lowagie.text.Rectangle; +import com.lowagie.text.pdf.PdfWriter; +import com.lowagie.tools.arguments.FileArgument; +import com.lowagie.tools.arguments.OptionArgument; +import com.lowagie.tools.arguments.PageSizeArgument; +import com.lowagie.tools.arguments.PdfFilter; +import com.lowagie.tools.arguments.ToolArgument; + +/** + * Converts a monospaced txt file to a PDF file. + */ +public class Txt2Pdf extends AbstractTool { + + static { + addVersion("$Id: Txt2Pdf.java,v 1.9 2005/11/29 21:05:02 blowagie Exp $"); + } + /** + * Constructs a Txt2Pdf object. + */ + public Txt2Pdf() { + menuoptions = MENU_EXECUTE | MENU_EXECUTE_SHOW | MENU_EXECUTE_PRINT_SILENT; + arguments.add(new FileArgument(this, "srcfile", "The file you want to convert", false)); + arguments.add(new FileArgument(this, "destfile", "The file to which the converted text has to be written", true, new PdfFilter())); + PageSizeArgument oa1 = new PageSizeArgument(this, "pagesize", "Pagesize"); + arguments.add(oa1); + OptionArgument oa2 = new OptionArgument(this, "orientation", "Orientation of the page"); + oa2.addOption("Portrait", "PORTRAIT"); + oa2.addOption("Landscape", "LANDSCAPE"); + arguments.add(oa2); + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#createFrame() + */ + protected void createFrame() { + internalFrame = new JInternalFrame("Txt2Pdf", true, true, true); + internalFrame.setSize(300, 80); + internalFrame.setJMenuBar(getMenubar()); + System.out.println("=== Txt2Pdf OPENED ==="); + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#execute() + */ + public void execute() { + try { + String line = null; + Document document; + Font f; + Rectangle pagesize = (Rectangle)getValue("pagesize"); + if ("LANDSCAPE".equals(getValue("orientation"))) { + f = FontFactory.getFont(FontFactory.COURIER, 10); + document = new Document(pagesize.rotate(), 36, 9, 36, 36); + } + else { + f = FontFactory.getFont(FontFactory.COURIER, 11); + document = new Document(pagesize, 72, 36, 36, 36); + } + BufferedReader in = new BufferedReader(new FileReader((File)getValue("srcfile"))); + PdfWriter.getInstance(document, new FileOutputStream((File)getValue("destfile"))); + document.open(); + while ((line = in.readLine()) != null) { + document.add(new Paragraph(12, line, f)); + } + document.close(); + } catch (Exception e) { + JOptionPane.showMessageDialog(internalFrame, + e.getMessage(), + e.getClass().getName(), + JOptionPane.ERROR_MESSAGE); + System.err.println(e.getMessage()); + } + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#valueHasChanged(com.lowagie.tools.arguments.ToolArgument) + */ + public void valueHasChanged(ToolArgument arg) { + if (internalFrame == null) { + // if the internal frame is null, the tool was called from the commandline + return; + } + // represent the changes of the argument in the internal frame + } + + + /** + * Converts a monospaced txt file to a PDF file. + * @param args + */ + public static void main(String[] args) { + Txt2Pdf tool = new Txt2Pdf(); + if (args.length < 3) { + System.err.println(tool.getUsage()); + } + tool.setArguments(args); + tool.execute(); + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#getDestPathPDF() + */ + protected File getDestPathPDF() throws InstantiationException { + return (File)getValue("destfile"); + } +} diff --git a/src/main/java/com/lowagie/tools/plugins/Watermarker.java b/src/main/java/com/lowagie/tools/plugins/Watermarker.java new file mode 100644 index 0000000..e1e64f8 --- /dev/null +++ b/src/main/java/com/lowagie/tools/plugins/Watermarker.java @@ -0,0 +1,163 @@ +/* + * $Id: Watermarker.java,v 1.6 2006/03/20 14:30:33 blowagie Exp $ + * $Name: $ + * + * This code is free software. It may only be copied or modified + * if you include the following copyright notice: + * + * This class by Carsten hammer and Bruno Lowagie. + * Copyright (c) 2005 Carsten Hammer and Bruno Lowagie. + * + * This code is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * + * itext-questions@list.sourceforge.net + */ + +package com.lowagie.tools.plugins; + +import java.io.*; + +import javax.swing.*; + +import com.lowagie.text.*; +import com.lowagie.text.pdf.*; +import com.lowagie.tools.arguments.*; + +/** + * This tool lets you add a text watermark to all pages of a document. + */ +public class Watermarker extends AbstractTool { + + static { + addVersion("$Id: Watermarker.java,v 1.6 2006/03/20 14:30:33 blowagie Exp $"); + } + + /** + * This tool lets you add a text watermark to all pages of a document. + */ + public Watermarker() { + super(); + arguments.add(new FileArgument(this, "srcfile", + "The file you want to watermark", false, new PdfFilter())); + arguments.add(new ToolArgument(this, "watermark", "The text that can be used as watermark", String.class.getName())); + arguments.add(new ToolArgument(this, "fontsize", "The fontsize of the watermark text", String.class.getName())); + arguments.add(new ToolArgument(this, "opacity", "The opacity of the watermark text", String.class.getName())); + arguments.add(new FileArgument(this, "destfile", + "The file to which the watermarked PDF has to be written", + true, new PdfFilter())); + + } + + /** + * Creates the internal frame. + */ + protected void createFrame() { + internalFrame = new JInternalFrame("Watermark", true, false, true); + internalFrame.setSize(300, 80); + internalFrame.setJMenuBar(getMenubar()); + System.out.println("=== Watermark OPENED ==="); + } + + /** + * Executes the tool (in most cases this generates a PDF file). + */ + public void execute() { + try { + if (getValue("srcfile") == null) { + throw new InstantiationException( + "You need to choose a sourcefile"); + } + if (getValue("destfile") == null) { + throw new InstantiationException( + "You need to choose a destination file"); + } + if (getValue("watermark") == null) { + throw new InstantiationException( + "You need to add a text for the watermark"); + } + int fontsize = Integer.parseInt((String)getValue("fontsize")); + float opacity = Float.parseFloat((String)getValue("opacity")); + BaseFont bf = BaseFont.createFont("Helvetica", BaseFont.WINANSI, + false); + PdfReader reader = new PdfReader(((File) getValue("srcfile")) + .getAbsolutePath()); + int pagecount = reader.getNumberOfPages(); + PdfGState gs1 = new PdfGState(); + gs1.setFillOpacity(opacity); + String text = (String)getValue("watermark"); + PdfStamper stamp = new PdfStamper(reader, new FileOutputStream( + (File) getValue("destfile"))); + float txtwidth = bf.getWidthPoint(text, fontsize); + for (int i = 1; i <= pagecount; i++) { + PdfContentByte seitex = stamp.getOverContent(i); + Rectangle recc = reader.getCropBox(i); + float winkel = (float) Math.atan(recc.height() / recc.width()); + float m1 = (float) Math.cos(winkel); + float m2 = (float) -Math.sin(winkel); + float m3 = (float) Math.sin(winkel); + float m4 = (float) Math.cos(winkel); + float xoff = (float) (-Math.cos(winkel) * txtwidth / 2 - Math + .sin(winkel) + * fontsize / 2); + float yoff = (float) (Math.sin(winkel) * txtwidth / 2 - Math + .cos(winkel) + * fontsize / 2); + seitex.saveState(); + seitex.setGState(gs1); + seitex.beginText(); + seitex.setFontAndSize(bf, fontsize); + seitex.setTextMatrix(m1, m2, m3, m4, xoff + recc.width() / 2, + yoff + recc.height() / 2); + seitex.showText(text); + seitex.endText(); + seitex.restoreState(); + } + stamp.close(); + } catch (Exception e) { + JOptionPane.showMessageDialog(internalFrame, e.getMessage(), e + .getClass().getName(), JOptionPane.ERROR_MESSAGE); + System.err.println(e.getMessage()); + } + } + + /** + * Gets the PDF file that should be generated (or null if the output isn't a + * PDF file). + * + * @return the PDF file that should be generated + * @throws InstantiationException + */ + protected File getDestPathPDF() throws InstantiationException { + return (File) getValue("destfile"); + } + + /** + * Indicates that the value of an argument has changed. + * + * @param arg + * the argument that has changed + */ + public void valueHasChanged(ToolArgument arg) { + if (internalFrame == null) { + // if the internal frame is null, the tool was called from the + // commandline + return; + } + + } + + /** + * This methods helps you running this tool as a standalone application. + * @param args the srcfile, watermark text and destfile + */ + public static void main(String[] args) { + Watermarker watermarker = new Watermarker(); + if (args.length != 5) { + System.err.println(watermarker.getUsage()); + } + watermarker.setArguments(args); + watermarker.execute(); + } +} \ No newline at end of file diff --git a/src/main/java/com/lowagie/tools/plugins/XML2Bookmarks.java b/src/main/java/com/lowagie/tools/plugins/XML2Bookmarks.java new file mode 100644 index 0000000..9b23657 --- /dev/null +++ b/src/main/java/com/lowagie/tools/plugins/XML2Bookmarks.java @@ -0,0 +1,156 @@ +/* + * $Id: XML2Bookmarks.java,v 1.2 2006/02/03 08:30:56 blowagie Exp $ + * $Name: $ + * + * Copyright 2005 by Hans-Werner Hilse. + * + * The contents of this file are subject to the Mozilla Public License Version 1.1 + * (the "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the License. + * + * The Original Code is 'iText, a free JAVA-PDF library'. + * + * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by + * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie. + * All Rights Reserved. + * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer + * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved. + * + * Contributor(s): all the names of the contributors are added in the source code + * where applicable. + * + * Alternatively, the contents of this file may be used under the terms of the + * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the + * provisions of LGPL are applicable instead of those above. If you wish to + * allow use of your version of this file only under the terms of the LGPL + * License and not to allow others to use your version of this file under + * the MPL, indicate your decision by deleting the provisions above and + * replace them with the notice and other provisions required by the LGPL. + * If you do not delete the provisions above, a recipient may use your version + * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE. + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the MPL as stated above or under the terms of the GNU + * Library General Public License as published by the Free Software Foundation; + * either version 2 of the License, or any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more + * details. + * + * If you didn't download this code from the following link, you should check if + * you aren't using an obsolete version: + * http://www.lowagie.com/iText/ + */ +package com.lowagie.tools.plugins; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.util.List; + +import javax.swing.JInternalFrame; +import javax.swing.JOptionPane; + +import com.lowagie.text.pdf.PdfReader; +import com.lowagie.text.pdf.PdfStamper; +import com.lowagie.text.pdf.PdfWriter; +import com.lowagie.text.pdf.SimpleBookmark; +import com.lowagie.tools.arguments.FileArgument; +import com.lowagie.tools.arguments.PdfFilter; +import com.lowagie.tools.arguments.ToolArgument; + +/** + * Allows you to add bookmarks to an existing PDF file + */ +public class XML2Bookmarks extends AbstractTool { + + static { + addVersion("$Id: XML2Bookmarks.java,v 1.2 2006/02/03 08:30:56 blowagie Exp $"); + } + + /** + * Constructs an XML2Bookmarks object. + */ + public XML2Bookmarks() { + arguments.add(new FileArgument(this, "xmlfile", "the bookmarks in XML", false)); + arguments.add(new FileArgument(this, "pdffile", "the PDF to which you want to add bookmarks", false, new PdfFilter())); + arguments.add(new FileArgument(this, "destfile", "the resulting PDF", true, new PdfFilter())); + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#createFrame() + */ + protected void createFrame() { + internalFrame = new JInternalFrame("XML + PDF = PDF", true, true, true); + internalFrame.setSize(300, 80); + internalFrame.setJMenuBar(getMenubar()); + System.out.println("=== XML2Bookmarks OPENED ==="); + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#execute() + */ + public void execute() { + try { + if (getValue("xmlfile") == null) throw new InstantiationException("You need to choose an xml file"); + if (getValue("pdffile") == null) throw new InstantiationException("You need to choose a source PDF file"); + if (getValue("destfile") == null) throw new InstantiationException("You need to choose a destination PDF file"); + FileInputStream bmReader = new FileInputStream( (File) getValue("xmlfile") ); + List bookmarks = SimpleBookmark.importFromXML( bmReader ); + bmReader.close(); + PdfReader reader = new PdfReader(((File)getValue("pdffile")).getAbsolutePath()); + reader.consolidateNamedDestinations(); + int n = reader.getNumberOfPages(); + PdfStamper stamper = new PdfStamper(reader, new FileOutputStream((File)getValue("destfile"))); + stamper.setOutlines(bookmarks); + stamper.setViewerPreferences(reader.getViewerPreferences() | PdfWriter.PageModeUseOutlines); + stamper.close(); + } + catch(Exception e) { + e.printStackTrace(); + JOptionPane.showMessageDialog(internalFrame, + e.getMessage(), + e.getClass().getName(), + JOptionPane.ERROR_MESSAGE); + System.err.println(e.getMessage()); + } + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#valueHasChanged(com.lowagie.tools.arguments.ToolArgument) + */ + public void valueHasChanged(ToolArgument arg) { + if (internalFrame == null) { + // if the internal frame is null, the tool was called from the commandline + return; + } + // represent the changes of the argument in the internal frame + } + + /** + * Allows you to generate an index file in HTML containing Bookmarks to an existing PDF file. + * @param args + */ + public static void main(String[] args) { + XML2Bookmarks tool = new XML2Bookmarks(); + if (args.length < 3) { + System.err.println(tool.getUsage()); + } + tool.setArguments(args); + tool.execute(); + } + + /** + * @see com.lowagie.tools.plugins.AbstractTool#getDestPathPDF() + */ + protected File getDestPathPDF() throws InstantiationException { + return (File)getValue("destfile"); + } + +} diff --git a/src/main/java/com/lowagie/tools/plugins/treeview/AnalyzePDF.java b/src/main/java/com/lowagie/tools/plugins/treeview/AnalyzePDF.java new file mode 100644 index 0000000..5907f9a --- /dev/null +++ b/src/main/java/com/lowagie/tools/plugins/treeview/AnalyzePDF.java @@ -0,0 +1,507 @@ +/* + * $Id: AnalyzePDF.java,v 1.4 2006/05/30 09:11:45 blowagie Exp $ + * $Name: $ + * + * Copyright 2005 by Anonymous. + * + * The contents of this file are subject to the Mozilla Public License Version 1.1 + * (the "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the License. + * + * The Original Code is 'iText, a free JAVA-PDF library'. + * + * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by + * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie. + * All Rights Reserved. + * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer + * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved. + * + * Contributor(s): all the names of the contributors are added in the source code + * where applicable. + * + * Alternatively, the contents of this file may be used under the terms of the + * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the + * provisions of LGPL are applicable instead of those above. If you wish to + * allow use of your version of this file only under the terms of the LGPL + * License and not to allow others to use your version of this file under + * the MPL, indicate your decision by deleting the provisions above and + * replace them with the notice and other provisions required by the LGPL. + * If you do not delete the provisions above, a recipient may use your version + * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE. + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the MPL as stated above or under the terms of the GNU + * Library General Public License as published by the Free Software Foundation; + * either version 2 of the License, or any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more + * details. + * + * If you didn't download this code from the following link, you should check if + * you aren't using an obsolete version: + * http://www.lowagie.com/iText/ + */ +package com.lowagie.tools.plugins.treeview; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Vector; + +import javax.swing.event.TreeModelEvent; +import javax.swing.event.TreeModelListener; +import javax.swing.tree.DefaultMutableTreeNode; +import javax.swing.tree.TreeModel; +import javax.swing.tree.TreePath; + +import com.lowagie.text.pdf.*; +import java.util.Set; +import java.util.Iterator; +import javax.swing.table.TableModel; +import javax.swing.table.AbstractTableModel; + +public class AnalyzePDF + extends Thread implements TreeModel, ICommonAnalyzer { + DefaultMutableTreeNode root; + DefaultMutableTreeNode filenode; + int pagecount; + ProgressDialog progressdialog; + int numberofpages; + ArrayList pageInh = new ArrayList(); + private transient Vector treeModelListeners; + PdfReader reader; + + public AnalyzePDF(String infile, + com.lowagie.tools.plugins.treeview.ProgressDialog blubb) { + this.progressdialog = blubb; + try { + reader = new PdfReader(infile); + root = new SimpletextTreeNode("Dokument"); + filenode = new FileTreeNode(infile, reader); + root.add(filenode); + this.numberofpages = reader.getNumberOfPages(); + } + catch (IOException ex) { + } + pagecount = 0; + } + + public TableModel getXReftable() { + TableModel tm = new AbstractTableModel() { + public int getColumnCount() { + return 2; + } + + public int getRowCount() { + return reader.getXrefSize() - 1; + } + + public Object getValueAt(int rowIndex, int columnIndex) { + switch (columnIndex) { + case 0: + return new Integer(rowIndex + 1); + case 1: + PdfObject pdfob=reader.getPdfObject(rowIndex + 1); + if(pdfob.isStream()){ + return "Stream "+pdfob; + }else{ + return pdfob; + } + default: + return null; + } + } + + /** + * Returns the name of the column at columnIndex. + * + * @param columnIndex the index of the column + * @return the name of the column + * @todo Implement this javax.swing.table.TableModel method + */ + public String getColumnName(int columnIndex) { + switch (columnIndex) { + case 0: + return "XRefNr"; + case 1: + return "Object"; + default: + return null; + } + + } + }; + return tm; + } + + /** + * Walk down the Pagetree + * @param page PdfDictionary + * @param pdfreader PdfReader + * @param count_in_leaf int + * @param node DefaultMutableTreeNode + */ + protected void iteratePages(PdfDictionary page, PdfReader pdfreader, + DefaultMutableTreeNode node) { + DefaultMutableTreeNode leaf; + + PdfArray kidsPR = (PdfArray) PdfReader.getPdfObject(page.get(PdfName.KIDS)); + if (kidsPR == null) { + node.add(new Pagetreenode(page, pagecount, this, pdfreader)); + System.out.println("Page= " + (pagecount + 1)); + pageInh.add(pagecount, page); + pagecount++; + } + else { + leaf = new PagelistTreeNode(kidsPR); + node.add(leaf); + page.put(PdfName.TYPE, PdfName.PAGES); + ArrayList kids = kidsPR.getArrayList(); + for (int k = 0; k < kids.size(); ++k) { + PdfDictionary kid = (PdfDictionary) PdfReader.getPdfObject( ( + PRIndirectReference) kids.get(k)); + iteratePages(kid, pdfreader, leaf); + } + } + } + + protected void iterateOutlines(PdfDictionary outlines, PdfReader pdfreader, + DefaultMutableTreeNode node) { + DefaultMutableTreeNode leaf; + PdfDictionary kid = outlines; + do { + PdfString title = (PdfString) pdfreader.getPdfObject( + kid.get(PdfName.TITLE)); + leaf = new OutlinelistTreeNode(title, kid); + node.add(leaf); + PdfDictionary first = (PdfDictionary) PdfReader.getPdfObject( ( + PRIndirectReference) kid.get(PdfName.FIRST)); + if (first != null) { + iterateOutlines(first, pdfreader, leaf); + } + else { + PdfDictionary se = (PdfDictionary) PdfReader.getPdfObject( ( + PRIndirectReference) kid.get(new PdfName("SE"))); + if (se != null) { + iterateObjects(se, pdfreader, leaf); + } + PdfObject dest = (PdfObject) pdfreader.getPdfObject(kid.get(PdfName. + DEST)); + if (dest != null) { + iterateObjects(dest, pdfreader, leaf); + } + PdfObject a = (PdfObject) pdfreader.getPdfObject(kid.get(PdfName.A)); + if (a != null) { + iterateObjects(a, pdfreader, leaf); + } + } + } + while ( (kid = (PdfDictionary) pdfreader.getPdfObject(kid.get(PdfName.NEXT))) != null); + } + + /** + * Recursive investigate PDF Objecttree (other than pagetree objects!) + * @param pdfobj PdfObject + * @param pdfreader PdfReader + * @param node DefaultMutableTreeNode + */ + public void iterateObjects(PdfObject pdfobj, PdfReader pdfreader, + DefaultMutableTreeNode node) { + DefaultMutableTreeNode leaf; + if (pdfobj.isDictionary()) { + leaf = new DictionaryTreeNode("PdfDictionary " + pdfobj, + (PdfDictionary) pdfobj); + node.add(leaf); + Set s = ( (PdfDictionary) pdfobj).getKeys(); + Iterator it = s.iterator(); + int i = 0; + while (it.hasNext()) { + i++; + Object obj = it.next(); +// System.out.println("Feld:" + obj); + + PdfObject value = PdfReader.getPdfObject( ( (PdfDictionary) pdfobj).get( ( + PdfName) obj)); +// System.out.println("Value:" + value); + SimpletextTreeNode sttn = new SimpletextTreeNode(obj + " " + value); + leaf.add(sttn); + if (obj.equals(PdfName.PARENT)) { + continue; + } + if (value != null) { + iterateObjects(value, pdfreader, sttn); + } + } + } + else if (pdfobj.isArray()) { + leaf = new ArrayTreeNode("PdfArray " + pdfobj, (PdfArray) pdfobj); + node.add(leaf); + ArrayList kids = ( (PdfArray) pdfobj).getArrayList(); + for (int k = 0; k < kids.size(); ++k) { + PdfObject curkid = (PdfObject) kids.get(k); + if (curkid.isIndirect()) { + PdfObject kid = PdfReader.getPdfObject( ( + PRIndirectReference) kids.get(k)); + if (kid != null) { + iterateObjects(kid, pdfreader, leaf); + } + } + else if (curkid.isNumber()) { + + } + else { + PdfObject kid = (PdfObject) kids.get(k); + iterateObjects(kid, pdfreader, leaf); + } + } + } + else if (pdfobj.isIndirect()) { + leaf = new SimpletextTreeNode("PRIndirectReference " + pdfobj); + node.add(leaf); + PdfObject target = PdfReader.getPdfObject( ( + PRIndirectReference) pdfobj); + if (target != null) { + iterateObjects(target, pdfreader, leaf); + } + } + else if (pdfobj.isBoolean()) { +// leaf = new SimpletextTreeNode("Boolean " + pdfobj); +// node.add(leaf); + } + else if (pdfobj.isName()) { +// leaf = new SimpletextTreeNode("Name " + pdfobj); +// node.add(leaf); + } + else if (pdfobj.isNull()) { +// leaf = new SimpletextTreeNode("Null " + pdfobj); +// node.add(leaf); + } + else if (pdfobj.isNumber()) { +// leaf = new SimpletextTreeNode("Number " + pdfobj); +// node.add(leaf); + } + else if (pdfobj.isString()) { +// leaf = new SimpletextTreeNode("String " + pdfobj); +// node.add(leaf); + } + else if (pdfobj.isStream()) { + leaf = new TextpaneTreeNode(pdfobj,"Stream"); + node.add(leaf); + leaf = new DictionaryTreeNode("PdfDictionary " + pdfobj, + (PdfDictionary) pdfobj); + node.add(leaf); + Set s = ( (PdfDictionary) pdfobj).getKeys(); + Iterator it = s.iterator(); + int i = 0; + while (it.hasNext()) { + i++; + Object obj = it.next(); +// System.out.println("Feld:" + obj); + + PdfObject value = PdfReader.getPdfObject( ( (PdfDictionary) pdfobj).get( ( + PdfName) obj)); +// System.out.println("Value:" + value); + SimpletextTreeNode sttn = new SimpletextTreeNode(obj + " " + value); + leaf.add(sttn); + if (obj.equals(PdfName.PARENT)) { + continue; + } + if (value != null) { + iterateObjects(value, pdfreader, sttn); + } + } + + } + + else { + leaf = new SimpletextTreeNode("Unknown " + pdfobj); + node.add(leaf); + } + + } + + /** + * Returns the root of the tree. + * + * @return the root of the tree + * @todo Diese javax.swing.tree.TreeModel-Methode implementieren + */ + public Object getRoot() { + return root; + } + + /** + * Returns the child of parent at index index in the + * parent's child array. + * + * @param parent a node in the tree, obtained from this data source + * @param index int + * @return the child of parent at index index + * @todo Diese javax.swing.tree.TreeModel-Methode implementieren + */ + public Object getChild(Object parent, int index) { + DefaultMutableTreeNode node = (DefaultMutableTreeNode) parent; + return node.getChildAt(index); + } + + /** + * Returns the number of children of parent. + * + * @param parent a node in the tree, obtained from this data source + * @return the number of children of the node parent + * @todo Diese javax.swing.tree.TreeModel-Methode implementieren + */ + public int getChildCount(Object parent) { + DefaultMutableTreeNode node = (DefaultMutableTreeNode) parent; + return node.getChildCount(); + } + + /** + * Returns true if node is a leaf. + * + * @param node a node in the tree, obtained from this data source + * @return true if node is a leaf + * @todo Diese javax.swing.tree.TreeModel-Methode implementieren + */ + public boolean isLeaf(Object node) { + DefaultMutableTreeNode leaf = (DefaultMutableTreeNode) node; + return leaf.isLeaf(); + } + + /** + * Messaged when the user has altered the value for the item identified by + * path to newValue. + * + * @param path path to the node that the user has altered + * @param newValue the new value from the TreeCellEditor + * @todo Diese javax.swing.tree.TreeModel-Methode implementieren + */ + public void valueForPathChanged(TreePath path, Object newValue) { + throw new RuntimeException("Manipulation of objecttree not yet supported!"); + } + + /** + * Returns the index of child in parent. + * + * @param parent a note in the tree, obtained from this data source + * @param child the node we are interested in + * @return the index of the child in the parent, or -1 if either + * child or parent are null + * @todo Diese javax.swing.tree.TreeModel-Methode implementieren + */ + public int getIndexOfChild(Object parent, Object child) { + DefaultMutableTreeNode parentobj = (DefaultMutableTreeNode) parent; + DefaultMutableTreeNode childobj = (DefaultMutableTreeNode) child; + return parentobj.getIndex(childobj); + } + + public synchronized void removeTreeModelListener(TreeModelListener l) { + if (treeModelListeners != null && treeModelListeners.contains(l)) { + Vector v = (Vector) treeModelListeners.clone(); + v.removeElement(l); + treeModelListeners = v; + } + } + + public synchronized void addTreeModelListener(TreeModelListener l) { + Vector v = treeModelListeners == null ? new Vector(2) : + (Vector) treeModelListeners.clone(); + if (!v.contains(l)) { + v.addElement(l); + treeModelListeners = v; + } + } + + protected void fireTreeNodesChanged(TreeModelEvent e) { + if (treeModelListeners != null) { + Vector listeners = treeModelListeners; + int count = listeners.size(); + for (int i = 0; i < count; i++) { + ( (TreeModelListener) listeners.elementAt(i)).treeNodesChanged(e); + } + } + } + + protected void fireTreeNodesInserted(TreeModelEvent e) { + if (treeModelListeners != null) { + Vector listeners = treeModelListeners; + int count = listeners.size(); + for (int i = 0; i < count; i++) { + ( (TreeModelListener) listeners.elementAt(i)).treeNodesInserted(e); + } + } + } + + protected void fireTreeNodesRemoved(TreeModelEvent e) { + if (treeModelListeners != null) { + Vector listeners = treeModelListeners; + int count = listeners.size(); + for (int i = 0; i < count; i++) { + ( (TreeModelListener) listeners.elementAt(i)).treeNodesRemoved(e); + } + } + } + + protected void fireTreeStructureChanged(TreeModelEvent e) { + if (treeModelListeners != null) { + Vector listeners = treeModelListeners; + int count = listeners.size(); + for (int i = 0; i < count; i++) { + ( (TreeModelListener) listeners.elementAt(i)).treeStructureChanged(e); + } + } + } + + /** + * When an object implementing interface Runnable is used to + * create a thread, starting the thread causes the object's run + * method to be called in that separately executing thread. + * + * @todo Diese java.lang.Runnable-Methode implementieren + */ + public void run() { + try { + PdfDictionary catalog = reader.getCatalog(); + PdfDictionary rootPages = (PdfDictionary) PdfReader.getPdfObject( + catalog.get(PdfName.PAGES)); + DefaultMutableTreeNode rootPagesGUI = new SimpletextTreeNode("Pagetree " + + rootPages); + filenode.add(rootPagesGUI); + iteratePages(rootPages, reader, rootPagesGUI); + + PdfDictionary rootOutlines = (PdfDictionary) PdfReader.getPdfObject( + catalog.get(PdfName.OUTLINES)); + if (rootOutlines != null) { + DefaultMutableTreeNode outlinetree = new SimpletextTreeNode( + "Outlinetree " + rootOutlines); + filenode.add(outlinetree); + PdfObject firstindref = rootOutlines.get(PdfName.FIRST); + if (firstindref != null) { + PdfDictionary first = (PdfDictionary) PdfReader.getPdfObject( ( + PRIndirectReference) firstindref); + if (first != null) { + iterateOutlines(first, reader, outlinetree); + } + } + } + System.out.println(" Pagecount= " + pagecount); + progressdialog.setVisible(false); + } + catch (Exception e) { + e.printStackTrace(System.out); + } + } + + public int getPagecount() { + return pagecount; + } + + public void updatecount() { + progressdialog.setAktuelleseite(getPagecount()); + } +} diff --git a/src/main/java/com/lowagie/tools/plugins/treeview/ArrayTreeNode.java b/src/main/java/com/lowagie/tools/plugins/treeview/ArrayTreeNode.java new file mode 100644 index 0000000..07ec49c --- /dev/null +++ b/src/main/java/com/lowagie/tools/plugins/treeview/ArrayTreeNode.java @@ -0,0 +1,107 @@ +/* + * $Id: ArrayTreeNode.java,v 1.2 2006/05/30 09:13:36 blowagie Exp $ + * $Name: $ + * + * Copyright 2005 by Anonymous. + * + * The contents of this file are subject to the Mozilla Public License Version 1.1 + * (the "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the License. + * + * The Original Code is 'iText, a free JAVA-PDF library'. + * + * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by + * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie. + * All Rights Reserved. + * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer + * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved. + * + * Contributor(s): all the names of the contributors are added in the source code + * where applicable. + * + * Alternatively, the contents of this file may be used under the terms of the + * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the + * provisions of LGPL are applicable instead of those above. If you wish to + * allow use of your version of this file only under the terms of the LGPL + * License and not to allow others to use your version of this file under + * the MPL, indicate your decision by deleting the provisions above and + * replace them with the notice and other provisions required by the LGPL. + * If you do not delete the provisions above, a recipient may use your version + * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE. + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the MPL as stated above or under the terms of the GNU + * Library General Public License as published by the Free Software Foundation; + * either version 2 of the License, or any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more + * details. + * + * If you didn't download this code from the following link, you should check if + * you aren't using an obsolete version: + * http://www.lowagie.com/iText/ + */ +package com.lowagie.tools.plugins.treeview; + +import com.lowagie.text.pdf.PdfArray; +import java.util.ArrayList; +import javax.swing.Icon; +import javax.swing.ImageIcon; + +/** + *

Title:

+ * + *

Description:

+ * + *

Copyright: Copyright (c) 2005

+ * + *

Company:

+ * + * @author not attributable + * @version 1.0 + */ +public class ArrayTreeNode + extends UpdateableTreeNode { + PdfArray arr; + + public ArrayTreeNode(Object userObject,PdfArray arr) { + super(userObject); + this.arr=arr; + } + + public ArrayTreeNode(Object userObject, boolean allowchildren) { + super(userObject, allowchildren); + } + + /** + * updateview + * + * @param updateobject IUpdatenodeview + * @todo Implement this com.lowagie.tools.plugins.treeview.UpdateableTreeNode method + */ + public void updateview(IUpdatenodeview updateobject) { + StringBuffer sb = new StringBuffer(); + sb.append(""); + sb.append("

"); + sb.append(this.userObject); + sb.append("

"); + ArrayList arl = arr.getArrayList(); + for (int i = 0; i < arl.size(); i++) { + sb.append("

"); + sb.append(" " + arl.get(i).toString()); + sb.append("

"); + } + sb.append(""); + updateobject.showvalues(sb.toString()); + } + public Icon getIcon(){ + return new ImageIcon(TreeViewInternalFrame.class.getResource( + "array.gif")); + } +} diff --git a/src/main/java/com/lowagie/tools/plugins/treeview/DictionaryTreeNode.java b/src/main/java/com/lowagie/tools/plugins/treeview/DictionaryTreeNode.java new file mode 100644 index 0000000..2fc6b4e --- /dev/null +++ b/src/main/java/com/lowagie/tools/plugins/treeview/DictionaryTreeNode.java @@ -0,0 +1,112 @@ +/* + * $Id: DictionaryTreeNode.java,v 1.2 2006/05/30 09:13:36 blowagie Exp $ + * $Name: $ + * + * Copyright 2005 by Anonymous. + * + * The contents of this file are subject to the Mozilla Public License Version 1.1 + * (the "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the License. + * + * The Original Code is 'iText, a free JAVA-PDF library'. + * + * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by + * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie. + * All Rights Reserved. + * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer + * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved. + * + * Contributor(s): all the names of the contributors are added in the source code + * where applicable. + * + * Alternatively, the contents of this file may be used under the terms of the + * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the + * provisions of LGPL are applicable instead of those above. If you wish to + * allow use of your version of this file only under the terms of the LGPL + * License and not to allow others to use your version of this file under + * the MPL, indicate your decision by deleting the provisions above and + * replace them with the notice and other provisions required by the LGPL. + * If you do not delete the provisions above, a recipient may use your version + * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE. + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the MPL as stated above or under the terms of the GNU + * Library General Public License as published by the Free Software Foundation; + * either version 2 of the License, or any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more + * details. + * + * If you didn't download this code from the following link, you should check if + * you aren't using an obsolete version: + * http://www.lowagie.com/iText/ + */ + +package com.lowagie.tools.plugins.treeview; + +import java.util.Set; +import java.util.Iterator; +import com.lowagie.text.pdf.PdfDictionary; +import javax.swing.Icon; +import javax.swing.ImageIcon; + +/** + *

Title:

+ * + *

Description:

+ * + *

Copyright: Copyright (c) 2005

+ * + *

Company:

+ * + * @author not attributable + * @version 1.0 + */ +public class DictionaryTreeNode + extends UpdateableTreeNode { + PdfDictionary dictionary; + + public DictionaryTreeNode(Object userObject, PdfDictionary dictionary) { + super(userObject); + this.dictionary = dictionary; + } + + public DictionaryTreeNode(Object userObject, boolean allowchildren) { + super(userObject, allowchildren); + } + + /** + * updateview + * + * @param updateobject IUpdatenodeview + * @todo Implement this com.lowagie.tools.plugins.treeview.UpdateableTreeNode method + */ + public void updateview(IUpdatenodeview updateobject) { + StringBuffer sb = new StringBuffer(); + sb.append(""); + sb.append("

"); + sb.append(this.userObject); + sb.append("

"); + Set set = dictionary.getKeys(); + Iterator it = set.iterator(); + while (it.hasNext()) { + sb.append("

"); + sb.append("Key " + it.next().toString()); + sb.append("

"); + } + sb.append(""); + updateobject.showvalues(sb.toString()); + } + + public Icon getIcon(){ + return new ImageIcon(TreeViewInternalFrame.class.getResource( + "dictionary.gif")); + } + +} diff --git a/src/main/java/com/lowagie/tools/plugins/treeview/FileTreeNode.java b/src/main/java/com/lowagie/tools/plugins/treeview/FileTreeNode.java new file mode 100644 index 0000000..85e83a6 --- /dev/null +++ b/src/main/java/com/lowagie/tools/plugins/treeview/FileTreeNode.java @@ -0,0 +1,86 @@ +/* + * $Id: FileTreeNode.java,v 1.3 2006/05/30 09:13:36 blowagie Exp $ + * $Name: $ + * + * Copyright 2005 by Anonymous. + * + * The contents of this file are subject to the Mozilla Public License Version 1.1 + * (the "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the License. + * + * The Original Code is 'iText, a free JAVA-PDF library'. + * + * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by + * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie. + * All Rights Reserved. + * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer + * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved. + * + * Contributor(s): all the names of the contributors are added in the source code + * where applicable. + * + * Alternatively, the contents of this file may be used under the terms of the + * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the + * provisions of LGPL are applicable instead of those above. If you wish to + * allow use of your version of this file only under the terms of the LGPL + * License and not to allow others to use your version of this file under + * the MPL, indicate your decision by deleting the provisions above and + * replace them with the notice and other provisions required by the LGPL. + * If you do not delete the provisions above, a recipient may use your version + * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE. + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the MPL as stated above or under the terms of the GNU + * Library General Public License as published by the Free Software Foundation; + * either version 2 of the License, or any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more + * details. + * + * If you didn't download this code from the following link, you should check if + * you aren't using an obsolete version: + * http://www.lowagie.com/iText/ + */ + +package com.lowagie.tools.plugins.treeview; + +import com.lowagie.text.pdf.*; +import javax.swing.Icon; +import javax.swing.ImageIcon; + +public class FileTreeNode + extends UpdateableTreeNode { + + PdfReader reader; + public FileTreeNode(Object p0, PdfReader reader) { + super(p0,true); + this.reader = reader; + } + + public void updateview(IUpdatenodeview updateobject) { + StringBuffer sb = new StringBuffer(); + sb.append(""); + sb.append("

"); + sb.append(this.userObject); + sb.append("

"); + sb.append("

"); + sb.append("PDF Version 1." + this.reader.getPdfVersion()); + sb.append("

"); + sb.append("

"); + sb.append("Number of Pages: " + this.reader.getNumberOfPages()); + sb.append("

"); + sb.append(""); + updateobject.showvalues(sb.toString()); + } + public Icon getIcon(){ + return new ImageIcon(TreeViewInternalFrame.class.getResource( + "icon16.gif")); + } + +} diff --git a/src/main/java/com/lowagie/tools/plugins/treeview/ICommonAnalyzer.java b/src/main/java/com/lowagie/tools/plugins/treeview/ICommonAnalyzer.java new file mode 100644 index 0000000..17093f0 --- /dev/null +++ b/src/main/java/com/lowagie/tools/plugins/treeview/ICommonAnalyzer.java @@ -0,0 +1,79 @@ +/* + * $Id: ICommonAnalyzer.java,v 1.2 2005/12/06 13:20:39 blowagie Exp $ + * $Name: $ + * + * Copyright 2005 by Anonymous. + * + * The contents of this file are subject to the Mozilla Public License Version 1.1 + * (the "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the License. + * + * The Original Code is 'iText, a free JAVA-PDF library'. + * + * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by + * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie. + * All Rights Reserved. + * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer + * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved. + * + * Contributor(s): all the names of the contributors are added in the source code + * where applicable. + * + * Alternatively, the contents of this file may be used under the terms of the + * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the + * provisions of LGPL are applicable instead of those above. If you wish to + * allow use of your version of this file only under the terms of the LGPL + * License and not to allow others to use your version of this file under + * the MPL, indicate your decision by deleting the provisions above and + * replace them with the notice and other provisions required by the LGPL. + * If you do not delete the provisions above, a recipient may use your version + * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE. + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the MPL as stated above or under the terms of the GNU + * Library General Public License as published by the Free Software Foundation; + * either version 2 of the License, or any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more + * details. + * + * If you didn't download this code from the following link, you should check if + * you aren't using an obsolete version: + * http://www.lowagie.com/iText/ + */ + +package com.lowagie.tools.plugins.treeview; + +import javax.swing.tree.DefaultMutableTreeNode; + +import com.lowagie.text.pdf.PdfObject; +import com.lowagie.text.pdf.PdfReader; +/** + * + *

Title:

+ * + *

Description:

+ * + *

Copyright: Copyright (c) 2005

+ * + *

Company:

+ * + * @author not attributable + * @version 1.0 + */ +public interface ICommonAnalyzer { + /** + * + * @param pdfobj PdfObject + * @param pdfreader PdfReader + * @param node DefaultMutableTreeNode + */ + void iterateObjects(PdfObject pdfobj, PdfReader pdfreader, + DefaultMutableTreeNode node); +} diff --git a/src/main/java/com/lowagie/tools/plugins/treeview/IUpdatenodeview.java b/src/main/java/com/lowagie/tools/plugins/treeview/IUpdatenodeview.java new file mode 100644 index 0000000..01a3b45 --- /dev/null +++ b/src/main/java/com/lowagie/tools/plugins/treeview/IUpdatenodeview.java @@ -0,0 +1,59 @@ +/* + * $Id: IUpdatenodeview.java,v 1.1 2005/11/29 21:05:02 blowagie Exp $ + * $Name: $ + * + * Copyright 2005 by Anonymous. + * + * The contents of this file are subject to the Mozilla Public License Version 1.1 + * (the "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the License. + * + * The Original Code is 'iText, a free JAVA-PDF library'. + * + * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by + * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie. + * All Rights Reserved. + * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer + * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved. + * + * Contributor(s): all the names of the contributors are added in the source code + * where applicable. + * + * Alternatively, the contents of this file may be used under the terms of the + * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the + * provisions of LGPL are applicable instead of those above. If you wish to + * allow use of your version of this file only under the terms of the LGPL + * License and not to allow others to use your version of this file under + * the MPL, indicate your decision by deleting the provisions above and + * replace them with the notice and other provisions required by the LGPL. + * If you do not delete the provisions above, a recipient may use your version + * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE. + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the MPL as stated above or under the terms of the GNU + * Library General Public License as published by the Free Software Foundation; + * either version 2 of the License, or any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more + * details. + * + * If you didn't download this code from the following link, you should check if + * you aren't using an obsolete version: + * http://www.lowagie.com/iText/ + */ + +package com.lowagie.tools.plugins.treeview; + +public interface IUpdatenodeview { + public void showempty(); + + public void showtext(String text); + + public void showvalues(String text); +} diff --git a/src/main/java/com/lowagie/tools/plugins/treeview/OutlinelistTreeNode.java b/src/main/java/com/lowagie/tools/plugins/treeview/OutlinelistTreeNode.java new file mode 100644 index 0000000..2fd78e5 --- /dev/null +++ b/src/main/java/com/lowagie/tools/plugins/treeview/OutlinelistTreeNode.java @@ -0,0 +1,98 @@ +/* + * $Id: OutlinelistTreeNode.java,v 1.2 2006/05/30 09:13:36 blowagie Exp $ + * $Name: $ + * + * Copyright 2005 by Anonymous. + * + * The contents of this file are subject to the Mozilla Public License Version 1.1 + * (the "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the License. + * + * The Original Code is 'iText, a free JAVA-PDF library'. + * + * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by + * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie. + * All Rights Reserved. + * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer + * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved. + * + * Contributor(s): all the names of the contributors are added in the source code + * where applicable. + * + * Alternatively, the contents of this file may be used under the terms of the + * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the + * provisions of LGPL are applicable instead of those above. If you wish to + * allow use of your version of this file only under the terms of the LGPL + * License and not to allow others to use your version of this file under + * the MPL, indicate your decision by deleting the provisions above and + * replace them with the notice and other provisions required by the LGPL. + * If you do not delete the provisions above, a recipient may use your version + * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE. + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the MPL as stated above or under the terms of the GNU + * Library General Public License as published by the Free Software Foundation; + * either version 2 of the License, or any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more + * details. + * + * If you didn't download this code from the following link, you should check if + * you aren't using an obsolete version: + * http://www.lowagie.com/iText/ + */ + +package com.lowagie.tools.plugins.treeview; + +import java.util.ArrayList; + +import com.lowagie.text.pdf.PdfArray; +import com.lowagie.text.pdf.PdfDictionary; +import java.util.Set; +import java.util.Iterator; +import javax.swing.Icon; +import javax.swing.ImageIcon; + +public class OutlinelistTreeNode + extends UpdateableTreeNode { + private com.lowagie.text.pdf.PdfDictionary dictionary; + public OutlinelistTreeNode(Object userObject, PdfDictionary dictionary) { + super(userObject); + this.dictionary = dictionary; + } + + public OutlinelistTreeNode(PdfDictionary dictionary) { + super("Outlinenode " + dictionary); + this.dictionary = dictionary; + } + + public void updateview(IUpdatenodeview updateobject) { + StringBuffer sb = new StringBuffer(); + sb.append(""); + sb.append("

"); + sb.append(this.userObject); + sb.append("

"); + Set set = dictionary.getKeys(); + Iterator it = set.iterator(); + while (it.hasNext()) { + sb.append("

"); + sb.append("Key " + it.next().toString()); + sb.append("

"); + } + sb.append(""); + updateobject.showvalues(sb.toString()); + + } + + public Icon getIcon(){ + return new ImageIcon(TreeViewInternalFrame.class.getResource( + "bookmarks.gif")); + } + +} diff --git a/src/main/java/com/lowagie/tools/plugins/treeview/PagelistTreeNode.java b/src/main/java/com/lowagie/tools/plugins/treeview/PagelistTreeNode.java new file mode 100644 index 0000000..928fefa --- /dev/null +++ b/src/main/java/com/lowagie/tools/plugins/treeview/PagelistTreeNode.java @@ -0,0 +1,87 @@ +/* + * $Id: PagelistTreeNode.java,v 1.2 2006/05/30 09:13:36 blowagie Exp $ + * $Name: $ + * + * Copyright 2005 by Anonymous. + * + * The contents of this file are subject to the Mozilla Public License Version 1.1 + * (the "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the License. + * + * The Original Code is 'iText, a free JAVA-PDF library'. + * + * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by + * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie. + * All Rights Reserved. + * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer + * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved. + * + * Contributor(s): all the names of the contributors are added in the source code + * where applicable. + * + * Alternatively, the contents of this file may be used under the terms of the + * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the + * provisions of LGPL are applicable instead of those above. If you wish to + * allow use of your version of this file only under the terms of the LGPL + * License and not to allow others to use your version of this file under + * the MPL, indicate your decision by deleting the provisions above and + * replace them with the notice and other provisions required by the LGPL. + * If you do not delete the provisions above, a recipient may use your version + * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE. + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the MPL as stated above or under the terms of the GNU + * Library General Public License as published by the Free Software Foundation; + * either version 2 of the License, or any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more + * details. + * + * If you didn't download this code from the following link, you should check if + * you aren't using an obsolete version: + * http://www.lowagie.com/iText/ + */ + +package com.lowagie.tools.plugins.treeview; + +import java.util.ArrayList; + +import com.lowagie.text.pdf.PdfArray; +import javax.swing.Icon; +import javax.swing.ImageIcon; + +public class PagelistTreeNode + extends UpdateableTreeNode { + private com.lowagie.text.pdf.PdfArray pdfarray; + public PagelistTreeNode(PdfArray pdfarray) { + super("Pagesnode " + pdfarray); + this.pdfarray = pdfarray; + } + + public void updateview(IUpdatenodeview updateobject) { + StringBuffer sb = new StringBuffer(); + sb.append(""); + sb.append("

"); + sb.append(this.userObject); + sb.append("

"); + ArrayList arl = pdfarray.getArrayList(); + for (int i = 0; i < arl.size(); i++) { + sb.append("

"); + sb.append(" " + arl.get(i).toString()); + sb.append("

"); + } + sb.append(""); + updateobject.showvalues(sb.toString()); + } + + public Icon getIcon(){ + return new ImageIcon(TreeViewInternalFrame.class.getResource( + "pageonly.gif")); + } +} diff --git a/src/main/java/com/lowagie/tools/plugins/treeview/Pagetreenode.java b/src/main/java/com/lowagie/tools/plugins/treeview/Pagetreenode.java new file mode 100644 index 0000000..4cbcfda --- /dev/null +++ b/src/main/java/com/lowagie/tools/plugins/treeview/Pagetreenode.java @@ -0,0 +1,208 @@ +/* + * $Id: Pagetreenode.java,v 1.3 2006/05/30 09:13:36 blowagie Exp $ + * $Name: $ + * + * Copyright 2005 by Anonymous. + * + * The contents of this file are subject to the Mozilla Public License Version 1.1 + * (the "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the License. + * + * The Original Code is 'iText, a free JAVA-PDF library'. + * + * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by + * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie. + * All Rights Reserved. + * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer + * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved. + * + * Contributor(s): all the names of the contributors are added in the source code + * where applicable. + * + * Alternatively, the contents of this file may be used under the terms of the + * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the + * provisions of LGPL are applicable instead of those above. If you wish to + * allow use of your version of this file only under the terms of the LGPL + * License and not to allow others to use your version of this file under + * the MPL, indicate your decision by deleting the provisions above and + * replace them with the notice and other provisions required by the LGPL. + * If you do not delete the provisions above, a recipient may use your version + * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE. + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the MPL as stated above or under the terms of the GNU + * Library General Public License as published by the Free Software Foundation; + * either version 2 of the License, or any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more + * details. + * + * If you didn't download this code from the following link, you should check if + * you aren't using an obsolete version: + * http://www.lowagie.com/iText/ + */ + +package com.lowagie.tools.plugins.treeview; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.Set; + +import javax.swing.tree.DefaultMutableTreeNode; + +import com.lowagie.text.Rectangle; +import com.lowagie.text.pdf.*; +import javax.swing.Icon; +import javax.swing.ImageIcon; + +public class Pagetreenode + extends UpdateableTreeNode { + private com.lowagie.text.pdf.PdfDictionary dictionary; + + private int seitennummer; + private float width; + private float height; + + public Pagetreenode(com.lowagie.text.pdf.PdfDictionary page, int seitennummer, + ICommonAnalyzer pageanalyzer, PdfReader pdfreader) { + super(); + this.dictionary = page; + this.seitennummer = seitennummer; + DefaultMutableTreeNode info; + + PdfArray arr = (PdfArray) page.get(PdfName.MEDIABOX); + float curwidth = 0; + float curheight = 0; + if (arr != null) { + ArrayList arl = arr.getArrayList(); + curwidth = Float.parseFloat(arl.get(2).toString()); + curheight = Float.parseFloat(arl.get(3).toString()); + info = new SimpletextTreeNode(PdfName.MEDIABOX + " " + curwidth + "*" + + curheight); + this.add(info); + + } + PdfArray arrcrop = (PdfArray) page.get(PdfName.CROPBOX); + float curwidthcrop = 0; + float curheightcrop = 0; + if (arrcrop != null) { + ArrayList arl = arrcrop.getArrayList(); + curwidthcrop = Float.parseFloat(arl.get(2).toString()); + curheightcrop = Float.parseFloat(arl.get(3).toString()); + info = new SimpletextTreeNode(PdfName.CROPBOX + " " + curwidthcrop + "*" + + curheightcrop); + this.add(info); + + } + + PdfNumber rotation = (PdfNumber) PdfReader.getPdfObject(page.get(PdfName. + ROTATE)); + + if (rotation == null) { + System.out.println("Rotation missing"); + rotation = new PdfNumber(0); + } + else { + info = new SimpletextTreeNode(PdfName. + ROTATE + " " + rotation); + this.add(info); + } + Rectangle rect = new Rectangle(curwidthcrop, + curheightcrop); + + if ( (rotation.floatValue() == 90) || (rotation.floatValue() == 270)) { + rect = rect.rotate(); + } + + width = rect.width(); + height = rect.height(); + + // ??? Dont want a backreference! PdfDictionary parent = (PdfDictionary) PdfReader.getPdfObject(page.get(PdfName.PARENT)); + PdfArray dict = (PdfArray) PdfReader.getPdfObject(page.get(PdfName.ANNOTS)); + if (dict != null) { + this.add(new SimpletextTreeNode(PdfName.ANNOTS + " " + dict.length())); + SimpletextTreeNode sttn = new SimpletextTreeNode(PdfName.ANNOTS + " " + + dict.type()); + this.add(sttn); + pageanalyzer.iterateObjects(dict, pdfreader, sttn); + + } + PdfObject reso = PdfReader.getPdfObject(page.get(PdfName.RESOURCES)); + if (reso != null) { + SimpletextTreeNode sttn = new SimpletextTreeNode(PdfName.RESOURCES + " " + + reso.type()); + this.add(sttn); + pageanalyzer.iterateObjects(reso, pdfreader, sttn); + } + + PdfObject contents = PdfReader.getPdfObject(page.get(PdfName.CONTENTS)); + if (contents != null) { + this.add(new TextpaneTreeNode(contents,"Content")); + + if (contents.isStream()) { + PRStream prstr = (PRStream) contents; + + Set s = prstr.getKeys(); + Iterator it = s.iterator(); + + while (it.hasNext()) { + Object obj = it.next(); + System.out.println("Field:" + obj); + + Object value = PdfReader.getPdfObject(prstr.get( (PdfName) obj)); + System.out.println("Value:" + value); + } + } + } + } + + public int getSeitennummer() { + return seitennummer; + } + + public String toString() { + return "Page " + seitennummer; + } + + public float getWidth() { + return width; + } + + public float getHeight() { + return height; + } + + public void updateview(IUpdatenodeview updateobject) { + StringBuffer sb = new StringBuffer(); + sb.append(""); + sb.append("

"); + sb.append("Page " + getSeitennummer()); + sb.append("

"); + sb.append("

"); + sb.append("Size: " + getWidth() + "*" + getHeight()); + sb.append("

"); + + Set set = dictionary.getKeys(); + Iterator it = set.iterator(); + while (it.hasNext()) { + sb.append("

"); + sb.append("Key " + it.next().toString()); + sb.append("

"); + } + sb.append(""); + updateobject.showvalues(sb.toString()); + } + + public Icon getIcon(){ + return new ImageIcon(TreeViewInternalFrame.class.getResource( + "pageonly.gif")); + } + + +} diff --git a/src/main/java/com/lowagie/tools/plugins/treeview/ProgressDialog.java b/src/main/java/com/lowagie/tools/plugins/treeview/ProgressDialog.java new file mode 100644 index 0000000..42e46db --- /dev/null +++ b/src/main/java/com/lowagie/tools/plugins/treeview/ProgressDialog.java @@ -0,0 +1,124 @@ +/* + * $Id: ProgressDialog.java,v 1.1 2005/11/29 21:05:02 blowagie Exp $ + * $Name: $ + * + * Copyright 2005 by Anonymous. + * + * The contents of this file are subject to the Mozilla Public License Version 1.1 + * (the "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the License. + * + * The Original Code is 'iText, a free JAVA-PDF library'. + * + * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by + * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie. + * All Rights Reserved. + * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer + * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved. + * + * Contributor(s): all the names of the contributors are added in the source code + * where applicable. + * + * Alternatively, the contents of this file may be used under the terms of the + * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the + * provisions of LGPL are applicable instead of those above. If you wish to + * allow use of your version of this file only under the terms of the LGPL + * License and not to allow others to use your version of this file under + * the MPL, indicate your decision by deleting the provisions above and + * replace them with the notice and other provisions required by the LGPL. + * If you do not delete the provisions above, a recipient may use your version + * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE. + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the MPL as stated above or under the terms of the GNU + * Library General Public License as published by the Free Software Foundation; + * either version 2 of the License, or any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more + * details. + * + * If you didn't download this code from the following link, you should check if + * you aren't using an obsolete version: + * http://www.lowagie.com/iText/ + */ + +package com.lowagie.tools.plugins.treeview; + +import java.awt.BorderLayout; +import java.awt.Dimension; +import java.awt.Frame; +import java.awt.Toolkit; +import javax.swing.JDialog; +import javax.swing.JLabel; +import javax.swing.JPanel; +import javax.swing.JProgressBar; + + +public class ProgressDialog extends JDialog { + JPanel panel1 = new JPanel(); + BorderLayout borderLayout1 = new BorderLayout(); + JPanel jPanel1 = new JPanel(); + BorderLayout borderLayout2 = new BorderLayout(); + BorderLayout borderLayout3 = new BorderLayout(); + JLabel jLabel1 = new JLabel(); + JProgressBar jProgressBar1 = new JProgressBar(); + private int anzahlseiten; + private int aktuelleseite; + + public ProgressDialog(Frame frame, String title, boolean modal) { + super(frame, title, modal); + try { + jbInit(); + pack(); + } + catch(Exception ex) { + ex.printStackTrace(); + } + } + + public ProgressDialog() { + this(null, "", false); + } + + private void jbInit() throws Exception { + panel1.setLayout(borderLayout1); + this.getContentPane().setLayout(borderLayout2); + jPanel1.setLayout(borderLayout3); + jLabel1.setText("Analysefortschritt"); + jProgressBar1.setStringPainted(true); + getContentPane().add(panel1, BorderLayout.CENTER); + panel1.add(jPanel1, BorderLayout.CENTER); + jPanel1.add(jProgressBar1, BorderLayout.CENTER); + this.getContentPane().add(jLabel1, BorderLayout.NORTH); + //Das Fenster zentrieren + Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); + Dimension frameSize = getSize(); + if (frameSize.height > screenSize.height) { + frameSize.height = screenSize.height; + } + if (frameSize.width > screenSize.width) { + frameSize.width = screenSize.width; + } + setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2); + } + public int getAnzahlseiten() { + return anzahlseiten; + } + public void setAnzahlseiten(int anzahlseiten) { + this.anzahlseiten = anzahlseiten; + jProgressBar1.setMaximum(anzahlseiten); + } + public int getAktuelleseite() { + return aktuelleseite; + } + public void setAktuelleseite(int aktuelleseite) { + this.aktuelleseite = aktuelleseite; + jProgressBar1.setValue(aktuelleseite); + } +} diff --git a/src/main/java/com/lowagie/tools/plugins/treeview/SimpletextTreeNode.java b/src/main/java/com/lowagie/tools/plugins/treeview/SimpletextTreeNode.java new file mode 100644 index 0000000..2eedb6e --- /dev/null +++ b/src/main/java/com/lowagie/tools/plugins/treeview/SimpletextTreeNode.java @@ -0,0 +1,69 @@ +/* + * $Id: SimpletextTreeNode.java,v 1.1 2005/11/29 21:05:02 blowagie Exp $ + * $Name: $ + * + * Copyright 2005 by Anonymous. + * + * The contents of this file are subject to the Mozilla Public License Version 1.1 + * (the "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the License. + * + * The Original Code is 'iText, a free JAVA-PDF library'. + * + * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by + * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie. + * All Rights Reserved. + * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer + * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved. + * + * Contributor(s): all the names of the contributors are added in the source code + * where applicable. + * + * Alternatively, the contents of this file may be used under the terms of the + * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the + * provisions of LGPL are applicable instead of those above. If you wish to + * allow use of your version of this file only under the terms of the LGPL + * License and not to allow others to use your version of this file under + * the MPL, indicate your decision by deleting the provisions above and + * replace them with the notice and other provisions required by the LGPL. + * If you do not delete the provisions above, a recipient may use your version + * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE. + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the MPL as stated above or under the terms of the GNU + * Library General Public License as published by the Free Software Foundation; + * either version 2 of the License, or any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more + * details. + * + * If you didn't download this code from the following link, you should check if + * you aren't using an obsolete version: + * http://www.lowagie.com/iText/ + */ + +package com.lowagie.tools.plugins.treeview; + +public class SimpletextTreeNode + extends UpdateableTreeNode { + + public SimpletextTreeNode(Object p0) { + super(p0); + } + + public void updateview(IUpdatenodeview updateobject) { + StringBuffer sb = new StringBuffer(); + sb.append(""); + sb.append("

"); + sb.append(this.userObject); + sb.append("

"); + sb.append(""); + updateobject.showvalues(sb.toString()); + } +} diff --git a/src/main/java/com/lowagie/tools/plugins/treeview/TextpaneTreeNode.java b/src/main/java/com/lowagie/tools/plugins/treeview/TextpaneTreeNode.java new file mode 100644 index 0000000..bf37c7a --- /dev/null +++ b/src/main/java/com/lowagie/tools/plugins/treeview/TextpaneTreeNode.java @@ -0,0 +1,91 @@ +/* + * $Id: TextpaneTreeNode.java,v 1.2 2006/05/30 09:13:36 blowagie Exp $ + * $Name: $ + * + * Copyright 2005 by Anonymous. + * + * The contents of this file are subject to the Mozilla Public License Version 1.1 + * (the "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the License. + * + * The Original Code is 'iText, a free JAVA-PDF library'. + * + * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by + * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie. + * All Rights Reserved. + * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer + * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved. + * + * Contributor(s): all the names of the contributors are added in the source code + * where applicable. + * + * Alternatively, the contents of this file may be used under the terms of the + * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the + * provisions of LGPL are applicable instead of those above. If you wish to + * allow use of your version of this file only under the terms of the LGPL + * License and not to allow others to use your version of this file under + * the MPL, indicate your decision by deleting the provisions above and + * replace them with the notice and other provisions required by the LGPL. + * If you do not delete the provisions above, a recipient may use your version + * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE. + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the MPL as stated above or under the terms of the GNU + * Library General Public License as published by the Free Software Foundation; + * either version 2 of the License, or any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more + * details. + * + * If you didn't download this code from the following link, you should check if + * you aren't using an obsolete version: + * http://www.lowagie.com/iText/ + */ + +package com.lowagie.tools.plugins.treeview; + +import java.io.IOException; + +import com.lowagie.text.pdf.*; +import javax.swing.Icon; +import javax.swing.ImageIcon; + +public class TextpaneTreeNode + extends UpdateableTreeNode { + PdfObject contents; + String marker; + + public TextpaneTreeNode(PdfObject contents,String marker) { + super(contents,false); + this.contents = contents; + this.marker=marker; + } + + public void updateview(IUpdatenodeview updateobject) { + if (contents.isStream()) { + PRStream prstr = (PRStream) contents; + try { + byte barr[] = PdfReader.getStreamBytes(prstr); + updateobject.showtext(new String(barr)); + } + catch (IOException ex) { + } + } + } + + public String toString() { + return marker; + } + + public Icon getIcon(){ + return new ImageIcon(TreeViewInternalFrame.class.getResource( + "string.gif")); + } + +} diff --git a/src/main/java/com/lowagie/tools/plugins/treeview/TreeViewInternalFrame.java b/src/main/java/com/lowagie/tools/plugins/treeview/TreeViewInternalFrame.java new file mode 100644 index 0000000..802059b --- /dev/null +++ b/src/main/java/com/lowagie/tools/plugins/treeview/TreeViewInternalFrame.java @@ -0,0 +1,272 @@ +/* + * $Id: TreeViewInternalFrame.java,v 1.4 2006/05/29 10:31:32 blowagie Exp $ + * $Name: $ + * + * Copyright 2005 by Anonymous. + * + * The contents of this file are subject to the Mozilla Public License Version 1.1 + * (the "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the License. + * + * The Original Code is 'iText, a free JAVA-PDF library'. + * + * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by + * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie. + * All Rights Reserved. + * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer + * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved. + * + * Contributor(s): all the names of the contributors are added in the source code + * where applicable. + * + * Alternatively, the contents of this file may be used under the terms of the + * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the + * provisions of LGPL are applicable instead of those above. If you wish to + * allow use of your version of this file only under the terms of the LGPL + * License and not to allow others to use your version of this file under + * the MPL, indicate your decision by deleting the provisions above and + * replace them with the notice and other provisions required by the LGPL. + * If you do not delete the provisions above, a recipient may use your version + * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE. + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the MPL as stated above or under the terms of the GNU + * Library General Public License as published by the Free Software Foundation; + * either version 2 of the License, or any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more + * details. + * + * If you didn't download this code from the following link, you should check if + * you aren't using an obsolete version: + * http://www.lowagie.com/iText/ + */ + +package com.lowagie.tools.plugins.treeview; + +import java.beans.PropertyChangeEvent; +import java.beans.PropertyChangeListener; + +import java.awt.BorderLayout; +import java.awt.CardLayout; +import java.awt.Dimension; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import javax.swing.*; +import javax.swing.event.TreeSelectionEvent; +import javax.swing.event.TreeSelectionListener; +import javax.swing.tree.TreeModel; +import javax.swing.tree.DefaultTreeModel; +import javax.swing.tree.DefaultMutableTreeNode; +import com.lowagie.tools.SwingWorker; +import java.awt.Cursor; +import javax.swing.tree.DefaultTreeCellRenderer; +import java.awt.Component; +import javax.swing.table.TableColumnModel; + +public class TreeViewInternalFrame + extends JInternalFrame implements PropertyChangeListener, IUpdatenodeview { + AnalyzePDF analyzer; + + public TreeViewInternalFrame(String title, boolean resizable, + boolean closable, + boolean maximizable) { + super(title, resizable, closable, maximizable); + try { + jbInit(); + } + catch (Exception ex) { + ex.printStackTrace(); + } + } + + private void jbInit() throws Exception { + this.getContentPane().setLayout(borderLayout1); + jPanel1.setLayout(borderLayout2); + jPanel2.setLayout(cardLayout2); + jSplitPane1.setMinimumSize(new Dimension(150, 100)); + jSplitPane1.setOrientation(JSplitPane.VERTICAL_SPLIT); + jTree1.addTreeSelectionListener(new Untitled1_jTree1_treeSelectionAdapter(this)); + jPanel4.setLayout(borderLayout3); + jPanel5.setLayout(borderLayout5); + jTree1.setModel(defaultree); + jPanel3.setLayout(borderLayout6); + jPanel6.setLayout(borderLayout7); + jSplitPane2.setDividerSize(3); + jPanel1.add(jSplitPane1, java.awt.BorderLayout.CENTER); + this.getContentPane().add(jSplitPane1, java.awt.BorderLayout.CENTER); + jPanel2.add(jPanel4, "empty"); + jPanel2.add(jPanel5, "values"); + jScrollPane3.setViewportView(jLabel1); + jScrollPane2.setViewportView(jTextPane1); + jPanel2.add(jScrollPane2, "text"); + jSplitPane1.add(jPanel3, JSplitPane.TOP); + jPanel5.add(jScrollPane3, java.awt.BorderLayout.CENTER); + jSplitPane1.add(jPanel2, JSplitPane.BOTTOM); + jSplitPane2.add(jScrollPane1, JSplitPane.TOP); + jSplitPane2.add(jPanel6, JSplitPane.BOTTOM); + jScrollPane4.setViewportView(jTable1); + jPanel3.add(jSplitPane2, java.awt.BorderLayout.CENTER); + jPanel6.add(jScrollPane4, java.awt.BorderLayout.CENTER); + jScrollPane1.setViewportView(jTree1); + jSplitPane1.setDividerLocation(170); + jSplitPane2.setDividerLocation(200); + PDFTreeCellRenderer renderer=new PDFTreeCellRenderer(); + jTree1.setCellRenderer(renderer); + } + + public void setTreemodel(TreeModel treemodel) { + jTree1.setModel(treemodel); + } + + JPanel jPanel1 = new JPanel(); + BorderLayout borderLayout1 = new BorderLayout(); + JSplitPane jSplitPane1 = new JSplitPane(); + JScrollPane jScrollPane1 = new JScrollPane(); + JTree jTree1 = new JTree(); + JPanel jPanel2 = new JPanel(); + BorderLayout borderLayout2 = new BorderLayout(); + BorderLayout borderLayout4 = new BorderLayout(); + CardLayout cardLayout2 = new CardLayout(); + JPanel jPanel4 = new JPanel(); + JTextPane jTextPane1 = new JTextPane(); + JPanel jPanel5 = new JPanel(); + BorderLayout borderLayout3 = new BorderLayout(); + BorderLayout borderLayout5 = new BorderLayout(); + JLabel jLabel1 = new JLabel(); + JScrollPane jScrollPane2 = new JScrollPane(); + JScrollPane jScrollPane3 = new JScrollPane(); + DefaultTreeModel defaultree = new DefaultTreeModel(new DefaultMutableTreeNode()); + JPanel jPanel3 = new JPanel(); + BorderLayout borderLayout6 = new BorderLayout(); + JPanel jPanel6 = new JPanel(); + JTable jTable1 = new JTable(); + BorderLayout borderLayout7 = new BorderLayout(); + JSplitPane jSplitPane2 = new JSplitPane(); + JScrollPane jScrollPane4 = new JScrollPane(); + TableColumnModel tableColumnModel1 = jTable1.getColumnModel(); + /** + * This method gets called when a bound property is changed. + * + * @param evt A PropertyChangeEvent object describing the event source and the property that has + * changed. + * @todo Implement this java.beans.PropertyChangeListener method + */ + public void propertyChange(PropertyChangeEvent evt) { + + String propertyname = evt.getPropertyName(); + + if (propertyname == null) { + return; + } + else if (propertyname.equals("srcfile")) { + final String filename = (String) evt.getNewValue(); + final ProgressDialog blubb; + blubb = new ProgressDialog(null, "PDF Analysefortschritt", false); + + final JInternalFrame jif = this; + SwingWorker work = new SwingWorker() { + + public Object construct() { + AnalyzePDF analyzer = new AnalyzePDF(filename, blubb); + jif.setCursor(Cursor.getDefaultCursor()); + return analyzer; + } + }; + jif.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); + work.start(); + blubb.show(); + analyzer = (AnalyzePDF) work.get(); + + Timer activitymonitor = new Timer(250, new ActionListener() { + public void actionPerformed(ActionEvent event) { + analyzer.updatecount(); + } + }); + + analyzer.start(); + activitymonitor.start(); + setTreemodel(analyzer); + this.jTable1.setModel(analyzer.getXReftable()); + } + } + + public void jTree1_valueChanged(TreeSelectionEvent e) { + String event = e.getClass().toString(); + if (event.equalsIgnoreCase("class javax.swing.event.TreeSelectionEvent")) { + UpdateableTreeNode selectednode = (UpdateableTreeNode) jTree1. + getLastSelectedPathComponent(); + System.out.println("Selected node: " + selectednode); + if (selectednode != null) { + selectednode.updateview(this); + } + } + } + + /** + * This method fills the bottom window with the given text in a JtextPane + * IUpdatenodeview method + * @param text String Plain text + */ + public void showtext(String text) { + jTextPane1.setText(text); + cardLayout2.show(jPanel2, "text"); + jPanel2.repaint(); + jTextPane1.repaint(); + } + + /** + * This method clears the bottom window + * IUpdatenodeview method + */ + public void showempty() { + cardLayout2.show(jPanel2, "empty"); + jPanel2.repaint(); + } + + /** + * This method fills the bottom window with the given text in a JLabel + * IUpdatenodeview method + * @param text String HTML formatted text + */ + public void showvalues(String text) { + jLabel1.setText(text); + cardLayout2.show(jPanel2, "values"); + } + + class PDFTreeCellRenderer extends DefaultTreeCellRenderer{ + public Component getTreeCellRendererComponent(JTree tree,Object value,boolean selected,boolean expanded,boolean leaf,int row,boolean hasFocus){ + super.getTreeCellRendererComponent(tree,value,selected,expanded,leaf,row,hasFocus); + DefaultMutableTreeNode node=(DefaultMutableTreeNode)value; + if(node instanceof UpdateableTreeNode){ + UpdateableTreeNode utn=(UpdateableTreeNode)node; + Object o = node.getUserObject(); + setFont(getFont().deriveFont(12.1f)); +// setIcon(new ImageIcon(TreeViewInternalFrame.class.getResource( +// "icon16.gif"))); + setIcon(utn.getIcon()); + } + return this; + } + } +} + +class Untitled1_jTree1_treeSelectionAdapter + implements TreeSelectionListener { + private TreeViewInternalFrame adaptee; + Untitled1_jTree1_treeSelectionAdapter(TreeViewInternalFrame adaptee) { + this.adaptee = adaptee; + } + + public void valueChanged(TreeSelectionEvent e) { + adaptee.jTree1_valueChanged(e); + } + + +} diff --git a/src/main/java/com/lowagie/tools/plugins/treeview/UpdateableTreeNode.java b/src/main/java/com/lowagie/tools/plugins/treeview/UpdateableTreeNode.java new file mode 100644 index 0000000..bd7cf47 --- /dev/null +++ b/src/main/java/com/lowagie/tools/plugins/treeview/UpdateableTreeNode.java @@ -0,0 +1,74 @@ +/* + * $Id: UpdateableTreeNode.java,v 1.2 2006/05/29 10:31:32 blowagie Exp $ + * $Name: $ + * + * Copyright 2005 by Anonymous. + * + * The contents of this file are subject to the Mozilla Public License Version 1.1 + * (the "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the License. + * + * The Original Code is 'iText, a free JAVA-PDF library'. + * + * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by + * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie. + * All Rights Reserved. + * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer + * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved. + * + * Contributor(s): all the names of the contributors are added in the source code + * where applicable. + * + * Alternatively, the contents of this file may be used under the terms of the + * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the + * provisions of LGPL are applicable instead of those above. If you wish to + * allow use of your version of this file only under the terms of the LGPL + * License and not to allow others to use your version of this file under + * the MPL, indicate your decision by deleting the provisions above and + * replace them with the notice and other provisions required by the LGPL. + * If you do not delete the provisions above, a recipient may use your version + * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE. + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the MPL as stated above or under the terms of the GNU + * Library General Public License as published by the Free Software Foundation; + * either version 2 of the License, or any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more + * details. + * + * If you didn't download this code from the following link, you should check if + * you aren't using an obsolete version: + * http://www.lowagie.com/iText/ + */ + +package com.lowagie.tools.plugins.treeview; + +import javax.swing.tree.DefaultMutableTreeNode; +import javax.swing.ImageIcon; +import javax.swing.Icon; + +public abstract class UpdateableTreeNode + extends DefaultMutableTreeNode { + public UpdateableTreeNode() { + super(); + } + + public UpdateableTreeNode(Object userObject) { + super(userObject); + } + public UpdateableTreeNode(Object userObject,boolean allowchildren) { + super(userObject,allowchildren); + } + public abstract void updateview(IUpdatenodeview updateobject); + public Icon getIcon(){ + return null; + } + } + diff --git a/src/main/java/com/lowagie/tools/split_pdf.java b/src/main/java/com/lowagie/tools/split_pdf.java new file mode 100644 index 0000000..d72b93e --- /dev/null +++ b/src/main/java/com/lowagie/tools/split_pdf.java @@ -0,0 +1,103 @@ +/* + * $Id: split_pdf.java,v 1.18 2005/04/11 08:27:24 blowagie Exp $ + * $Name: $ + * + * This code is free software. It may only be copied or modified + * if you include the following copyright notice: + * + * This class by Bruno Lowagie. Copyright (c) 2002 Bruno Lowagie. + * + * This code is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * + * itext@lowagie.com + */ +package com.lowagie.tools; + +import java.io.*; + +import com.lowagie.text.*; +import com.lowagie.text.pdf.*; + +/** + * This class demonstrates how to split a PDF file using iText. + * @author Bruno Lowagie + */ +public class split_pdf extends java.lang.Object { + + /** + * This class can be used to split an existing PDF file. + * @param args the command line arguments + */ + public static void main(String args[]) { + if (args.length != 4) { + System.err.println("arguments: srcfile destfile1 destfile2 pagenumber"); + } + else { + try { + int pagenumber = Integer.parseInt(args[3]); + + // we create a reader for a certain document + PdfReader reader = new PdfReader(args[0]); + // we retrieve the total number of pages + int n = reader.getNumberOfPages(); + System.out.println("There are " + n + " pages in the original file."); + + if (pagenumber < 2 || pagenumber > n) { + throw new DocumentException("You can't split this document at page " + pagenumber + "; there is no such page."); + } + + // step 1: creation of a document-object + Document document1 = new Document(reader.getPageSizeWithRotation(1)); + Document document2 = new Document(reader.getPageSizeWithRotation(pagenumber)); + // step 2: we create a writer that listens to the document + PdfWriter writer1 = PdfWriter.getInstance(document1, new FileOutputStream(args[1])); + PdfWriter writer2 = PdfWriter.getInstance(document2, new FileOutputStream(args[2])); + // step 3: we open the document + document1.open(); + PdfContentByte cb1 = writer1.getDirectContent(); + document2.open(); + PdfContentByte cb2 = writer2.getDirectContent(); + PdfImportedPage page; + int rotation; + int i = 0; + // step 4: we add content + while (i < pagenumber - 1) { + i++; + document1.setPageSize(reader.getPageSizeWithRotation(i)); + document1.newPage(); + page = writer1.getImportedPage(reader, i); + rotation = reader.getPageRotation(i); + if (rotation == 90 || rotation == 270) { + cb1.addTemplate(page, 0, -1f, 1f, 0, 0, reader.getPageSizeWithRotation(i).height()); + } + else { + cb1.addTemplate(page, 1f, 0, 0, 1f, 0, 0); + } + } + while (i < n) { + i++; + document2.setPageSize(reader.getPageSizeWithRotation(i)); + document2.newPage(); + page = writer2.getImportedPage(reader, i); + rotation = reader.getPageRotation(i); + if (rotation == 90 || rotation == 270) { + cb2.addTemplate(page, 0, -1f, 1f, 0, 0, reader.getPageSizeWithRotation(i).height()); + } + else { + cb2.addTemplate(page, 1f, 0, 0, 1f, 0, 0); + } + System.out.println("Processed page " + i); + } + // step 5: we close the document + document1.close(); + document2.close(); + } + catch(Exception e) { + e.printStackTrace(); + } + } + } +} + -- cgit v1.2.3