/// <summary> /// Saves the document to the specified stream. /// </summary> public void Save(Stream stream, bool closeStream) { if (!CanModify) throw new InvalidOperationException(PSSR.CannotModify); // TODO: more diagnostic checks string message = ""; if (!CanSave(ref message)) throw new PdfSharpException(message); // Get security handler if document gets encrypted PdfStandardSecurityHandler securityHandler = null; if (SecuritySettings.DocumentSecurityLevel != PdfDocumentSecurityLevel.None) securityHandler = SecuritySettings.SecurityHandler; PdfWriter writer = new PdfWriter(stream, securityHandler); try { DoSave(writer); } finally { if (stream != null) { if (closeStream) stream.Close(); else stream.Position = 0; // Reset the stream position if the stream is left open. } if (writer != null) writer.Close(closeStream); } }
/// <summary> /// Closes this instance. /// </summary> public void Close() { if (!CanModify) throw new InvalidOperationException(PSSR.CannotModify); if (this.outStream != null) { // Get security handler if document gets encrypted PdfStandardSecurityHandler securityHandler = null; if (SecuritySettings.DocumentSecurityLevel != PdfDocumentSecurityLevel.None) securityHandler = SecuritySettings.SecurityHandler; PdfWriter writer = new PdfWriter(this.outStream, securityHandler); try { DoSave(writer); } finally { writer.Close(); } } }