/// <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"); }
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"); }
/// <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"); }
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"); }
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"); }
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"); }
/// <summary> /// Adds an image. /// Encodes in JPEG if the image is already in JPEG and in PNG otherwise. /// </summary> /// <param name="bitmap">Bitmap</param> /// <param name="xPt">X position from the bottom in points</param> /// <param name="yPt">Y position from the bottom in points</param> /// <param name="widthPt">Width in points</param> /// <param name="heightPt">Height in points</param> public void WriteBitmap(Bitmap bitmap, int xPt, int yPt, int widthPt, int heightPt) { int idImg = _xRefs.CreateObject(); int idImgLength = _xRefs.CreateObject(); string imgName = "/I" + idImg; bool isJpeg = bitmap.RawFormat.Guid == ImageFormat.Jpeg.Guid; int dataLength = 0; _xRefs.RegisterObject(idImg); _pdfWriter.WriteLine(idImg + " 0 obj"); _pdfWriter.WriteLine("<<"); _pdfWriter.WriteLine("/Type /XObject"); _pdfWriter.WriteLine("/Subtype /Image"); _pdfWriter.WriteLine("/ColorSpace /DeviceRGB"); _pdfWriter.WriteLine("/BitsPerComponent 8"); if (isJpeg) { _pdfWriter.WriteLine("/Filter /DCTDecode"); } else { _pdfWriter.WriteLine("/Filter /FlateDecode"); _pdfWriter.WriteLine("/DecodeParms <<"); _pdfWriter.WriteLine(" /Columns " + bitmap.Width); _pdfWriter.WriteLine(" /Colors 3"); _pdfWriter.WriteLine(" /Predictor 15"); _pdfWriter.WriteLine(">>"); } _pdfWriter.WriteLine("/Name " + imgName); _pdfWriter.WriteLine("/Height " + bitmap.Height); _pdfWriter.WriteLine("/Width " + bitmap.Width); _pdfWriter.WriteLine("/Length " + idImgLength + " 0 R"); _pdfWriter.WriteLine(">>"); _pdfWriter.WriteLine("stream"); _pdfWriter.Flush(); if (isJpeg) { dataLength = PdfImage.WriteJPEG(bitmap, _pdfStream); } else { dataLength = PdfImage.WritePNG(bitmap, _pdfStream); } _pdfWriter.WriteLine(); _pdfWriter.WriteLine("endstream"); _pdfWriter.WriteLine("endobj"); //Writes the length object _xRefs.RegisterObject(idImgLength); _pdfWriter.WriteLine(idImgLength + " 0 obj"); _pdfWriter.WriteLine(dataLength); _pdfWriter.WriteLine("endobj"); //create image content and borders StringBuilder sb = new StringBuilder(); sb.Append("q "); sb.Append(widthPt + " 0 0 " + heightPt + " " + xPt + " " + yPt + " cm "); sb.Append(imgName); sb.Append(" Do Q\n"); sb.Append("0.5 w 0.2 G "); sb.Append(xPt + " " + yPt + " " + widthPt + " " + heightPt + " re s"); int idContent = WriteStreamObject(sb); //add the created objects to the page _currentPage.AddContent(idContent); _currentPage.AddXObject(idImg, imgName); }