1 50 51 package com.lowagie.tools; 52 53 import java.io.FileOutputStream ; 54 import java.util.ArrayList ; 55 import java.util.List ; 56 57 import com.lowagie.text.Document; 58 import com.lowagie.text.pdf.PdfCopy; 59 import com.lowagie.text.pdf.PdfImportedPage; 60 import com.lowagie.text.pdf.PdfReader; 61 import com.lowagie.text.pdf.SimpleBookmark; 62 63 66 public class concat_pdf { 67 68 73 public static void main(String args[]) { 74 if (args.length < 2) { 75 System.err.println("arguments: file1 [file2 ...] destfile"); 76 } 77 else { 78 try { 79 int pageOffset = 0; 80 ArrayList master = new ArrayList (); 81 int f = 0; 82 String outFile = args[args.length-1]; 83 Document document = null; 84 PdfCopy writer = null; 85 while (f < args.length-1) { 86 PdfReader reader = new PdfReader(args[f]); 88 reader.consolidateNamedDestinations(); 89 int n = reader.getNumberOfPages(); 91 List bookmarks = SimpleBookmark.getBookmark(reader); 92 if (bookmarks != null) { 93 if (pageOffset != 0) 94 SimpleBookmark.shiftPageNumbers(bookmarks, pageOffset, null); 95 master.addAll(bookmarks); 96 } 97 pageOffset += n; 98 System.out.println("There are " + n + " pages in " + args[f]); 99 100 if (f == 0) { 101 document = new Document(reader.getPageSizeWithRotation(1)); 103 writer = new PdfCopy(document, new FileOutputStream (outFile)); 105 document.open(); 107 } 108 PdfImportedPage page; 110 for (int i = 0; i < n; ) { 111 ++i; 112 page = writer.getImportedPage(reader, i); 113 writer.addPage(page); 114 System.out.println("Processed page " + i); 115 } 116 writer.freeReader(reader); 117 f++; 118 } 119 if (!master.isEmpty()) 120 writer.setOutlines(master); 121 document.close(); 123 } 124 catch(Exception e) { 125 e.printStackTrace(); 126 } 127 } 128 } 129 } 130 | Popular Tags |