Exemple #1
0
        /// <summary>
        /// Writes the pages
        /// </summary>
        public void Write(StreamWriter writer, PdfXRefs xrefs)
        {
            int count = _pages.Count;

            foreach (PdfPage page in _pages)
            {
                xrefs.RegisterObject(page.Id);
                writer.WriteLine(page.Id + " 0 obj");
                writer.WriteLine("<<");
                writer.WriteLine("/Type /Page");
                writer.WriteLine("/Parent " + PdfXRefs.ID_PAGES + " 0 R");
                writer.WriteLine("/MediaBox [0 0 " + page.Width + " " + page.Height + "]");
                writer.Write("/Contents [ ");
                foreach (int id in page.Contents)
                {
                    writer.Write(id + " 0 R ");
                }
                writer.WriteLine(']');
                writer.WriteLine("/Resources <<");
                writer.WriteLine(" /ProcSet [/PDF/Text/ImageC]");
                writer.WriteLine(" /Font " + PdfXRefs.ID_FONTS + " 0 R");
                if (page.XObjects.Count > 0)
                {
                    writer.Write(" /XObject << ");
                    foreach (var item in page.XObjects)
                    {
                        writer.Write(item.CodeName + " " + item.Id + " 0 R ");
                    }
                    writer.WriteLine(">>");
                }
                writer.WriteLine(">>");
                if (page.Annots.Count > 0)
                {
                    writer.Write("/Annots[ ");
                    foreach (int id in page.Annots)
                    {
                        writer.Write(id + " 0 R ");
                    }
                    writer.WriteLine(']');
                }
                writer.WriteLine(">>");
                writer.WriteLine("endobj");
            }

            //Writes the pages objects
            xrefs.RegisterObject(PdfXRefs.ID_PAGES);

            writer.WriteLine(PdfXRefs.ID_PAGES + " 0 obj");
            writer.WriteLine("<<");
            writer.WriteLine("/Type /Pages");
            writer.Write("/Kids [ ");
            foreach (PdfPage page in _pages)
            {
                writer.Write(page.Id + " 0 R ");
            }
            writer.WriteLine("]");
            writer.WriteLine("/Count " + _pages.Count);
            writer.WriteLine(">>");
            writer.WriteLine("endobj");
        }
Exemple #2
0
 public PdfWriter() {
     Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
     Encoding encoding = Encoding.GetEncoding(1252);
     _pdfStream = new MemoryStream(1024 * 10);
     _pdfWriter = new StreamWriter(_pdfStream, encoding);
     _xRefs = new PdfXRefs(_pdfWriter);
     _outlines = new PdfOutlines();
     _pages = new PdfPages();
     _pdfWriter.NewLine = "\n";
     _pdfWriter.WriteLine(FILE_HEADER);
 }
Exemple #3
0
        public PdfWriter()
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
            Encoding encoding = Encoding.GetEncoding(1252);

            _pdfStream         = new MemoryStream(1024 * 10);
            _pdfWriter         = new StreamWriter(_pdfStream, encoding);
            _xRefs             = new PdfXRefs(_pdfWriter);
            _outlines          = new PdfOutlines();
            _pages             = new PdfPages();
            _pdfWriter.NewLine = "\n";
            _pdfWriter.WriteLine(FILE_HEADER);
        }
Exemple #4
0
        public void Write(StreamWriter writer, PdfXRefs xrefs)
        {
            //Write the outlines entries
            int count = _outlines.Count;

            for (int i = 0; i < count; i++)
            {
                PdfOutline outline = _outlines[i];
                xrefs.RegisterObject(outline.Id);
                writer.WriteLine(outline.Id + " 0 obj");
                writer.WriteLine("<<");
                writer.WriteLine("/Title (" + PdfWriter.EscapeText(outline.Text) + ")");
                writer.WriteLine("/Parent " + PdfXRefs.ID_OUTLINES + " 0 R");
                writer.Write("/Dest [ ");
                writer.Write(outline.Page);
                writer.Write(" 0 R /XYZ null ");
                writer.Write(outline.Y == -1 ? "null" : outline.Y.ToString());
                writer.WriteLine(" 0 ]");
                if (i > 0)
                {
                    writer.WriteLine("/Prev " + _outlines[i - 1].Id + " 0 R");
                }
                if (i < count - 1)
                {
                    writer.WriteLine("/Next " + _outlines[i + 1].Id + " 0 R");
                }
                writer.WriteLine(">>");
                writer.WriteLine("endobj");
            }

            //Write the main entry
            xrefs.RegisterObject(PdfXRefs.ID_OUTLINES);
            writer.WriteLine(PdfXRefs.ID_OUTLINES + " 0 obj");
            writer.WriteLine("<<");
            writer.WriteLine("/Type /Outlines");
            if (_outlines.Count == 0)
            {
                writer.WriteLine("/Count 0");
                writer.WriteLine("/First null");
                writer.WriteLine("/Last null");
            }
            else
            {
                writer.WriteLine("/Count " + count);
                writer.WriteLine("/First " + _outlines[0].Id + " 0 R");
                writer.WriteLine("/Last " + _outlines[count - 1].Id + " 0 R");
            }
            writer.WriteLine(">>");
            writer.WriteLine("endobj");
        }
