/** * Applies a font to the specified characters of a string. * * @param startIndex The start index to apply the font to (inclusive) * @param endIndex The end index to apply to font to (exclusive) * @param font The index of the font to use. */ public void ApplyFont(int startIndex, int endIndex, IFont font) { if (startIndex > endIndex) { throw new ArgumentException("Start index must be less than end index, but had " + startIndex + " and " + endIndex); } if (startIndex < 0 || endIndex > Length) { throw new ArgumentException("Start and end index not in range, but had " + startIndex + " and " + endIndex); } if (startIndex == endIndex) { return; } if (st.sizeOfRArray() == 0 && st.IsSetT()) { //convert <t>string</t> into a text Run: <r><t>string</t></r> st.AddNewR().t = (st.t); st.unsetT(); } String text = this.String; XSSFFont xssfFont = (XSSFFont)font; SortedDictionary <int, CT_RPrElt> formats = GetFormatMap(st); CT_RPrElt fmt = new CT_RPrElt(); SetRunAttributes(xssfFont.GetCTFont(), fmt); ApplyFont(formats, startIndex, endIndex, fmt); CT_Rst newSt = buildCTRst(text, formats); st.Set(newSt); }
public void TestClearFormatting() { XSSFRichTextString rt = new XSSFRichTextString("Apache POI"); Assert.AreEqual("Apache POI", rt.String); Assert.AreEqual(false, rt.HasFormatting()); rt.ClearFormatting(); CT_Rst st = rt.GetCTRst(); Assert.IsTrue(st.IsSetT()); Assert.AreEqual("Apache POI", rt.String); Assert.AreEqual(0, rt.NumFormattingRuns); Assert.AreEqual(false, rt.HasFormatting()); XSSFFont font = new XSSFFont(); font.IsBold = true; rt.ApplyFont(7, 10, font); Assert.AreEqual(2, rt.NumFormattingRuns); Assert.AreEqual(true, rt.HasFormatting()); rt.ClearFormatting(); Assert.AreEqual("Apache POI", rt.String); Assert.AreEqual(0, rt.NumFormattingRuns); Assert.AreEqual(false, rt.HasFormatting()); }
public void TestCreate() { XSSFRichTextString rt = new XSSFRichTextString("Apache POI"); Assert.AreEqual("Apache POI", rt.String); CT_Rst st = rt.GetCTRst(); Assert.IsTrue(st.IsSetT()); Assert.AreEqual("Apache POI", st.t); rt.Append(" is cool stuff"); Assert.AreEqual(2, st.sizeOfRArray()); Assert.IsFalse(st.IsSetT()); Assert.AreEqual("Apache POI is cool stuff", rt.String); }