internal string GetPdfFont(StyleInfo si) { string face = FontNameNormalize(si.FontFamily); if (face == "Times-Roman" && (si.IsFontBold() || si.FontStyle == FontStyleEnum.Italic)) { face = "Times"; } if (si.IsFontBold() && si.FontStyle == FontStyleEnum.Italic) // bold and italic? { face = face + "-BoldOblique"; } else if (si.IsFontBold()) // just bold? { face = face + "-Bold"; } else if (si.FontStyle == FontStyleEnum.Italic) { face = face + "-Oblique"; } return(GetPdfFont(face)); }
internal string GetPdfFont(StyleInfo si) { string face = FontNameNormalize(si.FontFamily); if (face == "Times-Roman" && (si.IsFontBold() || si.FontStyle == FontStyleEnum.Italic)) face = "Times"; if (si.IsFontBold() && si.FontStyle == FontStyleEnum.Italic) // bold and italic? face = face + "-BoldOblique"; else if (si.IsFontBold()) // just bold? face = face + "-Bold"; else if (si.FontStyle == FontStyleEnum.Italic) face = face + "-Oblique"; return GetPdfFont(face); }
private void DoStyle(Style style, Row row) { if (style == null) { return; } StyleInfo si = style.GetStyleInfo(r, row); // tw.Write(@"\plain"); // reset current attributes // Handle the font if (!_Fonts.Contains(si.FontFamily)) { _Fonts.Add(si.FontFamily); } int fc = _Fonts.IndexOf(si.FontFamily); tw.Write(@"\f{0} ", fc); if (si.IsFontBold()) { tw.Write(@"\b"); } if (si.FontStyle == FontStyleEnum.Italic) { tw.Write(@"\i"); } switch (si.TextDecoration) { case TextDecorationEnum.Underline: tw.Write(@"\ul"); break; case TextDecorationEnum.LineThrough: tw.Write(@"\strike"); break; default: break; } tw.Write(@"\fs{0}", (int)Math.Round(si.FontSize * 2, 0)); // font size // Handle the color int ic; if (!_Colors.Contains(si.Color)) { _Colors.Add(si.Color); } ic = _Colors.IndexOf(si.Color) + 1; tw.Write(@"\cf{0} ", ic); }
internal string GetPdfFont(StyleInfo si) { string face = FontNameNormalize(si.FontFamily); //if (face == "AdobeSongStd-Light") //{ //} //else if (face == "Times-Roman") if (face == "Times-Roman") { if (si.IsFontBold() && si.FontStyle == FontStyleEnum.Italic) { face = "Times-BoldItalic"; } else if (si.IsFontBold()) { face = "Times-Bold"; } else if (si.FontStyle == FontStyleEnum.Italic) { face = "Times-Italic"; } } else if (si.IsFontBold() && si.FontStyle == FontStyleEnum.Italic) // bold and italic? { face = face + "-BoldOblique"; } else if (si.IsFontBold()) // just bold? { face = face + "-Bold"; } else if (si.FontStyle == FontStyleEnum.Italic) { face = face + "-Oblique"; } return(GetPdfFont(face)); }
int GetFontIndex(StyleInfo si) { StringBuilder sb = new StringBuilder(150); sb.Append("<font>"); sb.Append(GetColor(si.Color)); sb.AppendFormat("<sz val=\"{0}\"/> ", si.FontSize); sb.AppendFormat("<name val=\"{0}\"/> ", si.FontFamily); if (si.IsFontBold()) { sb.Append("<b /> "); } if (si.FontStyle == FontStyleEnum.Italic) { sb.Append("<i /> "); } sb.Append("</font>"); int i = _FontCache.GetIndex(sb.ToString()); return(i); }
/// <summary> /// Page Text element at the X Y position; multiple lines handled /// </summary> /// <returns></returns> private void iAddText (float x, float y, float height, float width, string[] sa, StyleInfo si, PdfFonts fonts, float[] tw, bool bWrap, string url, bool bNoClip, string tooltip) { int platform = (int)Environment.OSVersion.Platform; int version = (int)Environment.OSVersion.Version.Major; BaseFont bf; string face = iFontNameNormalize (si.FontFamily); string fontname = ""; bool fonttype1 = true; var folder = FontFolder; //Call to determine folder and set value of dejavuFonts; if (face == "Times-Roman") { if (si.IsFontBold () && si.FontStyle == FontStyleEnum.Italic) { //OSX if ((platform == 4 || platform == 6 || platform == 128) && version > 8) { face = "TimesNewRomanPS-BoldItalicMT"; fontname = "Times New Roman Bold Italic.ttf"; } else { face = dejavuFonts ? "DejaVu Serif Condensed Bold Italic" : "Times-BoldItalic"; fontname = (dejavuFonts ? "DejaVuSerifCondensed-BoldItalic.ttf" : "timesbi.ttf"); } } else if (si.IsFontBold ()) { //OSX if ((platform == 4 || platform == 6 || platform == 128) && version > 8) { face = "TimesNewRomanPS-BoldMT"; fontname = "Times New Roman Bold.ttf"; } else { face = dejavuFonts ? "DejaVu Serif Condensed Bold" : "Times-Bold"; fontname = (dejavuFonts ? "DejaVuSerifCondensed-Bold.ttf" : "timesbd.ttf"); } } else if (si.FontStyle == FontStyleEnum.Italic) { //OSX if ((platform == 4 || platform == 6 || platform == 128) && version > 8) { face = "TimesNewRomanPS-ItalicMT"; fontname = "Times New Roman Italic.ttf"; } else { face = dejavuFonts ? "DejaVu Serif Condensed Italic" : "Times-Italic"; fontname = (dejavuFonts ? "DejaVuSerifCondensed-Italic.ttf" : "timesi.ttf"); } } else { //OSX if ((platform == 4 || platform == 6 || platform == 128) && version > 8) { face = "TimesNewRomanPSMT"; fontname = "Times New Roman.ttf"; } else { face = dejavuFonts ? "DejaVu Serif Condensed" : face; fontname = (dejavuFonts ? "DejaVuSerifCondensed.ttf" : "times.ttf"); } } fonttype1 = false; } else if (face == "Arial") { if (si.IsFontBold () && si.FontStyle == FontStyleEnum.Italic) { //OSX if ((platform == 4 || platform == 6 || platform == 128) && version > 8) { face = "Arial BoldItalicMT"; fontname = "Arial Bold Italic.ttf"; } else { face = dejavuFonts ? "DejaVu Sans Condensed Bold Oblique" : "Arial-BoldItalic"; fontname = (dejavuFonts ? "DejaVuSansCondensed-BoldOblique.ttf" : "arialbi.ttf"); } } else if (si.IsFontBold ()) { //OSX if ((platform == 4 || platform == 6 || platform == 128) && version > 8) { face = "Arial-BoldMT"; fontname = "Arial Bold.ttf"; } else { face = dejavuFonts ? "DejaVu Sans Condensed Bold" : "Arial-Bold"; fontname = (dejavuFonts ? "DejaVuSansCondensed-Bold.ttf" : "arialbd.ttf"); } } else if (si.FontStyle == FontStyleEnum.Italic) { //OSX if ((platform == 4 || platform == 6 || platform == 128) && version > 8) { face = "Arial-ItalicMT"; fontname = "Arial Italic.ttf"; } else { face = dejavuFonts ? "DejaVu Sans Condensed Oblique" : "Arial-Italic"; fontname = (dejavuFonts ? "DejaVuSansCondensed-Oblique.ttf" : "ariali.ttf"); } } else { //OSX if ((platform == 4 || platform == 6 || platform == 128) && version > 8) { face = "ArialMT"; fontname = "Arial.ttf"; } else { face = dejavuFonts ? "DejaVu Sans Condensed" : face; fontname = (dejavuFonts ? "DejaVuSansCondensed.ttf" : "arial.ttf"); } } fonttype1 = false; } else if (face == "Courier New") { if (si.IsFontBold () && si.FontStyle == FontStyleEnum.Italic) { //OSX if ((platform == 4 || platform == 6 || platform == 128) && version > 8) { face = "CourierNewPS-BoldItalicMT"; fontname = "Courier New Bold Italic.ttf"; } else { face = dejavuFonts ? "DejaVu Sans Mono Bold Oblique" : "Courier New-BoldItalic"; fontname = (dejavuFonts ? "DejaVuSansMono-BoldOblique.ttf" : "courbi.ttf"); } } else if (si.IsFontBold ()) { //OSX if ((platform == 4 || platform == 6 || platform == 128) && version > 8) { face = "CourierNewPS-BoldMT"; fontname = "Courier New Bold.ttf"; } else { face = dejavuFonts ? "DejaVu Sans Mono Bold" : "Courier New-Bold"; fontname = (dejavuFonts ? "DejaVuSansMono-Oblique.ttf" : "courbd.ttf"); } } else if (si.FontStyle == FontStyleEnum.Italic) { //OSX if ((platform == 4 || platform == 6 || platform == 128) && version > 8) { face = "CourierNewPS-ItalicMT"; fontname = "Courier New Italic.ttf"; } else { face = dejavuFonts ? "DejaVu Sans Mono Oblique" : "Courier New-Italic"; fontname = (dejavuFonts ? "DejaVuSansMono-Oblique.ttf" : "couri.ttf"); } } else { //OSX if ((platform == 4 || platform == 6 || platform == 128) && version > 8) { face = "CourierNewPSMT"; fontname = "Courier New.ttf"; } else { face = dejavuFonts ? "DejaVu Sans Mono" : face; fontname = (dejavuFonts ? "DejaVuSansMono.ttf" : "cour.ttf"); } } fonttype1 = false; } else { if (si.IsFontBold() && si.FontStyle == FontStyleEnum.Italic) // bold and italic? face = face + "-BoldOblique"; else if (si.IsFontBold()) // just bold? face = face + "-Bold"; else if (si.FontStyle == FontStyleEnum.Italic) face = face + "-Oblique"; fonttype1 = true; } //Get index of fontname in List font name int indexbf = BaseFontsname.FindIndex(delegate(string _fontname) { return _fontname == face; }); //If not found then add new BaseFont if (indexbf == -1) { BaseFontsname.Add(face); if (fonttype1) { bf = BaseFont.CreateFont(face, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED); } else { string path = System.IO.Path.Combine(folder, fontname); bf = BaseFont.CreateFont(path, BaseFont.IDENTITY_H, BaseFont.EMBEDDED); } BaseFonts.Add(bf); } else //Get from List { bf = BaseFonts[indexbf]; } // Loop thru the lines of text for (int i = 0; i < sa.Length; i++) { string text = sa[i]; float textwidth = tw[i]; // Calculate the x positino float startX = x + si.PaddingLeft; // TODO: handle tb_rl float startY = y + si.PaddingTop + (i * si.FontSize); // TODO: handle tb_rl int align = 0; if (si.WritingMode == WritingModeEnum.lr_tb) { // TODO: not sure what alignment means with tb_lr so I'll leave it out for now switch (si.TextAlign) { case TextAlignEnum.Center: if (width > 0) { startX = x + si.PaddingLeft + (width - si.PaddingLeft - si.PaddingRight) / 2 - textwidth / 2; align = Element.ALIGN_CENTER; } break; case TextAlignEnum.Right: if (width > 0) { startX = x + width - textwidth - si.PaddingRight; align = Element.ALIGN_RIGHT; } break; case TextAlignEnum.Left: default: align = Element.ALIGN_LEFT; break; } // Calculate the y position switch (si.VerticalAlign) { case VerticalAlignEnum.Middle: if (height <= 0) break; // calculate the middle of the region startY = y + si.PaddingTop + (height - si.PaddingTop - si.PaddingBottom) / 2 - si.FontSize / 2; // now go up or down depending on which line if (sa.Length == 1) break; if (sa.Length % 2 == 0) // even number { startY = startY - ((sa.Length / 2 - i) * si.FontSize) + si.FontSize / 2; } else { startY = startY - ((sa.Length / 2 - i) * si.FontSize); } break; case VerticalAlignEnum.Bottom: if (height <= 0) break; startY = y + height - si.PaddingBottom - (si.FontSize * (sa.Length - i)); break; case VerticalAlignEnum.Top: default: break; } } else { //25072008 GJL - Move x in a little - it draws to close to the edge of the rectangle (25% of the font size seems to work!) and Center or right align vertical text startX += si.FontSize / 4; switch (si.TextAlign) { case TextAlignEnum.Center: if (height > 0) startY = y + si.PaddingLeft + (height - si.PaddingLeft - si.PaddingRight) / 2 - textwidth / 2; break; case TextAlignEnum.Right: if (width > 0) startY = y + height - textwidth - si.PaddingRight; break; case TextAlignEnum.Left: default: break; } } // Draw background rectangle if needed (only put out on the first line, since we do whole rectangle) if (!si.BackgroundColor.IsEmpty && height > 0 && width > 0 && i == 0) { // background color, height and width are specified iAddFillRect(x, y, width, height, si.BackgroundColor); } // Set the clipping path, (Itext have no clip) if (height > 0 && width > 0) { cb.SetRGBColorFill(si.Color.R, si.Color.G, si.Color.B); if (si.WritingMode == WritingModeEnum.lr_tb) { //If textline after measure with word break can fit just simple show Text if (width >= textwidth) { cb.BeginText(); cb.SetFontAndSize(bf, si.FontSize); cb.SetTextMatrix(startX, (_pSize.yHeight - startY - si.FontSize)); cb.ShowText(text); cb.EndText(); } else { //else use Column text to wrap or clip (wrap: for example a text like an URL so word break is not working here, itextsharp ColumnText do the work for us) ColumnText ct = new ColumnText(cb); Phrase myPhrase = new Phrase(text, new iTextSharp.text.Font(bf, si.FontSize)); ct.SetSimpleColumn(myPhrase, x + si.PaddingLeft, _pSize.yHeight - startY, x + width - si.PaddingRight, _pSize.yHeight - y - si.PaddingBottom - height, 10f, align); ct.Go(); } } else { //Not checked double rads = -283.0 / 180.0; double radsCos = Math.Cos(rads); double radsSin = Math.Sin(rads); cb.BeginText(); cb.SetFontAndSize(bf, si.FontSize); cb.SetTextMatrix((float)radsCos, (float)radsSin, (float)-radsSin, (float)radsCos, startX, _pSize.yHeight - startY); cb.ShowText(text); cb.EndText(); } //add URL if (url != null) pdfdocument.Add(new Annotation(x, _pSize.yHeight - (y + height), width + x, _pSize.yHeight - y, url)); //add tooltip if (tooltip != null) pdfdocument.Add(new Annotation(x, _pSize.yHeight - (y + height), width + x, _pSize.yHeight - y, tooltip)); } // Handle underlining etc. float maxX; switch (si.TextDecoration) { case TextDecorationEnum.Underline: maxX = width > 0 ? Math.Min(x + width, startX + textwidth) : startX + textwidth; iAddLine(startX, startY + si.FontSize + 1, maxX, startY + si.FontSize + 1, 1, si.Color, BorderStyleEnum.Solid); break; case TextDecorationEnum.LineThrough: maxX = width > 0 ? Math.Min(x + width, startX + textwidth) : startX + textwidth; iAddLine(startX, startY + (si.FontSize / 2) + 1, maxX, startY + (si.FontSize / 2) + 1, 1, si.Color, BorderStyleEnum.Solid); break; case TextDecorationEnum.Overline: maxX = width > 0 ? Math.Min(x + width, startX + textwidth) : startX + textwidth; iAddLine(startX, startY + 1, maxX, startY + 1, 1, si.Color, BorderStyleEnum.Solid); break; case TextDecorationEnum.None: default: break; } } iAddBorder(si, x, y, height, width); // add any required border return; }
protected internal override void AddText(float x, float y, float height, float width, string[] sa, StyleInfo si, float[] tw, bool bWrap, string url, bool bNoClip, string tooltip) { BaseFont bf; string face = iFontNameNormalize(si.FontFamily); string fontname = ""; bool fonttype1 = true; var folder = FontFolder; //Call to determine folder and set value of _dejavuFonts; if (face == "Times-Roman") { if (si.IsFontBold() && si.FontStyle == FontStyleEnum.Italic) { //OSX if (IsOSX) { face = "TimesNewRomanPS-BoldItalicMT"; fontname = "Times New Roman Bold Italic.ttf"; } else { face = _dejavuFonts ? "DejaVu Serif Condensed Bold Italic" : "Times-BoldItalic"; fontname = (_dejavuFonts ? "DejaVuSerifCondensed-BoldItalic.ttf" : "timesbi.ttf"); } } else if (si.IsFontBold()) { //OSX if (IsOSX) { face = "TimesNewRomanPS-BoldMT"; fontname = "Times New Roman Bold.ttf"; } else { face = _dejavuFonts ? "DejaVu Serif Condensed Bold" : "Times-Bold"; fontname = (_dejavuFonts ? "DejaVuSerifCondensed-Bold.ttf" : "timesbd.ttf"); } } else if (si.FontStyle == FontStyleEnum.Italic) { //OSX if (IsOSX) { face = "TimesNewRomanPS-ItalicMT"; fontname = "Times New Roman Italic.ttf"; } else { face = _dejavuFonts ? "DejaVu Serif Condensed Italic" : "Times-Italic"; fontname = (_dejavuFonts ? "DejaVuSerifCondensed-Italic.ttf" : "timesi.ttf"); } } else { //OSX if (IsOSX) { face = "TimesNewRomanPSMT"; fontname = "Times New Roman.ttf"; } else { face = _dejavuFonts ? "DejaVu Serif Condensed" : face; fontname = (_dejavuFonts ? "DejaVuSerifCondensed.ttf" : "times.ttf"); } } fonttype1 = false; } else if (face == "Arial") { if (IsAsian(sa)) { face = "Arial Unicode MS"; fontname = "arialuni.ttf"; } else if (si.IsFontBold() && si.FontStyle == FontStyleEnum.Italic) { //OSX if (IsOSX) { face = "Arial BoldItalicMT"; fontname = "Arial Bold Italic.ttf"; } else { face = _dejavuFonts ? "DejaVu Sans Condensed Bold Oblique" : "Arial-BoldItalic"; fontname = (_dejavuFonts ? "DejaVuSansCondensed-BoldOblique.ttf" : "arialbi.ttf"); } } else if (si.IsFontBold()) { //OSX if (IsOSX) { face = "Arial-BoldMT"; fontname = "Arial Bold.ttf"; } else { face = _dejavuFonts ? "DejaVu Sans Condensed Bold" : "Arial-Bold"; fontname = (_dejavuFonts ? "DejaVuSansCondensed-Bold.ttf" : "arialbd.ttf"); } } else if (si.FontStyle == FontStyleEnum.Italic) { //OSX if (IsOSX) { face = "Arial-ItalicMT"; fontname = "Arial Italic.ttf"; } else { face = _dejavuFonts ? "DejaVu Sans Condensed Oblique" : "Arial-Italic"; fontname = (_dejavuFonts ? "DejaVuSansCondensed-Oblique.ttf" : "ariali.ttf"); } } else { //OSX if (IsOSX) { face = "ArialMT"; fontname = "Arial.ttf"; } else { face = _dejavuFonts ? "DejaVu Sans Condensed" : face; fontname = (_dejavuFonts ? "DejaVuSansCondensed.ttf" : "arial.ttf"); } } fonttype1 = false; } else if (face == "Courier New") { if (si.IsFontBold() && si.FontStyle == FontStyleEnum.Italic) { //OSX if (IsOSX) { face = "CourierNewPS-BoldItalicMT"; fontname = "Courier New Bold Italic.ttf"; } else { face = _dejavuFonts ? "DejaVu Sans Mono Bold Oblique" : "Courier New-BoldItalic"; fontname = (_dejavuFonts ? "DejaVuSansMono-BoldOblique.ttf" : "courbi.ttf"); } } else if (si.IsFontBold()) { //OSX if (IsOSX) { face = "CourierNewPS-BoldMT"; fontname = "Courier New Bold.ttf"; } else { face = _dejavuFonts ? "DejaVu Sans Mono Bold" : "Courier New-Bold"; fontname = (_dejavuFonts ? "DejaVuSansMono-Oblique.ttf" : "courbd.ttf"); } } else if (si.FontStyle == FontStyleEnum.Italic) { //OSX if (IsOSX) { face = "CourierNewPS-ItalicMT"; fontname = "Courier New Italic.ttf"; } else { face = _dejavuFonts ? "DejaVu Sans Mono Oblique" : "Courier New-Italic"; fontname = (_dejavuFonts ? "DejaVuSansMono-Oblique.ttf" : "couri.ttf"); } } else { //OSX if (IsOSX) { face = "CourierNewPSMT"; fontname = "Courier New.ttf"; } else { face = _dejavuFonts ? "DejaVu Sans Mono" : face; fontname = (_dejavuFonts ? "DejaVuSansMono.ttf" : "cour.ttf"); } } fonttype1 = false; } else { if (si.IsFontBold() && si.FontStyle == FontStyleEnum.Italic) // bold and italic? { face = face + "-BoldOblique"; } else if (si.IsFontBold()) // just bold? { face = face + "-Bold"; } else if (si.FontStyle == FontStyleEnum.Italic) { face = face + "-Oblique"; } fonttype1 = true; } //Get index of fontname in List font name int indexbf = _baseFontsName.FindIndex(delegate(string _fontname) { return(_fontname == face); }); //If not found then add new BaseFont if (indexbf == -1) { _baseFontsName.Add(face); if (fonttype1) { bf = BaseFont.CreateFont(face, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED); } else { string path = System.IO.Path.Combine(folder, fontname); bf = BaseFont.CreateFont(path, BaseFont.IDENTITY_H, BaseFont.EMBEDDED); } _baseFonts.Add(bf); } else //Get from List { bf = _baseFonts[indexbf]; } // Loop thru the lines of text for (int i = 0; i < sa.Length; i++) { string text = sa[i]; float textwidth = tw[i]; // Calculate the x positino float startX = x + si.PaddingLeft; // TODO: handle tb_rl float startY = y + si.PaddingTop + (i * si.FontSize); // TODO: handle tb_rl int align = 0; if (si.WritingMode == WritingModeEnum.lr_tb) { // TODO: not sure what alignment means with tb_lr so I'll leave it out for now switch (si.TextAlign) { case TextAlignEnum.Center: if (width > 0) { startX = x + si.PaddingLeft + (width - si.PaddingLeft - si.PaddingRight) / 2 - textwidth / 2; align = Element.ALIGN_CENTER; } break; case TextAlignEnum.Right: if (width > 0) { startX = x + width - textwidth - si.PaddingRight; align = Element.ALIGN_RIGHT; } break; case TextAlignEnum.Left: default: align = Element.ALIGN_LEFT; break; } // Calculate the y position switch (si.VerticalAlign) { case VerticalAlignEnum.Middle: if (height <= 0) { break; } // calculate the middle of the region startY = y + si.PaddingTop + (height - si.PaddingTop - si.PaddingBottom) / 2 - si.FontSize / 2; // now go up or down depending on which line if (sa.Length == 1) { break; } if (sa.Length % 2 == 0) // even number { startY = startY - ((sa.Length / 2 - i) * si.FontSize) + si.FontSize / 2; } else { startY = startY - ((sa.Length / 2 - i) * si.FontSize); } break; case VerticalAlignEnum.Bottom: if (height <= 0) { break; } startY = y + height - si.PaddingBottom - (si.FontSize * (sa.Length - i)); break; case VerticalAlignEnum.Top: default: break; } } else { //25072008 GJL - Move x in a little - it draws to close to the edge of the rectangle (25% of the font size seems to work!) and Center or right align vertical text startX += si.FontSize / 4; switch (si.TextAlign) { case TextAlignEnum.Center: if (height > 0) { startY = y + si.PaddingLeft + (height - si.PaddingLeft - si.PaddingRight) / 2 - textwidth / 2; } break; case TextAlignEnum.Right: if (width > 0) { startY = y + height - textwidth - si.PaddingRight; } break; case TextAlignEnum.Left: default: break; } } // Draw background rectangle if needed (only put out on the first line, since we do whole rectangle) if (!si.BackgroundColor.IsEmpty && height > 0 && width > 0 && i == 0) { // background color, height and width are specified iAddFillRect(x, y, width, height, si.BackgroundColor); } // Set the clipping path, (Itext have no clip) if (height > 0 && width > 0) { _contentByte.SetRgbColorFill(si.Color.R, si.Color.G, si.Color.B); if (si.WritingMode == WritingModeEnum.lr_tb) { //If textline after measure with word break can fit just simple show Text if (width >= textwidth) { _contentByte.BeginText(); _contentByte.SetFontAndSize(bf, si.FontSize); _contentByte.SetTextMatrix(startX, (PageSize.yHeight - startY - si.FontSize)); _contentByte.ShowText(text); _contentByte.EndText(); } else { //else use Column text to wrap or clip (wrap: for example a text like an URL so word break is not working here, itextsharp ColumnText do the work for us) ColumnText ct = new ColumnText(_contentByte); Phrase myPhrase = new Phrase(text, new iTextSharp.text.Font(bf, si.FontSize)); ct.SetSimpleColumn(myPhrase, x + si.PaddingLeft, PageSize.yHeight - startY, x + width - si.PaddingRight, PageSize.yHeight - y - si.PaddingBottom - height, 10f, align); ct.Go(); } } else { //Not checked double rads = -283.0 / 180.0; double radsCos = Math.Cos(rads); double radsSin = Math.Sin(rads); _contentByte.BeginText(); _contentByte.SetFontAndSize(bf, si.FontSize); _contentByte.SetTextMatrix((float)radsCos, (float)radsSin, (float)-radsSin, (float)radsCos, startX, PageSize.yHeight - startY); _contentByte.ShowText(text); _contentByte.EndText(); } //add URL if (url != null) { _pdfDocument.Add(new Annotation(x, PageSize.yHeight - (y + height), width + x, PageSize.yHeight - y, url)); } //add tooltip if (tooltip != null) { _pdfDocument.Add(new Annotation(x, PageSize.yHeight - (y + height), width + x, PageSize.yHeight - y, tooltip)); } } // Handle underlining etc. float maxX; switch (si.TextDecoration) { case TextDecorationEnum.Underline: maxX = width > 0 ? Math.Min(x + width, startX + textwidth) : startX + textwidth; AddLine(startX, startY + si.FontSize + 1, maxX, startY + si.FontSize + 1, 1, si.Color, BorderStyleEnum.Solid); break; case TextDecorationEnum.LineThrough: maxX = width > 0 ? Math.Min(x + width, startX + textwidth) : startX + textwidth; AddLine(startX, startY + (si.FontSize / 2) + 1, maxX, startY + (si.FontSize / 2) + 1, 1, si.Color, BorderStyleEnum.Solid); break; case TextDecorationEnum.Overline: maxX = width > 0 ? Math.Min(x + width, startX + textwidth) : startX + textwidth; AddLine(startX, startY + 1, maxX, startY + 1, 1, si.Color, BorderStyleEnum.Solid); break; case TextDecorationEnum.None: default: break; } } iAddBorder(si, x, y, height, width); // add any required border return; }
private string[] MeasureString(PageText pt, System.Drawing.Graphics g) { StyleInfo si = pt.SI; string s = pt.Text; System.Drawing.Font drawFont = null; System.Drawing.StringFormat drawFormat = null; System.Drawing.SizeF ms; string[] sa = null; try { // STYLE System.Drawing.FontStyle fs = 0; if (si.FontStyle == FontStyleEnum.Italic) { fs |= System.Drawing.FontStyle.Italic; } if (si.IsFontBold()) { fs |= System.Drawing.FontStyle.Bold; } if (si.TextDecoration == TextDecorationEnum.Underline) { fs |= System.Drawing.FontStyle.Underline; } drawFont = new System.Drawing.Font(StyleInfo.GetFontFamily(si.FontFamilyFull), si.FontSize, fs); drawFormat = new System.Drawing.StringFormat(); if (si.TextAlign == TextAlignEnum.Center) { drawFormat.Alignment = System.Drawing.StringAlignment.Center; } if (si.TextAlign == TextAlignEnum.Right) { drawFormat.Alignment = System.Drawing.StringAlignment.Far; } else { drawFormat.Alignment = System.Drawing.StringAlignment.Near; } ms = MeasureString(pt.Text, g, drawFont, drawFormat); float width = pt.W - si.PaddingLeft - si.PaddingRight; if (ms.Width <= (width + si.FontSize * 1.5)) { sa = new string[1]; sa[0] = pt.Text; } else { int x = (int)(ms.Width / width) + 1; List <string> texts = new List <string>(); int len = pt.Text.Length; int start = 0; int setup = len / x; while (true) { ms = MeasureString(pt.Text.Substring(start, setup), g, drawFont, drawFormat); float subW = width - ms.Width; if ((subW >= 0) & (subW <= si.FontSize)) { texts.Add(pt.Text.Substring(start, setup)); if ((start + setup) == len) { break; } else { start += setup; if ((start + setup) >= len) { setup = len - start; } } } else if ((subW >= 0) & (subW > si.FontSize)) { if ((start + setup) == len) { texts.Add(pt.Text.Substring(start, setup)); break; } setup++; } else if (subW < 0) { setup--; } } sa = new string[texts.Count]; texts.CopyTo(sa); } } finally { if (drawFont != null) { drawFont.Dispose(); } if (drawFormat != null) { drawFont.Dispose(); } } return(sa); }