📄 pdftool.java
字号:
// $Id: PDFTool.java,v 1.8 2007/04/13 16:59:24 mike Exp $package org.faceless.pdf2.viewer;import org.faceless.pdf2.*;import org.xml.sax.SAXException;import java.io.*;import java.util.*;import java.awt.RenderingHints;import java.security.*;/** * <p> * This class is a utility class which does many of the common tasks required on PDFs - * joining them, completing forms, converting to bitmap images or viewing them. * It's the default action when the "bfopdf.jar" file is run. Try * <code>java -jar bfopdf.jar --help</code> for a list of options. * </p> * <p><i> * This code is copyright the Big Faceless Organization. You're welcome to * use, modify and distribute it in any form in your own projects, provided * those projects continue to make use of the Big Faceless PDF library. * </i></p> * @since 2.7.1 */public class PDFTool{ private static final String APPNAME = "PDFTool"; static String filename; public static void main(String[] args) throws Exception { setLicense(); PDF pdf = null; OutputStream out = System.out; OutputProfile profile = null; try { ListIterator i = Arrays.asList(args).listIterator(); while (i.hasNext()) { String cmd = (String)i.next(); if (cmd.equals("--join")) { pdf = join(i, pdf); } else if (cmd.startsWith("--output=")) { out = new FileOutputStream(cmd.substring(9)); } else if (cmd.equals("--profile=nocompression")) { profile = OutputProfile.NoCompression; } else if (cmd.equals("--profile=recompression")) { profile = new OutputProfile(OutputProfile.Default); profile.setRequired(OutputProfile.Feature.RegularCompression); } else if (cmd.equals("--profile=acrobat4")) { profile = OutputProfile.Acrobat4Compatible; } else if (cmd.equals("--profile=acrobat5")) { profile = OutputProfile.Acrobat5Compatible; } else if (cmd.equals("--profile=acrobat6")) { profile = OutputProfile.Acrobat6Compatible; } else if (cmd.equals("--profile=acrobat7")) { profile = OutputProfile.Acrobat7Compatible; } else if (cmd.equals("--form")) { pdf = form(i, pdf); } else if (cmd.equals("--view")) { view(i, pdf, filename); pdf = null; if (i.hasNext()) throw new IllegalArgumentException("--view must be the last action"); } else if (cmd.equals("--sign")) { pdf = sign(i, pdf); } else if (cmd.equals("--version")) { usage(); System.exit(0); } else if (cmd.equals("--help")) { help(); System.exit(0); } else if (cmd.equals("--toimage")) { toimage(i, pdf, out); pdf = null; if (i.hasNext()) throw new IllegalArgumentException("--toimage must be the last action"); } else if (!cmd.startsWith("--")) { pdf = load(cmd); } else { throw new IllegalArgumentException("Unknown argument \""+cmd+"\""); } } } catch (IllegalArgumentException e) { System.err.println(APPNAME+" "+PDF.VERSION+": Error: "+e.getMessage()); System.err.println(" Run "+APPNAME+" to open the viewer"); System.err.println(" Run "+APPNAME+" --help to display options"); System.exit(1); } if (args.length==0 || args.length==1 && !args[0].startsWith("--")) { view(null, pdf, filename); } else { if (pdf!=null) { if (profile!=null) pdf.setOutputProfile(profile); pdf.render(out); out.close(); } } } private static PDF load(String file) throws IOException { filename = file; return new PDF(new PDFReader(new FileInputStream(filename))); } private static final String getValue(String in) { return in.substring(in.indexOf("=")+1); } protected static void setLicense() { // To set your license key, insert it here, recompile the class // and insert it back into the JAR. // // PDF.setLicenseKey(...); } static PDF join(ListIterator i, PDF pdf) throws IOException { int dpi = 0; boolean flatten = true; String pages = null, pagesize = null; if (pdf==null) pdf = new PDF(); List outpages = pdf.getPages(); while (i.hasNext()) { String opt = (String)i.next(); if (opt.startsWith("--dpi=")) { dpi = Integer.parseInt(getValue(opt)); if (dpi<0 || dpi>600) throw new IllegalArgumentException("DPI must be betweeen 0 and 600"); } else if (opt.startsWith("--pages=")) { pages = getValue(opt); } else if (opt.startsWith("--pagesize=")) { pagesize = getValue(opt); if (pagesize.equals("auto")) pagesize=null; } else if (opt.startsWith("--")) { i.previous(); return pdf; } else { try { InputStream in = new FileInputStream(opt); PDF pdf2 = new PDF(new PDFReader(in)); in.close(); if (flatten) pdf2.getForm().flatten(); PDFPage outpage = pagesize==null ? null : new PDFPage(pagesize); List inpages = pdf2.getPages(); int[] pagelist = parsePages(pages, inpages.size()); for (int j=0;j<pagelist.length;j++) { PDFPage inpage = (PDFPage)inpages.get(pagelist[j]); outpage = pagesize==null ? null : new PDFPage(pagesize); if (outpage!=null && (inpage.getWidth() != outpage.getWidth() || inpage.getHeight()!=outpage.getHeight())) { PDFCanvas can = new PDFCanvas(inpage); outpage.drawCanvas(can, 0, 0, outpage.getWidth(), outpage.getHeight()); outpages.add(outpage); outpage = new PDFPage(pagesize); } else { outpages.add(inpage); } } } catch (IOException e1) { try { InputStream in = new FileInputStream(opt); PDFImageSet images = new PDFImageSet(in); in.close(); int[] pagelist = parsePages(pages, images.getNumImages()); for (int j=0;j<pagelist.length;j++) { PDFImage image = images.getImage(pagelist[j]); PDFPage page; if (pagesize!=null) { page = new PDFPage(pagesize); } else { double iw = dpi==0 ? image.getWidth() : image.getWidth()*image.getDPIX()/dpi; double ih = dpi==0 ? image.getHeight() : image.getHeight()*image.getDPIY()/dpi; page = new PDFPage((int)iw, (int)ih); } page.drawImage(image, 0, 0, page.getWidth(), page.getHeight()); outpages.add(page); } } catch (IOException e2) { throw e1; } } pages=null; } } return pdf; } /** * Given a page range, eg "1-4,6,7-end" or "reverse" or "all", return an integer * list of the pages to use * @param pages the page list as a string * @param num the number of pages available */ private static int[] parsePages(String pages, int num) { int[] out; if (pages==null || pages.equals("all")) { out = new int[num]; for (int i=0;i<out.length;i++) out[i] = i; } else if (pages.equals("reverse")) { out = new int[num]; for (int i=0;i<out.length;i++) out[i] = num-i-1; } else { if (pages.charAt(0)=='-') { pages = "1" + pages; } if (pages.charAt(pages.length()-1)=='-') { pages += (num+1); } List t = new ArrayList(); for (StringTokenizer st = new StringTokenizer(pages, ","); st.hasMoreTokens();) { String s1 = st.nextToken(); int k = s1.indexOf('-'); if (k < 0) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -