Ejemplo n.º 1
0
 public Document()
 {
     PageRoot = new PageRoot
     {
         Document = this
     };
     _catalog = new Catalog
     {
         Document = this
     };
     Fonts.AddRange(TypeFaces.Fonts.Styles());
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Called to produce the document stream
        /// </summary>
        /// <param name="baseStream"></param>
        public void Publish(Stream baseStream)
        {
            using (StreamWriter stream = new StreamWriter(baseStream, new UTF8Encoding(false)))
            {
                stream.NewLine = "\n";

                // PDF Header
                stream.WriteLine("%PDF-{0}", "1.4"); // the version of PDF 
                stream.WriteLine("%\x82\x82\x82\x82"); // needed to allow editors to know that this is a binary file

                // place all the document objects into postion
                var objects = PositionObjects();

                // then get all the data ready for the document
                PrepareStreams();

                // Call publish on all the child objects
                _catalog.Publish(stream);

                foreach (var font in Fonts)
                {
                    font.Publish(stream);
                }

                foreach (var image in Images)
                {
                    image.Publish(stream);
                }

                PageRoot.Publish(stream);

                foreach (var headerFooter in HeadersFooters)
                {
                    headerFooter.Publish(stream);
                }

                foreach (var page in Pages)
                {
                    foreach (var content in page.Contents)
                    {
                        content.Publish(stream);
                    }
                }

                foreach (var page in Pages)
                {
                    page.Publish(Fonts, stream);
                }

                //// Document attributes
                Properties.Publish(stream);

                // PDF Cross Reference
                var startXref = stream.BaseStream.Position;
                var xref = "xref\n";
                xref += string.Format("0 {0}\n", objects + 1);
                xref += CreateXRefTable();

                stream.Write(xref);

                // PDF Trailer
                var trailer = "trailer\n";
                trailer += string.Format("<</Size {0}\n", objects + 1);
                trailer += "/Root 1 0 R\n";

                if (Properties.ObjectNumber > 0)
                {
                    trailer += string.Format("/Info {0} 0 R\n", Properties.ObjectNumber);
                }
                trailer += ">>\n";
                trailer += "startxref\n";
                trailer += startXref.ToString() + "\n";
                trailer += "%%EOF";

                // PDF END
                stream.Write(trailer);

                stream.Flush();
            }
        }