Code with Finding: |
class PdfWriter {
/**
* Signals that the <CODE>Document</CODE> was closed and that no other
* <CODE>Elements</CODE> will be added.
* <P>
* The pages-tree is built and written to the outputstream.
* A Catalog is constructed, as well as an Info-object,
* the reference table is composed and everything is written
* to the outputstream embedded in a Trailer.
* @see com.itextpdf.text.DocWriter#close()
*/
@Override
public void close() {
if (open) {
if (currentPageNumber - 1 != pageReferences.size())
throw new RuntimeException("The page " + pageReferences.size() +
" was requested but the document has only " + (currentPageNumber - 1) + " pages.");
pdf.close();
try {
addSharedObjectsToBody();
for (PdfOCG layer : documentOCG) {
addToBody(layer.getPdfObject(), layer.getRef());
}
// add the root to the body
PdfIndirectReference rootRef = root.writePageTree();
// make the catalog-object and add it to the body
PdfDictionary catalog = getCatalog(rootRef);
// [C9] if there is XMP data to add: add it
if (xmpMetadata != null) {
PdfStream xmp = new PdfStream(xmpMetadata);
xmp.put(PdfName.TYPE, PdfName.METADATA);
xmp.put(PdfName.SUBTYPE, PdfName.XML);
if (crypto != null && !crypto.isMetadataEncrypted()) {
PdfArray ar = new PdfArray();
ar.add(PdfName.CRYPT);
xmp.put(PdfName.FILTER, ar);
}
catalog.put(PdfName.METADATA, body.add(xmp).getIndirectReference());
}
// [C10] make pdfx conformant
if (isPdfX()) {
pdfxConformance.completeInfoDictionary(getInfo());
pdfxConformance.completeExtraCatalog(getExtraCatalog());
}
// [C11] Output Intents
if (extraCatalog != null) {
catalog.mergeDifferent(extraCatalog);
}
writeOutlines(catalog, false);
// add the Catalog to the body
PdfIndirectObject indirectCatalog = addToBody(catalog, false);
// add the info-object to the body
PdfIndirectObject infoObj = addToBody(getInfo(), false);
// [F1] encryption
PdfIndirectReference encryption = null;
PdfObject fileID = null;
body.flushObjStm();
if (crypto != null) {
PdfIndirectObject encryptionObject = addToBody(crypto.getEncryptionDictionary(), false);
encryption = encryptionObject.getIndirectReference();
fileID = crypto.getFileID();
}
else
fileID = PdfEncryption.createInfoId(PdfEncryption.createDocumentId());
// write the cross-reference table of the body
body.writeCrossReferenceTable(os, indirectCatalog.getIndirectReference(),
infoObj.getIndirectReference(), encryption, fileID, prevxref);
// make the trailer
// [F2] full compression
if (fullCompression) {
os.write(getISOBytes("startxref\n"));
os.write(getISOBytes(String.valueOf(body.offset())));
os.write(getISOBytes("\n%%EOF\n"));
}
else {
PdfTrailer trailer = new PdfTrailer(body.size(),
body.offset(),
indirectCatalog.getIndirectReference(),
infoObj.getIndirectReference(),
encryption,
fileID, prevxref);
trailer.toPdf(this, os);
}
super.close();
}
catch(IOException ioe) {
throw new ExceptionConverter(ioe);
}
}
}
}
|