1 31 package org.pdfbox.examples.persistence; 32 33 import java.io.IOException ; 34 import java.io.OutputStream ; 35 import java.io.FileOutputStream ; 36 import java.io.InputStream ; 37 import java.io.FileInputStream ; 38 39 import java.util.Iterator ; 40 41 import org.pdfbox.pdfparser.PDFParser; 42 43 import org.pdfbox.pdfwriter.COSWriter; 44 45 import org.pdfbox.cos.COSDocument; 46 import org.pdfbox.cos.COSString; 47 import org.pdfbox.cos.COSBase; 48 import org.pdfbox.cos.COSDictionary; 49 import org.pdfbox.cos.COSObject; 50 import org.pdfbox.cos.COSName; 51 import org.pdfbox.cos.COSArray; 52 import org.pdfbox.cos.COSNumber; 53 import org.pdfbox.cos.COSInteger; 54 55 import org.pdfbox.exceptions.COSVisitorException; 56 57 64 public class AppendAndFillDoc 65 { 66 69 public AppendAndFillDoc() 70 { 71 super(); 72 } 73 74 83 public void appendDocument(COSDocument destination, COSDocument source) 84 { 85 COSDictionary pages2 = getPages(source); 86 COSArray kids = (COSArray)pages2.getItem(COSName.getPDFName("Kids")); 87 for (Iterator i = kids.iterator(); i.hasNext();) 88 { 89 COSDictionary page = (COSDictionary)((COSObject)i.next()).getObject(); 90 appendPage(destination, page); 91 } 92 } 93 102 public void appendPage(COSDocument destination, COSDictionary page) 103 { 104 COSDictionary pages = getPages(destination); 106 page.setItem(COSName.PARENT, pages); 108 COSArray kids = (COSArray) pages.getItem(COSName.getPDFName("Kids")); 110 kids.add(page); 111 COSNumber count = (COSNumber) pages.getItem(COSName.COUNT); 113 pages.setItem(COSName.COUNT, new COSInteger(count.intValue() + 1)); 114 } 115 131 public void doIt( String in1, 132 String in2, 133 String out, 134 String name1, 135 String value1, 136 String name2, 137 String value2) throws IOException , COSVisitorException 138 { 139 COSDocument doc1 = null; 140 COSDocument doc2 = null; 141 InputStream is1 = null; 142 InputStream is2 = null; 143 PDFParser parser1 = null; 144 PDFParser parser2 = null; 145 146 OutputStream os = null; 147 COSWriter writer = null; 148 try 149 { 150 is1 = new FileInputStream (in1); 151 parser1 = new PDFParser(is1); 152 parser1.parse(); 153 doc1 = parser1.getDocument(); 154 155 is2 = new FileInputStream (in2); 156 parser2 = new PDFParser(is2); 157 parser2.parse(); 158 doc2 = parser2.getDocument(); 159 160 setField(doc1, "doc1", new COSString(name1), new COSString(value1)); 161 setField(doc2, "doc2", new COSString(name2), new COSString(value2)); 162 163 appendDocument(doc1, doc2); 164 165 os = new FileOutputStream (out); 166 writer = new COSWriter(os); 167 writer.write(doc1); 168 } 169 finally 170 { 171 is1.close(); 172 doc1.close(); 173 174 is2.close(); 175 doc2.close(); 176 177 os.close(); 178 writer.close(); 179 } 180 } 181 182 191 public COSDictionary getPages(COSDocument doc) 192 { 193 for (Iterator i = doc.getObjects().iterator(); i.hasNext();) 195 { 196 COSObject obj = (COSObject) i.next(); 197 COSBase base = obj.getObject(); 198 if (base instanceof COSDictionary) 199 { 200 COSDictionary dict = (COSDictionary) base; 201 COSBase type = dict.getItem(COSName.TYPE); 202 if (type != null && type.equals(COSName.getPDFName("Pages"))) 203 { 204 return dict; 205 } 206 } 207 } 208 return null; 209 } 210 217 public static void main(String [] args) 218 { 219 AppendAndFillDoc app = new AppendAndFillDoc(); 220 try 221 { 222 if( args.length != 7 ) 223 { 224 app.usage(); 225 } 226 else 227 { 228 app.doIt( args[0], args[1], args[2], args[3], args[4], args[5], args[6]); 229 } 230 } 231 catch (Exception e) 232 { 233 e.printStackTrace(); 234 } 235 } 236 247 public void setField(COSDocument doc, String prefix, COSString name, COSString value) 248 { 249 for (Iterator i = doc.getObjects().iterator(); i.hasNext();) 250 { 251 COSObject obj = (COSObject) i.next(); 252 COSBase base = obj.getObject(); 253 if (base instanceof COSDictionary) 254 { 255 COSDictionary dict = (COSDictionary) base; 256 COSBase type = dict.getItem(COSName.TYPE); 257 if (type != null && type.equals(COSName.getPDFName("Annot"))) 258 { 259 COSBase subtype = dict.getItem(COSName.getPDFName("Subtype")); 260 if (subtype != null && subtype.equals(COSName.getPDFName("Widget"))) 261 { 262 COSBase fname = dict.getItem(COSName.getPDFName("T")); 264 if (fname != null && fname.equals(name)) 265 { 266 dict.setItem(COSName.getPDFName("V"), value); 267 dict.setItem(COSName.getPDFName("T"), new COSString(prefix + name.getString())); 268 } 269 } 270 } 271 } 272 } 273 } 274 277 private void usage() 278 { 279 System.err.println( "usage: " + this.getClass().getName() + 280 " <input-file1> <input-file2> <output-file> <name1> <value1> <name2> <value2>" ); 281 } 282 } | Popular Tags |