List of usage examples for com.itextpdf.text.pdf PdfSmartCopy freeReader
@Override public void freeReader(PdfReader reader) throws IOException
From source file:com.github.hossman.PdfShrinker.java
License:Apache License
public static void main(String args[]) throws Exception { if (1 != args.length) { System.err.println("Run this app with a single command line PDF filename"); System.err.println("The specified file will be read, and a shrunk version written to stdout"); System.err.println("ie: java -jar pdf-shrinker.jar big.pdf > small.pdf"); System.exit(-1);//from w w w . j av a 2 s . co m } Document document = new Document(); PdfSmartCopy copy = new PdfSmartCopy(document, System.out); copy.setCompressionLevel(9); copy.setFullCompression(); document.open(); PdfReader reader = new PdfReader(args[0]); List<HashMap<String, Object>> bookmarks = SimpleBookmark.getBookmark(reader); int pages = reader.getNumberOfPages(); for (int i = 0; i < pages; i++) { PdfImportedPage page = copy.getImportedPage(reader, i + 1); copy.addPage(page); } copy.freeReader(reader); reader.close(); copy.setOutlines(bookmarks); document.close(); }
From source file:org.h819.commons.file.MyPDFUtils.java
/** * ?//from w ww.j a v a2 s .co m * <p> * ? iText in Action 2nd EditionChapter 6: Working with existing PDFs * Concatenate * </p> * * @param files ? * @param result ?? * @throws DocumentException * @throws java.io.IOException */ public static void merge(File[] files, File result) throws DocumentException, IOException { // step 1 Document document = new Document(); // step 2 /** * PdfCopy PdfSmartCopy * * PdfCopy??? * * PdfSmartCopy?????? */ // // PdfCopy copy = new PdfCopy(document, new FileOutputStream(result)); PdfSmartCopy copy = new PdfSmartCopy(document, new FileOutputStream(result)); // step 3 document.open(); // step 4 PdfReader reader; int n; // loop over the documents you want to concatenate for (int i = 0; i < files.length; i++) { reader = getPdfReader(files[i]); // loop over the pages in that document n = reader.getNumberOfPages(); for (int page = 0; page < n;) { copy.addPage(copy.getImportedPage(reader, ++page)); } copy.freeReader(reader); } // step 5 document.close(); }