1 50 51 package com.lowagie.text.pdf; 52 import java.io.IOException ; 53 import java.util.ArrayList ; 54 import java.util.HashMap ; 55 import java.util.Iterator ; 56 61 class PdfReaderInstance { 62 static final PdfLiteral IDENTITYMATRIX = new PdfLiteral("[1 0 0 1 0 0]"); 63 static final PdfNumber ONE = new PdfNumber(1); 64 int myXref[]; 65 PdfReader reader; 66 RandomAccessFileOrArray file; 67 HashMap importedPages = new HashMap (); 68 PdfWriter writer; 69 HashMap visited = new HashMap (); 70 ArrayList nextRound = new ArrayList (); 71 72 PdfReaderInstance(PdfReader reader, PdfWriter writer) { 73 this.reader = reader; 74 this.writer = writer; 75 file = reader.getSafeFile(); 76 myXref = new int[reader.getXrefSize()]; 77 } 78 79 PdfReader getReader() { 80 return reader; 81 } 82 83 PdfImportedPage getImportedPage(int pageNumber) { 84 if (!reader.isOpenedWithFullPermissions()) 85 throw new IllegalArgumentException ("PdfReader not opened with owner password"); 86 if (pageNumber < 1 || pageNumber > reader.getNumberOfPages()) 87 throw new IllegalArgumentException ("Invalid page number"); 88 Integer i = new Integer (pageNumber); 89 PdfImportedPage pageT = (PdfImportedPage)importedPages.get(i); 90 if (pageT == null) { 91 pageT = new PdfImportedPage(this, writer, pageNumber); 92 importedPages.put(i, pageT); 93 } 94 return pageT; 95 } 96 97 int getNewObjectNumber(int number, int generation) { 98 if (myXref[number] == 0) { 99 myXref[number] = writer.getIndirectReferenceNumber(); 100 nextRound.add(new Integer (number)); 101 } 102 return myXref[number]; 103 } 104 105 RandomAccessFileOrArray getReaderFile() { 106 return file; 107 } 108 109 PdfObject getResources(int pageNumber) { 110 PdfObject obj = PdfReader.getPdfObjectRelease(reader.getPageNRelease(pageNumber).get(PdfName.RESOURCES)); 111 return obj; 112 } 113 114 115 PdfStream getFormXObject(int pageNumber) throws IOException { 116 PdfDictionary page = reader.getPageNRelease(pageNumber); 117 PdfObject contents = PdfReader.getPdfObjectRelease(page.get(PdfName.CONTENTS)); 118 PdfDictionary dic = new PdfDictionary(); 119 byte bout[] = null; 120 if (contents != null) { 121 if (contents.isStream()) 122 dic.putAll((PRStream)contents); 123 else 124 bout = reader.getPageContent(pageNumber, file); 125 } 126 else 127 bout = new byte[0]; 128 dic.put(PdfName.RESOURCES, PdfReader.getPdfObjectRelease(page.get(PdfName.RESOURCES))); 129 dic.put(PdfName.TYPE, PdfName.XOBJECT); 130 dic.put(PdfName.SUBTYPE, PdfName.FORM); 131 PdfImportedPage impPage = (PdfImportedPage)importedPages.get(new Integer (pageNumber)); 132 dic.put(PdfName.BBOX, new PdfRectangle(impPage.getBoundingBox())); 133 PdfArray matrix = impPage.getMatrix(); 134 if (matrix == null) 135 dic.put(PdfName.MATRIX, IDENTITYMATRIX); 136 else 137 dic.put(PdfName.MATRIX, matrix); 138 dic.put(PdfName.FORMTYPE, ONE); 139 PRStream stream; 140 if (bout == null) { 141 stream = new PRStream((PRStream)contents, dic); 142 } 143 else { 144 stream = new PRStream(reader, bout); 145 stream.putAll(dic); 146 } 147 return stream; 148 } 149 150 void writeAllVisited() throws IOException { 151 while (!nextRound.isEmpty()) { 152 ArrayList vec = nextRound; 153 nextRound = new ArrayList (); 154 for (int k = 0; k < vec.size(); ++k) { 155 Integer i = (Integer )vec.get(k); 156 if (!visited.containsKey(i)) { 157 visited.put(i, null); 158 int n = i.intValue(); 159 writer.addToBody(reader.getPdfObjectRelease(n), myXref[n]); 160 } 161 } 162 } 163 } 164 165 void writeAllPages() throws IOException { 166 try { 167 file.reOpen(); 168 for (Iterator it = importedPages.values().iterator(); it.hasNext();) { 169 PdfImportedPage ip = (PdfImportedPage)it.next(); 170 writer.addToBody(ip.getFormXObject(), ip.getIndirectReference()); 171 } 172 writeAllVisited(); 173 } 174 finally { 175 try { 176 reader.close(); 177 file.close(); 178 } 179 catch (Exception e) { 180 } 182 } 183 } 184 } 185 | Popular Tags |