Example #1
0
        /// <summary>
        /// Closes this instance.
        /// </summary>
        public void Close()
        {
            if (!CanModify)
            {
                throw new InvalidOperationException(PSSR.CannotModify);
            }

            if (OutStream != null)
            {
                // Get security handler if document gets encrypted
                PDFStandardSecurityHandler securityHandler = null;
                if (SecuritySettings.DocumentSecurityLevel != PDFDocumentSecurityLevel.None)
                {
                    securityHandler = SecuritySettings.SecurityHandler;
                }

                PDFWriter writer = new PDFWriter(OutStream, securityHandler);
                try
                {
                    DoSave(writer);
                }
                finally
                {
                    writer.Close();
                }
            }
        }
Example #2
0
        /// <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 = null;

            try
            {
                writer = new PDFWriter(stream, securityHandler);
                DoSave(writer);
            }
            finally
            {
                if (stream is Stream)
                {
                    if (closeStream)
                    {
                        stream.Close();
                    }
                    else if (stream.CanRead == true && stream.CanSeek == true)
                    {
                        stream.Position = 0;     // Reset the stream position if the stream is kept open.
                    }
                }

                writer?.Close(closeStream);
            }
        }