Code with Finding: |
class PdfReader {
/** Eliminates shared streams if they exist. */
public void eliminateSharedStreams() {
if (!sharedStreams)
return;
sharedStreams = false;
if (pageRefs.size() == 1)
return;
ArrayList<PRIndirectReference> newRefs = new ArrayList<PRIndirectReference>();
ArrayList<PRStream> newStreams = new ArrayList<PRStream>();
IntHashtable visited = new IntHashtable();
for (int k = 1; k <= pageRefs.size(); ++k) {
PdfDictionary page = pageRefs.getPageN(k);
if (page == null)
continue;
PdfObject contents = getPdfObject(page.get(PdfName.CONTENTS));
if (contents == null)
continue;
if (contents.isStream()) {
PRIndirectReference ref = (PRIndirectReference)page.get(PdfName.CONTENTS);
if (visited.containsKey(ref.getNumber())) {
// need to duplicate
newRefs.add(ref);
newStreams.add(new PRStream((PRStream)contents, null));
}
else
visited.put(ref.getNumber(), 1);
}
else if (contents.isArray()) {
PdfArray array = (PdfArray)contents;
for (int j = 0; j < array.size(); ++j) {
PRIndirectReference ref = (PRIndirectReference)array.getPdfObject(j);
if (visited.containsKey(ref.getNumber())) {
// need to duplicate
newRefs.add(ref);
newStreams.add(new PRStream((PRStream)getPdfObject(ref), null));
}
else
visited.put(ref.getNumber(), 1);
}
}
}
if (newStreams.isEmpty())
return;
for (int k = 0; k < newStreams.size(); ++k) {
xrefObj.add(newStreams.get(k));
PRIndirectReference ref = newRefs.get(k);
ref.setNumber(xrefObj.size() - 1, 0);
}
}
}
class PdfReader.PageRefs {
protected PRIndirectReference getSinglePage(final int n) {
PdfDictionary acc = new PdfDictionary();
PdfDictionary top = reader.rootPages;
int base = 0;
while (true) {
for (int k = 0; k < pageInhCandidates.length; ++k) {
PdfObject obj = top.get(pageInhCandidates[k]);
if (obj != null)
acc.put(pageInhCandidates[k], obj);
}
PdfArray kids = (PdfArray)PdfReader.getPdfObjectRelease(top.get(PdfName.KIDS));
for (Iterator<PdfObject> it = kids.listIterator(); it.hasNext();) {
PRIndirectReference ref = (PRIndirectReference)it.next();
PdfDictionary dic = (PdfDictionary)getPdfObject(ref);
int last = reader.lastXrefPartial;
PdfObject count = getPdfObjectRelease(dic.get(PdfName.COUNT));
reader.lastXrefPartial = last;
int acn = 1;
if (count != null && count.type() == PdfObject.NUMBER)
acn = ((PdfNumber)count).intValue();
if (n < base + acn) {
if (count == null) {
dic.mergeDifferent(acc);
return ref;
}
reader.releaseLastXrefPartial();
top = dic;
break;
}
reader.releaseLastXrefPartial();
base += acn;
}
}
}
}
|