public override void Close()
{
if (open) {
if ((currentPageNumber - 1) != pageReferences.Count)
throw new Exception("The page " + pageReferences.Count +
" was requested but the document has only " + (currentPageNumber - 1) + " pages.");
pdf.Close();
AddSharedObjectsToBody();
foreach (IPdfOCG layer in documentOCG.Keys) {
AddToBody(layer.PdfObject, layer.Ref);
}
// 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).IndirectReference);
}
// [C10] make pdfx conformant
if (IsPdfX()) {
CompleteInfoDictionary(Info);
CompleteExtraCatalog(ExtraCatalog);
}
// [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(Info, false);
// [F1] encryption
PdfIndirectReference encryption = null;
PdfObject fileID = null;
body.FlushObjStm();
if (crypto != null) {
PdfIndirectObject encryptionObject = AddToBody(crypto.GetEncryptionDictionary(), false);
encryption = encryptionObject.IndirectReference;
fileID = crypto.FileID;
}
else
fileID = PdfEncryption.CreateInfoId(PdfEncryption.CreateDocumentId());
// write the cross-reference table of the body
body.WriteCrossReferenceTable(os, indirectCatalog.IndirectReference,
infoObj.IndirectReference, encryption, fileID, prevxref);
// make the trailer
// [F2] full compression
if (fullCompression) {
WriteKeyInfo(os);
byte[] tmp = GetISOBytes("startxref\n");
os.Write(tmp, 0, tmp.Length);
tmp = GetISOBytes(body.Offset.ToString());
os.Write(tmp, 0, tmp.Length);
tmp = GetISOBytes("\n%%EOF\n");
os.Write(tmp, 0, tmp.Length);
}
else {
PdfTrailer trailer = new PdfTrailer(body.Size,
body.Offset,
indirectCatalog.IndirectReference,
infoObj.IndirectReference,
encryption,
fileID, prevxref);
trailer.ToPdf(this, os);
}
base.Close();
}
}