Exemple #5
0
        /// <summary>
        /// Writes the pages
        /// </summary>
        public void Write(StreamWriter writer, PdfXRefs xrefs) {
            int count = _pages.Count;
            foreach (PdfPage page in _pages) {
                xrefs.RegisterObject(page.Id);
                writer.WriteLine(page.Id + " 0 obj");
                writer.WriteLine("<<");
                writer.WriteLine("/Type /Page");
                writer.WriteLine("/Parent " + PdfXRefs.ID_PAGES + " 0 R");
                writer.WriteLine("/MediaBox [0 0 " + page.Width + " " + page.Height + "]");
                writer.Write("/Contents [ ");
                foreach (int id in page.Contents)
                    writer.Write(id + " 0 R ");
                writer.WriteLine(']');
                writer.WriteLine("/Resources <<");
                writer.WriteLine(" /ProcSet [/PDF/Text/ImageC]");
                writer.WriteLine(" /Font " + PdfXRefs.ID_FONTS + " 0 R");
                if (page.XObjects.Count > 0) {
                    writer.Write(" /XObject << ");
                    foreach (var item in page.XObjects)
                        writer.Write(item.CodeName + " " + item.Id + " 0 R ");
                    writer.WriteLine(">>");
                }
                writer.WriteLine(">>");
                if (page.Annots.Count > 0) {
                    writer.Write("/Annots[ ");
                    foreach (int id in page.Annots) {
                        writer.Write(id + " 0 R ");
                    }
                    writer.WriteLine(']');
                }
                writer.WriteLine(">>");
                writer.WriteLine("endobj");
            }

            //Writes the pages objects
            xrefs.RegisterObject(PdfXRefs.ID_PAGES);

            writer.WriteLine(PdfXRefs.ID_PAGES + " 0 obj");
            writer.WriteLine("<<");
            writer.WriteLine("/Type /Pages");
            writer.Write("/Kids [ ");
            foreach (PdfPage page in _pages) {
                writer.Write(page.Id + " 0 R ");
            }
            writer.WriteLine("]");
            writer.WriteLine("/Count " + _pages.Count);
            writer.WriteLine(">>");
            writer.WriteLine("endobj");
        }
Exemple #6
0
        public static void Write(StreamWriter writer, PdfXRefs xrefs)
        {
            PdfFont[] fonts = new[] {
                PdfFonts.FONT_REGULAR,
                PdfFonts.FONT_BOLD,
                PdfFonts.FONT_OBLIQUE,
                PdfFonts.FONT_BOLD_OBLIQUE
            };
            int[] fontIds = new int[fonts.Length];

            for (int i = 0; i < fonts.Length; i++)
            {
                fontIds[i] = xrefs.CreateAndRegisterObject();

                writer.WriteLine(fontIds[i] + " 0 obj");
                writer.WriteLine("<<");
                writer.WriteLine("/Type /Font");
                writer.WriteLine("/Subtype /Type1");
                writer.WriteLine("/BaseFont /" + fonts[i].Name);
                writer.WriteLine("/Encoding /WinAnsiEncoding");

                /*
                 * _pdfWriter.Write("/FirstChar 0");
                 * _pdfWriter.Write("/LastChar 255");
                 * _pdfWriter.Write("/Widths[");
                 * foreach (short w in font.WidthsTs) {
                 *  _pdfWriter.Write(w);
                 *  _pdfWriter.Write(' ');
                 * }
                 * _pdfWriter.Write(']');
                 */
                writer.WriteLine(">>");
                writer.WriteLine("endobj");
            }

            xrefs.RegisterObject(PdfXRefs.ID_FONTS);
            writer.WriteLine(PdfXRefs.ID_FONTS + " 0 obj");
            writer.Write("<< ");
            for (int i = 0; i < fonts.Length; i++)
            {
                writer.Write(fonts[i].CodeName + " " + fontIds[i] + " 0 R ");
            }
            writer.WriteLine(">>");
            writer.WriteLine("endobj");
        }
