public void DrawString(String str, int x, int y) { if (string.IsNullOrEmpty(str)) { return; } using (Font excelFont = new Font(font.Name.Equals("SansSerif") ? "Arial" : font.Name, (int)(font.Size / verticalPixelsPerPoint), font.Style)) { FontDetails d = StaticFontMetrics.GetFontDetails(excelFont); int width = (int)((d.GetStringWidth(str) * 8) + 12); int height = (int)((font.Size / verticalPixelsPerPoint) + 6) * 2; y -= Convert.ToInt32((font.Size / verticalPixelsPerPoint) + 2 * verticalPixelsPerPoint); // we want to Draw the shape from the top-left HSSFTextbox textbox = escherGroup.CreateTextbox(new HSSFChildAnchor(x, y, x + width, y + height)); textbox.IsNoFill = (true); textbox.LineStyle = LineStyle.None; HSSFRichTextString s = new HSSFRichTextString(str); HSSFFont hssfFont = MatchFont(excelFont); s.ApplyFont(hssfFont); textbox.String = (s); } }
private HSSFFont MatchFont(Font font) { HSSFColor hssfColor = workbook.GetCustomPalette() .FindColor((byte)foreground.R, (byte)foreground.G, (byte)foreground.B); if (hssfColor == null) { hssfColor = workbook.GetCustomPalette().FindSimilarColor((byte)foreground.R, (byte)foreground.G, (byte)foreground.B); } bool bold = font.Bold; bool italic = font.Italic; HSSFFont hssfFont = (HSSFFont)workbook.FindFont(bold ? (short)Zephyr.Utils.NPOI.SS.UserModel.FontBoldWeight.BOLD : (short)Zephyr.Utils.NPOI.SS.UserModel.FontBoldWeight.NORMAL, hssfColor.GetIndex(), (short)(font.Size * 20), font.Name, italic, false, (short)Zephyr.Utils.NPOI.SS.UserModel.FontSuperScript.NONE, (byte)Zephyr.Utils.NPOI.SS.UserModel.FontUnderlineType.NONE ); if (hssfFont == null) { hssfFont = (HSSFFont)workbook.CreateFont(); hssfFont.Boldweight = (short)(bold ? Zephyr.Utils.NPOI.SS.UserModel.FontBoldWeight.BOLD : 0); hssfFont.Color = (hssfColor.GetIndex()); hssfFont.FontHeight = ((short)(font.Size * 20)); hssfFont.FontName = font.Name; hssfFont.IsItalic = (italic); hssfFont.IsStrikeout = (false); hssfFont.TypeOffset = 0; hssfFont.Underline = 0; } return(hssfFont); }
/// <summary> /// Convert HSSFFont to Font. /// </summary> /// <param name="font1">The font.</param> /// <returns></returns> public System.Drawing.Font HSSFFont2Font(HSSFFont font1) { return new System.Drawing.Font(font1.FontName, font1.FontHeightInPoints); }
/// <summary> /// Get the font at the given index number /// </summary> /// <param name="idx">The index number</param> /// <returns>HSSFFont at the index</returns> public Zephyr.Utils.NPOI.SS.UserModel.IFont GetFontAt(short idx) { if (fonts == null) fonts = new Hashtable(); // So we don't confuse users, give them back // the same object every time, but create // them lazily if (fonts.ContainsKey(idx)) { return (HSSFFont)fonts[idx]; } FontRecord font = workbook.GetFontRecordAt(idx); HSSFFont retval = new HSSFFont(idx, font); fonts[idx] = retval; return retval; }
/// <summary> /// Take a cell, and apply a font to it /// </summary> /// <param name="cell">the cell to Set the alignment for</param> /// <param name="workbook">The workbook that is being worked with.</param> /// <param name="font">The HSSFFont that you want to Set...</param> public static void SetFont(ICell cell, HSSFWorkbook workbook, HSSFFont font) { SetCellStyleProperty(cell, workbook, FONT, font); }
/// <summary> /// Clones all the style information from another /// HSSFCellStyle, onto this one. This /// HSSFCellStyle will then have all the same /// properties as the source, but the two may /// be edited independently. /// Any stylings on this HSSFCellStyle will be lost! /// The source HSSFCellStyle could be from another /// HSSFWorkbook if you like. This allows you to /// copy styles from one HSSFWorkbook to another. /// </summary> /// <param name="source">The source.</param> public void CloneStyleFrom(HSSFCellStyle source) { // First we need to clone the extended format // record format.CloneStyleFrom(source.format); // Handle matching things if we cross workbooks if (workbook != source.workbook) { // Then we need to clone the format string, // and update the format record for this short fmt = (short)workbook.CreateFormat( source.GetDataFormatString() ); this.DataFormat=(fmt); // Finally we need to clone the font, // and update the format record for this FontRecord fr = workbook.CreateNewFont(); fr.CloneStyleFrom( source.workbook.GetFontRecordAt( source.FontIndex ) ); HSSFFont font = new HSSFFont( (short)workbook.GetFontIndex(fr), fr ); this.SetFont(font); } }