Esempio n. 1
0
        ////////////////////////////////////////////////////////////////////
        /// <summary>
        ///     Dispose PDF document object
        /// </summary>
        ////////////////////////////////////////////////////////////////////
        public void Dispose()
        {
            // close output file
            // Note: stream input will not be closed
            if (FileName != null && PdfFile != null)
            {
                PdfFile.Close();
                PdfFile = null;
            }

            // dispose all objects with IDisposable interface
            foreach (var Obj in ObjectArray)
            {
                if (Obj is IDisposable)
                {
                    ((IDisposable)Obj).Dispose();
                }
            }
        }
Esempio n. 2
0
        ////////////////////////////////////////////////////////////////////
        /// <summary>
        /// Create PDF document file
        /// </summary>
        /// <remarks>
        /// The last step of document creation after all pages were constructed.
        /// FileName is the path and name of the output file.
        /// </remarks>
        ////////////////////////////////////////////////////////////////////
        public void CreateFile()
        {
            // create page array
            StringBuilder Kids = new StringBuilder("[");

            for (Int32 Index = 0; Index < PageArray.Count; Index++)
            {
                Kids.AppendFormat("{0} 0 R ", PageArray[Index].ObjectNumber);
            }
            if (Kids.Length > 1)
            {
                Kids.Length--;
            }
            Kids.Append("]");
            PagesObject.Dictionary.Add("/Kids", Kids.ToString());

            // page count
            PagesObject.Dictionary.AddInteger("/Count", PageArray.Count);

            // objects
            for (Int32 Index = 0; Index < ObjectArray.Count; Index++)
            {
                ObjectArray[Index].WriteObjectHeaderToPdfFile();
            }

            // save cross reference table position
            Int32 XRefPos = (Int32)PdfFile.BaseStream.Position;

            // cross reference
            PdfFile.WriteFormat("xref\n0 {0}\n0000000000 65535 f \n", ObjectArray.Count + 1);
            foreach (PdfObject PO in ObjectArray)
            {
                PdfFile.WriteFormat("{0:0000000000} 00000 n \n", PO.FilePosition);
            }

            // trailer
            PdfFile.WriteFormat("trailer\n<</Size {0}/Root 1 0 R/ID [{1}{1}]{2}>>\nstartxref\n{3}\n",
                                ObjectArray.Count + 1, ByteArrayToPdfHexString(DocumentID),
                                Encryption == null ? String.Empty : String.Format("/Encrypt {0} 0 R", Encryption.ObjectNumber), XRefPos);

            // write PDF end of file marker
            PdfFile.WriteString("%%EOF\n");

            // close file
            PdfFile.Close();

            // dispose all FontApi resources
            foreach (PdfObject Obj in ObjectArray)
            {
                if (Obj.GetType() == typeof(PdfFont))
                {
                    ((PdfFont)Obj).FontInfo.Dispose();
                }
            }

            // dispose encryption resources
            if (Encryption != null)
            {
                Encryption.Dispose();
                Encryption = null;
            }

            // successful exit
            return;
        }