Exemple #7
0
        public static void Write(StreamWriter writer, PdfXRefs xrefs) {
            PdfFont[] fonts = new[]{
                PdfFonts.FONT_REGULAR, 
                PdfFonts.FONT_BOLD,
                PdfFonts.FONT_OBLIQUE, 
                PdfFonts.FONT_BOLD_OBLIQUE
            };
            int[] fontIds = new int[fonts.Length];

            for (int i = 0; i < fonts.Length; i++) {
                fontIds[i] = xrefs.CreateAndRegisterObject();

                writer.WriteLine(fontIds[i] + " 0 obj");
                writer.WriteLine("<<");
                writer.WriteLine("/Type /Font");
                writer.WriteLine("/Subtype /Type1");
                writer.WriteLine("/BaseFont /" + fonts[i].Name);
                writer.WriteLine("/Encoding /WinAnsiEncoding");
                /*
                _pdfWriter.Write("/FirstChar 0");
                _pdfWriter.Write("/LastChar 255");
                _pdfWriter.Write("/Widths[");
                foreach (short w in font.WidthsTs) {
                    _pdfWriter.Write(w);
                    _pdfWriter.Write(' ');
                }
                _pdfWriter.Write(']');
                */
                writer.WriteLine(">>");
                writer.WriteLine("endobj");
            }

            xrefs.RegisterObject(PdfXRefs.ID_FONTS);
            writer.WriteLine(PdfXRefs.ID_FONTS + " 0 obj");
            writer.Write("<< ");
            for (int i = 0; i < fonts.Length; i++) {
                writer.Write(fonts[i].CodeName + " " + fontIds[i] + " 0 R ");
            }
            writer.WriteLine(">>");
            writer.WriteLine("endobj");
        }
Exemple #8
0
        public void Write(StreamWriter writer, PdfXRefs xrefs) {

            //Write the outlines entries
            int count = _outlines.Count;
            for (int i = 0; i < count; i++) {
                PdfOutline outline = _outlines[i];
                xrefs.RegisterObject(outline.Id);
                writer.WriteLine(outline.Id + " 0 obj");
                writer.WriteLine("<<");
                writer.WriteLine("/Title (" + PdfWriter.EscapeText(outline.Text) + ")");
                writer.WriteLine("/Parent " + PdfXRefs.ID_OUTLINES + " 0 R");
                writer.Write("/Dest [ ");
                writer.Write(outline.Page);
                writer.Write(" 0 R /XYZ null ");
                writer.Write(outline.Y == -1 ? "null" : outline.Y.ToString());
                writer.WriteLine(" 0 ]");
                if (i > 0)
                    writer.WriteLine("/Prev " + _outlines[i - 1].Id + " 0 R");
                if (i < count - 1)
                    writer.WriteLine("/Next " + _outlines[i + 1].Id + " 0 R");
                writer.WriteLine(">>");
                writer.WriteLine("endobj");
            }

            //Write the main entry
            xrefs.RegisterObject(PdfXRefs.ID_OUTLINES);
            writer.WriteLine(PdfXRefs.ID_OUTLINES + " 0 obj");
            writer.WriteLine("<<");
            writer.WriteLine("/Type /Outlines");
            if (_outlines.Count == 0) {
                writer.WriteLine("/Count 0");
                writer.WriteLine("/First null");
                writer.WriteLine("/Last null");
            } else {
                writer.WriteLine("/Count " + count);
                writer.WriteLine("/First " + _outlines[0].Id + " 0 R");
                writer.WriteLine("/Last " + _outlines[count - 1].Id + " 0 R");
            }
            writer.WriteLine(">>");
            writer.WriteLine("endobj");
        }