static PdfFonts() { using (var graphic = Graphics.FromHwnd(IntPtr.Zero)) using (var fontFamilly = new FontFamily("Arial")) { FONT_REGULAR = new PdfFont(graphic, fontFamilly, FontStyle.Regular, "Helvetica"); FONT_BOLD = new PdfFont(graphic, fontFamilly, FontStyle.Bold, "Helvetica-Bold"); FONT_OBLIQUE = new PdfFont(graphic, fontFamilly, FontStyle.Italic, "Helvetica−Oblique"); FONT_BOLD_OBLIQUE = new PdfFont(graphic, fontFamilly, FontStyle.Bold | FontStyle.Italic, "Helvetica−BoldOblique"); } }
/// <summary> /// Writes a multiple strings centered at the given position. /// </summary> /// <param name="lines">Text lines</param> /// <param name="offset">Offset of the text line</param> /// <param name="count">Count of lines to write</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="font">Font</param> /// <param name="fontSize">Font size in em</param> /// <param name="rgb">RGB color. Blue is the lower byte</param> /// <param name="linesWidthsPt">Widths of the lines</param> /// <param name="lineHeightPt">Height of a single line</param> public void WriteLinesCenter(List<string> lines, int offset, int count , int xPt, int yPt, PdfFont font, int fontSize, int rgb , float[] linesWidthsPt, float lineHeightPt) { yPt += (int)(lineHeightPt * 0.3); StringBuilder sb = new StringBuilder(); sb.Append("BT "); sb.Append(FormatRGB(rgb) + " rg "); //font color sb.Append(font.CodeName + " " + fontSize + " Tf\n"); //font name, size float y = yPt - lineHeightPt; count += offset; for (int i = offset; i < count; i++) { float x = xPt - linesWidthsPt[i] / 2; sb.Append("1 0 0 1 " + x + " " + y + " Tm ("); sb.Append(EscapeText(lines[i])); sb.Append(")Tj\n"); y -= lineHeightPt; } sb.Append("ET"); int id = WriteStreamObject(sb); _currentPage.AddContent(id); }
/// <summary> /// Writes the pages numbers /// </summary> /// <param name="font">Font</param> /// <param name="fontSize">Fonr size</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="rgb">RGB color. Blue is the lower byte</param> public void WritePagesNumbers(PdfFont font, int fontSize, int xPt, int yPt, int rgb) { string color = FormatRGB(rgb); int pageCount = _pages.Count; for (int i = 0; i < pageCount; i++) { PdfPage page = _pages[i]; string txt = (i + 1) + "/" + pageCount; float txtWidth = font.MesureWidth(txt, fontSize); int xPtAbs = xPt < 0 ? (int)(xPt + page.Width - txtWidth) : xPt; StringBuilder sb = new StringBuilder(); sb.Append("BT "); sb.Append(color + " rg "); sb.Append(font.CodeName + " " + fontSize + " Tf "); sb.Append(xPtAbs + " " + yPt + " Td\n"); sb.Append("(" + txt + ")Tj\n"); sb.Append("ET"); int id = WriteStreamObject(sb); page.AddContent(id); } }
/// <summary> /// Writes a multiple strings at the given position. /// </summary> /// <param name="lines">Text lines</param> /// <param name="offset">Offset of the text line</param> /// <param name="count">Count of lines to write</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="font">Font</param> /// <param name="fontSize">Font size in em</param> /// <param name="rgb">RGB color. Blue is the lower byte</param> /// <param name="lineHeightPt">Line height in points</param> public void WriteLines(List<string> lines, int offset, int count, int xPt , int yPt, PdfFont font, int fontSize, int rgb, float lineHeightPt) { yPt += (int)(lineHeightPt * 0.3); StringBuilder sb = new StringBuilder(); sb.Append("BT "); sb.Append(FormatRGB(rgb) + " rg "); //font color sb.Append(font.CodeName + " " + fontSize + " Tf "); //font name, size sb.Append(lineHeightPt + " TL "); //line height sb.Append(xPt + " " + yPt + " Td\n"); //x , y count += offset; for (int i = offset; i < count; i++) { sb.Append('('); sb.Append(EscapeText(lines[i])); sb.Append(")'\n"); } sb.Append("ET"); int id = WriteStreamObject(sb); _currentPage.AddContent(id); }