In Method: | writeCrossReferenceTable(OutputStream, PdfIndirectReference, PdfIndirectReference, PdfIndirectReference, PdfObject, long) |
Code with Finding: |
class PdfWriter.PdfBody {
void writeCrossReferenceTable(final OutputStream os, final PdfIndirectReference root, final PdfIndirectReference info, final PdfIndirectReference encryption, final PdfObject fileID, final long prevxref) throws IOException {
int refNumber = 0;
if (writer.isFullCompression()) {
flushObjStm();
refNumber = getIndirectReferenceNumber();
xrefs.add(new PdfCrossReference(refNumber, position));
}
PdfCrossReference entry = xrefs.first();
int first = entry.getRefnum();
int len = 0;
ArrayList<Integer> sections = new ArrayList<Integer>();
for (PdfCrossReference pdfCrossReference : xrefs) {
entry = pdfCrossReference;
if (first + len == entry.getRefnum())
++len;
else {
sections.add(Integer.valueOf(first));
sections.add(Integer.valueOf(len));
first = entry.getRefnum();
len = 1;
}
}
sections.add(Integer.valueOf(first));
sections.add(Integer.valueOf(len));
if (writer.isFullCompression()) {
int mid = 5;
long mask = 0xff00000000L;
for (; mid > 1; --mid) {
if ((mask & position) != 0)
break;
mask >>>= 8;
}
ByteBuffer buf = new ByteBuffer();
for (Object element : xrefs) {
entry = (PdfCrossReference) element;
entry.toPdf(mid, buf);
}
PdfStream xr = new PdfStream(buf.toByteArray());
buf = null;
xr.flateCompress(writer.getCompressionLevel());
xr.put(PdfName.SIZE, new PdfNumber(size()));
xr.put(PdfName.ROOT, root);
if (info != null) {
xr.put(PdfName.INFO, info);
}
if (encryption != null)
xr.put(PdfName.ENCRYPT, encryption);
if (fileID != null)
xr.put(PdfName.ID, fileID);
xr.put(PdfName.W, new PdfArray(new int[]{1, mid, 2}));
xr.put(PdfName.TYPE, PdfName.XREF);
PdfArray idx = new PdfArray();
for (int k = 0; k < sections.size(); ++k)
idx.add(new PdfNumber(sections.get(k).intValue()));
xr.put(PdfName.INDEX, idx);
if (prevxref > 0)
xr.put(PdfName.PREV, new PdfNumber(prevxref));
PdfEncryption enc = writer.crypto;
writer.crypto = null;
PdfIndirectObject indirect = new PdfIndirectObject(refNumber, xr, writer);
indirect.writeTo(writer.getOs());
writer.crypto = enc;
}
else {
os.write(getISOBytes("xref\n"));
Iterator<PdfCrossReference> i = xrefs.iterator();
for (int k = 0; k < sections.size(); k += 2) {
first = sections.get(k).intValue();
len = sections.get(k + 1).intValue();
os.write(getISOBytes(String.valueOf(first)));
os.write(getISOBytes(" "));
os.write(getISOBytes(String.valueOf(len)));
os.write('\n');
while (len-- > 0) {
entry = i.next();
entry.toPdf(os);
}
}
}
}
}
class PdfWriter {
protected void addSharedObjectsToBody() throws IOException {
// [F3] add the fonts
for (FontDetails details : documentFonts.values()) {
details.writeFont(this);
}
// [F4] add the form XObjects
for (Object objs[] : formXObjects.values()) {
PdfTemplate template = (PdfTemplate)objs[1];
if (template != null && template.getIndirectReference() instanceof PRIndirectReference)
continue;
if (template != null && template.getType() == PdfTemplate.TYPE_TEMPLATE) {
addToBody(template.getFormXObject(compressionLevel), template.getIndirectReference());
}
}
// [F5] add all the dependencies in the imported pages
for (PdfReaderInstance element : readerInstances.values()) {
currentPdfReaderInstance= element;
currentPdfReaderInstance.writeAllPages();
}
currentPdfReaderInstance = null;
// [F6] add the spotcolors
for (ColorDetails color : documentColors.values()) {
addToBody(color.getSpotColor(this), color.getIndirectReference());
}
// [F7] add the pattern
for (PdfPatternPainter pat : documentPatterns.keySet()) {
addToBody(pat.getPattern(compressionLevel), pat.getIndirectReference());
}
// [F8] add the shading patterns
for (PdfShadingPattern shadingPattern : documentShadingPatterns) {
shadingPattern.addToBody();
}
// [F9] add the shadings
for (PdfShading shading : documentShadings) {
shading.addToBody();
}
// [F10] add the extgstate
for (Map.Entry<PdfDictionary, PdfObject[]>entry : documentExtGState.entrySet()) {
PdfDictionary gstate = entry.getKey();
PdfObject obj[] = entry.getValue();
addToBody(gstate, (PdfIndirectReference)obj[1]);
}
// [F11] add the properties
for (Map.Entry<Object, PdfObject[]>entry : documentProperties.entrySet()) {
Object prop = entry.getKey();
PdfObject[] obj = entry.getValue();
if (prop instanceof PdfLayerMembership){
PdfLayerMembership layer = (PdfLayerMembership)prop;
addToBody(layer.getPdfObject(), layer.getRef());
}
else if (prop instanceof PdfDictionary && !(prop instanceof PdfLayer)){
addToBody((PdfDictionary)prop, (PdfIndirectReference)obj[1]);
}
}
}
}
|