/// <summary> /// Sets the text of this text run.in the /// </summary> /// <param name="value">the literal text which shall be displayed in the document</param> /// <param name="pos">position in the text array (NB: 0 based)</param> public void SetText(String value, int pos) { int length = run.SizeOfTArray(); if (pos > length) { throw new IndexOutOfRangeException("Value too large for the parameter position"); } CT_Text t = (pos < length && pos >= 0) ? run.GetTArray(pos): run.AddNewT(); t.Value = (value); preserveSpaces(t); }
/** * Add the xml:spaces="preserve" attribute if the string has leading or trailing white spaces * * @param xs the string to check */ static void preserveSpaces(CT_Text xs) { String text = xs.Value; if (text != null && text.Length >= 1 && (text.StartsWith(" ") || text.EndsWith(" "))) { // XmlCursor c = xs.NewCursor(); // c.ToNextToken(); // c.InsertAttributeWithValue(new QName("http://www.w3.org/XML/1998/namespace", "space"), "preserve"); // c.Dispose(); xs.space = "preserve"; } }
public void SetText(string value, int pos) { int num = this.run.SizeOfTArray(); if (pos > num) { throw new IndexOutOfRangeException("Value too large for the parameter position in XWPFrun.Text=(String value,int pos)"); } CT_Text xs = pos >= num || pos < 0 ? this.run.AddNewT() : this.run.GetTArray(pos); xs.Value = value; XWPFRun.preserveSpaces(xs); }
public TOC(CT_SdtBlock block) { this.block = block; CT_SdtPr sdtPr = block.AddNewSdtPr(); CT_DecimalNumber id = sdtPr.AddNewId(); id.val = ("4844945"); sdtPr.AddNewDocPartObj().AddNewDocPartGallery().val = ("Table of Contents"); CT_SdtEndPr sdtEndPr = block.AddNewSdtEndPr(); CT_RPr rPr = sdtEndPr.AddNewRPr(); CT_Fonts fonts = rPr.AddNewRFonts(); fonts.asciiTheme = (ST_Theme.minorHAnsi); fonts.eastAsiaTheme = (ST_Theme.minorHAnsi); fonts.hAnsiTheme = (ST_Theme.minorHAnsi); fonts.cstheme = (ST_Theme.minorBidi); CT_SdtContentBlock content = block.AddNewSdtContent(); CT_P p = content.AddNewP(); byte[] b = Encoding.Unicode.GetBytes("00EF7E24"); p.rsidR = b; p.rsidRDefault = b; CT_PPr pPr = p.AddNewPPr(); pPr.AddNewPStyle().val = ("TOCHeading"); pPr.AddNewJc().val = ST_Jc.center; CT_R run = p.AddNewR(); run.AddNewRPr().AddNewSz().val = 48; run.AddNewT().Value = ("Table of Contents"); run.AddNewBr().type = ST_BrType.textWrapping; // line break // TOC Field p = content.AddNewP(); pPr = p.AddNewPPr(); pPr.AddNewPStyle().val = "TOC1"; pPr.AddNewRPr().AddNewNoProof(); run = p.AddNewR(); run.AddNewFldChar().fldCharType = ST_FldCharType.begin; run = p.AddNewR(); CT_Text text = run.AddNewInstrText(); text.space = "preserve"; text.Value = (" TOC \\h \\z "); p.AddNewR().AddNewFldChar().fldCharType = ST_FldCharType.separate; }
public void AddRow(int level, string title, int page, string bookmarkRef) { CT_P ctP = this.block.sdtContent.AddNewP(); byte[] bytes = Encoding.Unicode.GetBytes("00EF7E24"); ctP.rsidR = bytes; ctP.rsidRDefault = bytes; CT_PPr ctPpr = ctP.AddNewPPr(); ctPpr.AddNewPStyle().val = nameof(TOC) + (object)level; CT_TabStop ctTabStop = ctPpr.AddNewTabs().AddNewTab(); ctTabStop.val = ST_TabJc.right; ctTabStop.leader = ST_TabTlc.dot; ctTabStop.pos = "8290"; ctPpr.AddNewRPr().AddNewNoProof(); CT_R ctR1 = ctP.AddNewR(); ctR1.AddNewRPr().AddNewNoProof(); ctR1.AddNewT().Value = title; CT_R ctR2 = ctP.AddNewR(); ctR2.AddNewRPr().AddNewNoProof(); ctR2.AddNewTab(); CT_R ctR3 = ctP.AddNewR(); ctR3.AddNewRPr().AddNewNoProof(); ctR3.AddNewFldChar().fldCharType = ST_FldCharType.begin; CT_R ctR4 = ctP.AddNewR(); ctR4.AddNewRPr().AddNewNoProof(); CT_Text ctText = ctR4.AddNewInstrText(); ctText.space = "preserve"; ctText.Value = " PAGEREF _Toc" + bookmarkRef + " \\h "; ctP.AddNewR().AddNewRPr().AddNewNoProof(); CT_R ctR5 = ctP.AddNewR(); ctR5.AddNewRPr().AddNewNoProof(); ctR5.AddNewFldChar().fldCharType = ST_FldCharType.separate; CT_R ctR6 = ctP.AddNewR(); ctR6.AddNewRPr().AddNewNoProof(); ctR6.AddNewT().Value = page.ToString(); CT_R ctR7 = ctP.AddNewR(); ctR7.AddNewRPr().AddNewNoProof(); ctR7.AddNewFldChar().fldCharType = ST_FldCharType.end; }
public void AddRow(int level, String title, int page, String bookmarkRef) { CT_SdtContentBlock contentBlock = this.block.sdtContent; CT_P p = contentBlock.AddNewP(); byte[] b = Encoding.Unicode.GetBytes("00EF7E24"); p.rsidR = b; p.rsidRDefault = b; CT_PPr pPr = p.AddNewPPr(); pPr.AddNewPStyle().val = ("TOC" + level); CT_Tabs tabs = pPr.AddNewTabs(); CT_TabStop tab = tabs.AddNewTab(); tab.val = (ST_TabJc.right); tab.leader = (ST_TabTlc.dot); tab.pos = "8290"; //(new BigInteger("8290")); pPr.AddNewRPr().AddNewNoProof(); CT_R Run = p.AddNewR(); Run.AddNewRPr().AddNewNoProof(); Run.AddNewT().Value = (title); Run = p.AddNewR(); Run.AddNewRPr().AddNewNoProof(); Run.AddNewTab(); Run = p.AddNewR(); Run.AddNewRPr().AddNewNoProof(); Run.AddNewFldChar().fldCharType = (ST_FldCharType.begin); // pageref run Run = p.AddNewR(); Run.AddNewRPr().AddNewNoProof(); CT_Text text = Run.AddNewInstrText(); text.space = "preserve";// (Space.PRESERVE); // bookmark reference text.Value = (" PAGEREF _Toc" + bookmarkRef + " \\h "); p.AddNewR().AddNewRPr().AddNewNoProof(); Run = p.AddNewR(); Run.AddNewRPr().AddNewNoProof(); Run.AddNewFldChar().fldCharType = (ST_FldCharType.separate); // page number run Run = p.AddNewR(); Run.AddNewRPr().AddNewNoProof(); Run.AddNewT().Value = page.ToString(); Run = p.AddNewR(); Run.AddNewRPr().AddNewNoProof(); Run.AddNewFldChar().fldCharType = (ST_FldCharType.end); }
public void TestGetText() { XWPFDocument doc = new XWPFDocument(); CT_Tbl table = new CT_Tbl(); CT_Row row = table.AddNewTr(); CT_Tc cell = row.AddNewTc(); CT_P paragraph = cell.AddNewP(); CT_R run = paragraph.AddNewR(); CT_Text text = run.AddNewT(); text.Value = ("finally I can Write!"); XWPFTable xtab = new XWPFTable(table, doc); Assert.AreEqual("finally I can Write!\n", xtab.GetText()); }
public void TestWhitespace() { String[] text = new String[] { " The quick brown fox", "\t\tjumped over the lazy dog" }; MemoryStream bos = new MemoryStream(); XWPFDocument doc = new XWPFDocument(); foreach (String s in text) { XWPFParagraph p1 = doc.CreateParagraph(); XWPFRun r1 = p1.CreateRun(); r1.SetText(s); } doc.Write(bos); MemoryStream bis = new MemoryStream(bos.ToArray()); var doc2 = new XWPFDocument(bis); var paragraphs = doc2.Paragraphs; Assert.AreEqual(2, paragraphs.Count); for (int i = 0; i < text.Length; i++) { XWPFParagraph p1 = paragraphs[i]; String expected = text[i]; Assert.AreEqual(expected, p1.Text); CT_P ctp = p1.GetCTP(); CT_R ctr = ctp.GetRArray(0); CT_Text ctText = ctr.GetTArray(0); // if text has leading whitespace then expect xml-fragment to have xml:space="preserve" set // <xml-fragment xml:space="preserve" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"> bool isWhitespace = Character.isWhitespace(expected[0]); Assert.AreEqual(isWhitespace, ctText.space == "preserve"); } }
public static CT_FldChar Parse(XmlNode node, XmlNamespaceManager namespaceManager) { if (node == null) { return(null); } CT_FldChar ctObj = new CT_FldChar(); if (node.Attributes["w:fldCharType"] != null) { ctObj.fldCharType = (ST_FldCharType)Enum.Parse(typeof(ST_FldCharType), node.Attributes["w:fldCharType"].Value); } if (node.Attributes["w:fldLock"] != null) { ctObj.fldLock = (ST_OnOff)Enum.Parse(typeof(ST_OnOff), node.Attributes["w:fldLock"].Value, true); } if (node.Attributes["w:dirty"] != null) { ctObj.dirty = (ST_OnOff)Enum.Parse(typeof(ST_OnOff), node.Attributes["w:dirty"].Value, true); } foreach (XmlNode childNode in node.ChildNodes) { if (childNode.LocalName == "ffData") { ctObj.ffDataField = CT_FFData.Parse(childNode, namespaceManager); } else if (childNode.LocalName == "fldData") { ctObj.fldDataField = CT_Text.Parse(childNode, namespaceManager); } else if (childNode.LocalName == "numberingChange") { ctObj.numberingChangeField = CT_TrackChangeNumbering.Parse(childNode, namespaceManager); } } return(ctObj); }
internal void InsertText(CT_Text text, int textIndex) { run.GetTList().Insert(textIndex, text); }
/** * Add the xml:spaces="preserve" attribute if the string has leading or trailing white spaces * * @param xs the string to check */ static void preserveSpaces(CT_Text xs) { String text = xs.Value; if (text != null && (text.StartsWith(" ") || text.EndsWith(" "))) { // XmlCursor c = xs.NewCursor(); // c.ToNextToken(); // c.InsertAttributeWithValue(new QName("http://www.w3.org/XML/1998/namespace", "space"), "preserve"); // c.Dispose(); xs.space = "preserve"; } }
public void TestSetHeader() { XWPFDocument sampleDoc = XWPFTestDataSamples.OpenSampleDocument("SampleDoc.docx"); // no header is Set (yet) XWPFHeaderFooterPolicy policy = sampleDoc.GetHeaderFooterPolicy(); Assert.IsNull(policy.GetDefaultHeader()); Assert.IsNull(policy.GetFirstPageHeader()); Assert.IsNull(policy.GetDefaultFooter()); CT_P ctP1 = new CT_P(); CT_R ctR1 = ctP1.AddNewR(); CT_Text t = ctR1.AddNewT(); t.Value = ("Paragraph in header"); // Commented MB 23 May 2010 //CTP ctP2 = CTP.Factory.NewInstance(); //CTR ctR2 = ctP2.AddNewR(); //CTText t2 = ctR2.AddNewT(); //t2.StringValue=("Second paragraph.. for footer"); // Create two paragraphs for insertion into the footer. // Previously only one was inserted MB 23 May 2010 CT_P ctP2 = new CT_P(); CT_R ctR2 = ctP2.AddNewR(); CT_Text t2 = ctR2.AddNewT(); t2.Value = ("First paragraph for the footer"); CT_P ctP3 = new CT_P(); CT_R ctR3 = ctP3.AddNewR(); CT_Text t3 = ctR3.AddNewT(); t3.Value = ("Second paragraph for the footer"); XWPFParagraph p1 = new XWPFParagraph(ctP1, sampleDoc); XWPFParagraph[] pars = new XWPFParagraph[1]; pars[0] = p1; XWPFParagraph p2 = new XWPFParagraph(ctP2, sampleDoc); XWPFParagraph p3 = new XWPFParagraph(ctP3, sampleDoc); XWPFParagraph[] pars2 = new XWPFParagraph[2]; pars2[0] = p2; pars2[1] = p3; // Set headers policy.CreateHeader(XWPFHeaderFooterPolicy.DEFAULT, pars); policy.CreateHeader(XWPFHeaderFooterPolicy.FIRST); // Set a default footer and capture the returned XWPFFooter object. XWPFFooter footer = policy.CreateFooter(XWPFHeaderFooterPolicy.DEFAULT, pars2); // Ensure the headers and footer were Set correctly.... Assert.IsNotNull(policy.GetDefaultHeader()); Assert.IsNotNull(policy.GetFirstPageHeader()); Assert.IsNotNull(policy.GetDefaultFooter()); // ....and that the footer object captured above Contains two // paragraphs of text. Assert.AreEqual(2, footer.Paragraphs.Count); // As an Additional Check, recover the defauls footer and // make sure that it Contains two paragraphs of text and that // both do hold what is expected. footer = policy.GetDefaultFooter(); XWPFParagraph[] paras = new XWPFParagraph[footer.Paragraphs.Count]; int i = 0; foreach (XWPFParagraph p in footer.Paragraphs) { paras[i++] = p; } Assert.AreEqual(2, paras.Length); Assert.AreEqual("First paragraph for the footer", paras[0].Text); Assert.AreEqual("Second paragraph for the footer", paras[1].Text); }
public void TestSetHeader() { XWPFDocument sampleDoc = XWPFTestDataSamples.OpenSampleDocument("SampleDoc.docx"); // no header is Set (yet) XWPFHeaderFooterPolicy policy = sampleDoc.GetHeaderFooterPolicy(); Assert.IsNull(policy.GetDefaultHeader()); Assert.IsNull(policy.GetFirstPageHeader()); Assert.IsNull(policy.GetDefaultFooter()); CT_P ctP1 = new CT_P(); CT_R ctR1 = ctP1.AddNewR(); CT_Text t = ctR1.AddNewT(); String tText = "Paragraph in header"; t.Value = tText; // Commented MB 23 May 2010 //CTP ctP2 = CTP.Factory.NewInstance(); //CTR ctR2 = ctP2.AddNewR(); //CTText t2 = ctR2.AddNewT(); //t2.StringValue=("Second paragraph.. for footer"); // Create two paragraphs for insertion into the footer. // Previously only one was inserted MB 23 May 2010 CT_P ctP2 = new CT_P(); CT_R ctR2 = ctP2.AddNewR(); CT_Text t2 = ctR2.AddNewT(); t2.Value = ("First paragraph for the footer"); CT_P ctP3 = new CT_P(); CT_R ctR3 = ctP3.AddNewR(); CT_Text t3 = ctR3.AddNewT(); t3.Value = ("Second paragraph for the footer"); XWPFParagraph p1 = new XWPFParagraph(ctP1, sampleDoc); XWPFParagraph[] pars = new XWPFParagraph[1]; pars[0] = p1; XWPFParagraph p2 = new XWPFParagraph(ctP2, sampleDoc); XWPFParagraph p3 = new XWPFParagraph(ctP3, sampleDoc); XWPFParagraph[] pars2 = new XWPFParagraph[2]; pars2[0] = p2; pars2[1] = p3; // Set headers XWPFHeader headerD = policy.CreateHeader(XWPFHeaderFooterPolicy.DEFAULT, pars); XWPFHeader headerF = policy.CreateHeader(XWPFHeaderFooterPolicy.FIRST); // Set a default footer and capture the returned XWPFFooter object. XWPFFooter footer = policy.CreateFooter(XWPFHeaderFooterPolicy.DEFAULT, pars2); // Ensure the headers and footer were Set correctly.... Assert.IsNotNull(policy.GetDefaultHeader()); Assert.IsNotNull(policy.GetFirstPageHeader()); Assert.IsNotNull(policy.GetDefaultFooter()); // ....and that the footer object captured above Contains two // paragraphs of text. Assert.AreEqual(2, footer.Paragraphs.Count); // Check the header created with the paragraph got them, and the one // created without got an empty one Assert.AreEqual(1, headerD.Paragraphs.Count); Assert.AreEqual(1, headerF.Paragraphs.Count); Assert.AreEqual(tText, headerD.Paragraphs[0].Text); Assert.AreEqual("", headerF.Paragraphs[0].Text); // As an Additional Check, recover the defauls footer and // make sure that it Contains two paragraphs of text and that // both do hold what is expected. footer = policy.GetDefaultFooter(); XWPFParagraph[] paras = new List <XWPFParagraph>(footer.Paragraphs).ToArray(); Assert.AreEqual(2, paras.Length); Assert.AreEqual("First paragraph for the footer", paras[0].Text); Assert.AreEqual("Second paragraph for the footer", paras[1].Text); // Add some text to the empty header String fText1 = "New Text!"; headerF.Paragraphs[0].InsertNewRun(0).SetText(fText1); // TODO Add another paragraph and check // Check it Assert.AreEqual(tText, headerD.Paragraphs[0].Text); Assert.AreEqual(fText1, headerF.Paragraphs[0].Text); // Save, re-open, ensure it's all still there XWPFDocument reopened = XWPFTestDataSamples.WriteOutAndReadBack(sampleDoc); policy = reopened.GetHeaderFooterPolicy(); Assert.IsNotNull(policy.GetDefaultHeader()); Assert.IsNotNull(policy.GetFirstPageHeader()); Assert.IsNull(policy.GetEvenPageHeader()); Assert.IsNotNull(policy.GetDefaultFooter()); Assert.IsNull(policy.GetFirstPageFooter()); Assert.IsNull(policy.GetEvenPageFooter()); // Check the new headers still have their text headerD = policy.GetDefaultHeader(); headerF = policy.GetFirstPageHeader(); Assert.AreEqual(tText, headerD.Paragraphs[0].Text); Assert.AreEqual(fText1, headerF.Paragraphs[0].Text); // Check the new footers have their new text too footer = policy.GetDefaultFooter(); paras = new List <XWPFParagraph>(footer.Paragraphs).ToArray(); Assert.AreEqual(2, paras.Length); Assert.AreEqual("First paragraph for the footer", paras[0].Text); Assert.AreEqual("Second paragraph for the footer", paras[1].Text); }