CT_Rst BuildCTRst(String text, SortedDictionary <int, CT_RPrElt> formats) { if (text.Length != GetLastKey(formats.Keys)) { throw new ArgumentException("Text length was " + text.Length + " but the last format index was " + GetLastKey(formats.Keys)); } CT_Rst st = new CT_Rst(); int runStartIdx = 0; foreach (KeyValuePair <int, CT_RPrElt> kv in formats) { int runEndIdx = kv.Key; CT_RElt run = st.AddNewR(); String fragment = text.Substring(runStartIdx, runEndIdx - runStartIdx); run.t = (fragment); PreserveSpaces(run.t); CT_RPrElt fmt = kv.Value; if (fmt != null) { run.rPr = fmt; } runStartIdx = runEndIdx; } return(st); }
private CT_Rst buildCTRst(string text, SortedDictionary <int, CT_RPrElt> formats) { if (text.Length != this.GetLastKey(formats.Keys)) { throw new ArgumentException("Text length was " + (object)text.Length + " but the last format index was " + (object)this.GetLastKey(formats.Keys)); } CT_Rst ctRst = new CT_Rst(); int startIndex = 0; SortedDictionary <int, CT_RPrElt> .KeyCollection.Enumerator enumerator = formats.Keys.GetEnumerator(); while (enumerator.MoveNext()) { int current = enumerator.Current; CT_RElt ctRelt = ctRst.AddNewR(); string str = text.Substring(startIndex, current - startIndex); ctRelt.t = str; XSSFRichTextString.PreserveSpaces(ctRelt.t); CT_RPrElt format = formats[current]; if (format != null) { ctRelt.rPr = format; } startIndex = current; } return(ctRst); }
CT_Rst buildCTRst(String text, SortedDictionary <int, CT_RPrElt> formats) { if (text.Length != GetLastKey(formats.Keys)) { throw new ArgumentException("Text length was " + text.Length + " but the last format index was " + GetLastKey(formats.Keys)); } CT_Rst st = new CT_Rst(); int runStartIdx = 0; for (SortedDictionary <int, CT_RPrElt> .KeyCollection.Enumerator it = formats.Keys.GetEnumerator(); it.MoveNext();) { int runEndIdx = it.Current; CT_RElt run = st.AddNewR(); String fragment = text.Substring(runStartIdx, runEndIdx - runStartIdx); run.t = (fragment); PreserveSpaces(run.t); CT_RPrElt fmt = formats[runEndIdx]; if (fmt != null) { run.rPr = (fmt); } runStartIdx = runEndIdx; } return(st); }
public void SetText(XSSFRichTextString str) { XSSFWorkbook parent = (XSSFWorkbook)this.GetDrawing().GetParent().GetParent(); str.SetStylesTableReference(parent.GetStylesSource()); CT_TextParagraph ctTextParagraph = new CT_TextParagraph(); if (str.NumFormattingRuns == 0) { CT_RegularTextRun ctRegularTextRun = ctTextParagraph.AddNewR(); CT_TextCharacterProperties characterProperties = ctRegularTextRun.AddNewRPr(); characterProperties.lang = "en-US"; characterProperties.sz = 1100; ctRegularTextRun.t = str.String; } else { for (int index = 0; index < str.GetCTRst().sizeOfRArray(); ++index) { CT_RElt rarray = str.GetCTRst().GetRArray(index); CT_RPrElt pr = rarray.rPr ?? rarray.AddNewRPr(); CT_RegularTextRun ctRegularTextRun = ctTextParagraph.AddNewR(); CT_TextCharacterProperties rPr = ctRegularTextRun.AddNewRPr(); rPr.lang = "en-US"; XSSFSimpleShape.ApplyAttributes(pr, rPr); ctRegularTextRun.t = rarray.t; } } this.ctShape.txBody.SetPArray(new CT_TextParagraph[1] { ctTextParagraph }); }
public void Split(CT_RElt original, CT_RElt added) { if (this._runs.Contains(original)) { this._runs.Add(added); this._font.Add(added.RPr); } }
/** * Returns the number of characters this format run covers. * * @param index the index of the formatting run * @return the number of characters this format run covers */ public int GetLengthOfFormattingRun(int index) { if (st.sizeOfRArray() == 0 || index >= st.sizeOfRArray()) { return(-1); } CT_RElt r = st.GetRArray(index); return(r.t.Length); }
public int GetLengthOfFormattingRun(int index) { if (this.st.sizeOfRArray() == 0) { return(this.Length); } for (int index1 = 0; index1 < this.st.sizeOfRArray(); ++index1) { CT_RElt rarray = this.st.GetRArray(index1); if (index1 == index) { return(rarray.t.Length); } } return(-1); }
/** * Returns the number of characters this format run covers. * * @param index the index of the formatting run * @return the number of characters this format run covers */ public int GetLengthOfFormattingRun(int index) { if (st.sizeOfRArray() == 0) { return(this.Length); } for (int i = 0; i < st.sizeOfRArray(); i++) { CT_RElt r = st.GetRArray(i); if (i == index) { return(r.t.Length); } } return(-1); }
/** * Gets a copy of the font used in a particular formatting Run. * * @param index the index of the formatting run * @return A copy of the font used or null if no formatting is applied to the specified text Run. */ public IFont GetFontOfFormattingRun(int index) { if (st.sizeOfRArray() == 0 || index >= st.sizeOfRArray()) { return(null); } CT_RElt r = st.GetRArray(index); if (r.rPr != null) { XSSFFont fnt = new XSSFFont(ToCTFont(r.rPr)); fnt.SetThemesTable(GetThemesTable()); return(fnt); } return(null); }
/** * Append new text to this text run and apply the specify font to it * * @param text the text to append * @param font the font to apply to the Appended text or <code>null</code> if no formatting is required */ public void Append(String text, XSSFFont font) { 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(); } CT_RElt lt = st.AddNewR(); lt.t = (text); CT_RPrElt pr = lt.AddNewRPr(); if (font != null) { SetRunAttributes(font.GetCTFont(), pr); } }
public IFont GetFontOfFormattingRun(int index) { if (this.st.sizeOfRArray() == 0) { return((IFont)null); } for (int index1 = 0; index1 < this.st.sizeOfRArray(); ++index1) { CT_RElt rarray = this.st.GetRArray(index1); if (index1 == index) { XSSFFont xssfFont = new XSSFFont(XSSFRichTextString.ToCTFont(rarray.rPr)); xssfFont.SetThemesTable(this.GetThemesTable()); return((IFont)xssfFont); } } return((IFont)null); }
private void SplitRun(int runIndex, int splitIndex) { CT_RElt cT_RElt = _inlineString.R[runIndex]; CT_RElt cT_RElt2 = new CT_RElt(); string t = cT_RElt.T.Substring(0, splitIndex); string t2 = cT_RElt.T.Substring(splitIndex); cT_RElt.T = t; cT_RElt2.T = t2; if (cT_RElt.RPr != null) { throw new FatalException(); } foreach (XMLCharacterRunModel model in _models) { model.Split(cT_RElt, cT_RElt2); } _inlineString.R.Insert(runIndex + 1, cT_RElt2); }
/** * Gets a copy of the font used in a particular formatting Run. * * @param index the index of the formatting run * @return A copy of the font used or null if no formatting is applied to the specified text Run. */ public IFont GetFontOfFormattingRun(int index) { if (st.sizeOfRArray() == 0) { return(null); } for (int i = 0; i < st.sizeOfRArray(); i++) { CT_RElt r = st.GetRArray(i); if (i == index) { XSSFFont fnt = new XSSFFont(ToCTFont(r.rPr)); fnt.SetThemesTable(GetThemesTable()); return(fnt); } } return(null); }
public int GetIndexOfFormattingRun(int index) { if (this.st.sizeOfRArray() == 0) { return(0); } int num = 0; for (int index1 = 0; index1 < this.st.sizeOfRArray(); ++index1) { CT_RElt rarray = this.st.GetRArray(index1); if (index1 == index) { return(num); } num += rarray.t.Length; } return(-1); }
/** * Add a new paragraph run to this shape, Set to the provided rich text string * * @return Created paragraph run */ public XSSFTextParagraph AddNewTextParagraph(XSSFRichTextString str) { NPOI.OpenXmlFormats.Dml.Spreadsheet.CT_TextBody txBody = ctShape.txBody; CT_TextParagraph p = txBody.AddNewP(); if (str.NumFormattingRuns == 0) { CT_RegularTextRun r = p.AddNewR(); CT_TextCharacterProperties rPr = r.AddNewRPr(); rPr.lang = (/*setter*/ "en-US"); rPr.sz = (/*setter*/ 1100); r.t = (/*setter*/ str.String); } else { for (int i = 0; i < str.GetCTRst().SizeOfRArray(); i++) { CT_RElt lt = str.GetCTRst().GetRArray(i); CT_RPrElt ltPr = lt.rPr; if (ltPr == null) { ltPr = lt.AddNewRPr(); } CT_RegularTextRun r = p.AddNewR(); CT_TextCharacterProperties rPr = r.AddNewRPr(); rPr.lang = (/*setter*/ "en-US"); ApplyAttributes(ltPr, rPr); r.t = (/*setter*/ lt.t); } } // Note: the XSSFTextParagraph constructor will create its required XSSFTextRuns from the provided CTTextParagraph XSSFTextParagraph paragraph = new XSSFTextParagraph(p, ctShape); _paragraphs.Add(paragraph); return(paragraph); }
/** * Set a single paragraph of text on the shape. Note this will replace all existing paragraphs Created on the shape. * @param str rich text string representing the paragraph text */ public void SetText(XSSFRichTextString str) { XSSFWorkbook wb = (XSSFWorkbook)GetDrawing().GetParent().GetParent(); str.SetStylesTableReference(wb.GetStylesSource()); CT_TextParagraph p = new CT_TextParagraph(); if (str.NumFormattingRuns == 0) { CT_RegularTextRun r = p.AddNewR(); CT_TextCharacterProperties rPr = r.AddNewRPr(); rPr.lang = (/*setter*/ "en-US"); rPr.sz = (/*setter*/ 1100); r.t = (/*setter*/ str.String); } else { for (int i = 0; i < str.GetCTRst().SizeOfRArray(); i++) { CT_RElt lt = str.GetCTRst().GetRArray(i); CT_RPrElt ltPr = lt.rPr; if (ltPr == null) { ltPr = lt.AddNewRPr(); } CT_RegularTextRun r = p.AddNewR(); CT_TextCharacterProperties rPr = r.AddNewRPr(); rPr.lang = (/*setter*/ "en-US"); ApplyAttributes(ltPr, rPr); r.t = (/*setter*/ lt.t); } } ClearText(); ctShape.txBody.SetPArray(new CT_TextParagraph[] { p }); _paragraphs.Add(new XSSFTextParagraph(ctShape.txBody.GetPArray(0), ctShape)); }
/** * The index within the string to which the specified formatting run applies. * * @param index the index of the formatting run * @return the index within the string. */ public int GetIndexOfFormattingRun(int index) { if (st.sizeOfRArray() == 0) { return(0); } int pos = 0; for (int i = 0; i < st.sizeOfRArray(); i++) { CT_RElt r = st.GetRArray(i); if (i == index) { return(pos); } pos += r.t.Length; } return(-1); }
public void Append(string text, XSSFFont font) { if (this.st.sizeOfRArray() == 0 && this.st.IsSetT()) { CT_RElt ctRelt = this.st.AddNewR(); ctRelt.t = this.st.t; XSSFRichTextString.PreserveSpaces(ctRelt.t); this.st.unsetT(); } CT_RElt ctRelt1 = this.st.AddNewR(); ctRelt1.t = text; XSSFRichTextString.PreserveSpaces(ctRelt1.t); CT_RPrElt pr = ctRelt1.AddNewRPr(); if (font == null) { return; } this.SetRunAttributes(font.GetCTFont(), pr); }
public short GetFontAtIndex(int index) { if (this.st.sizeOfRArray() == 0) { return(-1); } int num = 0; for (int index1 = 0; index1 < this.st.sizeOfRArray(); ++index1) { CT_RElt rarray = this.st.GetRArray(index1); if (index >= num && index < num + rarray.t.Length) { XSSFFont xssfFont = new XSSFFont(XSSFRichTextString.ToCTFont(rarray.rPr)); xssfFont.SetThemesTable(this.GetThemesTable()); return(xssfFont.Index); } num += rarray.t.Length; } return(-1); }
/** * Return a copy of the font in use at a particular index. * * @param index The index. * @return A copy of the font that's currently being applied at that * index or null if no font is being applied or the * index is out of range. */ public XSSFFont GetFontAtIndex(int index) { if (st.sizeOfRArray() == 0) { return(null); } int pos = 0; for (int i = 0; i < st.sizeOfRArray(); i++) { CT_RElt r = st.GetRArray(i); if (index >= pos && index < pos + r.t.Length) { XSSFFont fnt = new XSSFFont(ToCTFont(r.rPr)); fnt.SetThemesTable(GetThemesTable()); return(fnt); } pos += r.t.Length; } return(null); }
public void TestCreateNew() { SharedStringsTable sst = new SharedStringsTable(); CT_Rst st; int idx; // Check defaults Assert.IsNotNull(sst.Items); Assert.AreEqual(0, sst.Items.Count); Assert.AreEqual(0, sst.Count); Assert.AreEqual(0, sst.UniqueCount); st = new CT_Rst(); st.t = ("Hello, World!"); idx = sst.AddEntry(st); Assert.AreEqual(0, idx); Assert.AreEqual(1, sst.Count); Assert.AreEqual(1, sst.UniqueCount); //add the same entry again idx = sst.AddEntry(st); Assert.AreEqual(0, idx); Assert.AreEqual(2, sst.Count); Assert.AreEqual(1, sst.UniqueCount); //and again idx = sst.AddEntry(st); Assert.AreEqual(0, idx); Assert.AreEqual(3, sst.Count); Assert.AreEqual(1, sst.UniqueCount); st = new CT_Rst(); st.t = ("Second string"); idx = sst.AddEntry(st); Assert.AreEqual(1, idx); Assert.AreEqual(4, sst.Count); Assert.AreEqual(2, sst.UniqueCount); //add the same entry again idx = sst.AddEntry(st); Assert.AreEqual(1, idx); Assert.AreEqual(5, sst.Count); Assert.AreEqual(2, sst.UniqueCount); st = new CT_Rst(); CT_RElt r = st.AddNewR(); CT_RPrElt pr = r.AddNewRPr(); pr.AddNewColor().SetRgb(new byte[] { (byte)0xFF, 0, 0 }); //red pr.AddNewI().val = (true); //bold pr.AddNewB().val = (true); //italic r.t = ("Second string"); idx = sst.AddEntry(st); Assert.AreEqual(2, idx); Assert.AreEqual(6, sst.Count); Assert.AreEqual(3, sst.UniqueCount); idx = sst.AddEntry(st); Assert.AreEqual(2, idx); Assert.AreEqual(7, sst.Count); Assert.AreEqual(3, sst.UniqueCount); //OK. the sst table is Filled, check the contents Assert.AreEqual(3, sst.Items.Count); Assert.AreEqual("Hello, World!", new XSSFRichTextString(sst.GetEntryAt(0)).ToString()); Assert.AreEqual("Second string", new XSSFRichTextString(sst.GetEntryAt(1)).ToString()); Assert.AreEqual("Second string", new XSSFRichTextString(sst.GetEntryAt(2)).ToString()); }