public void SetUp() { XWPFDocument doc = new XWPFDocument(); p = doc.CreateParagraph(); this.ctRun = new CT_R(); }
/** * return the XWPFRun-Element which owns the CTR Run-Element * * @param r */ public XWPFRun GetRun(CT_R r) { for (int i = 0; i < GetRuns().Count; i++) { if (GetRuns()[(i)].GetCTR() == r) { return GetRuns()[(i)]; } } return null; }
/** * add a new run at the end of the position of * the content of parameter run * @param run */ protected void AddRun(CT_R Run) { //int pos; //pos = paragraph.RList.Size(); //paragraph.AddNewR(); //paragraph.SetRArray(pos, Run); throw new NotImplementedException(); }
/** * @param r the CT_R bean which holds the run.attributes * @param p the parent paragraph */ public XWPFRun(CT_R r, IRunBody p) { this.run = r; this.parent = p; /** * reserve already occupied Drawing ids, so reserving new ids later will * not corrupt the document */ IList<CT_Drawing> drawingList = r.GetDrawingList(); foreach (CT_Drawing ctDrawing in drawingList) { List<CT_Anchor> anchorList = ctDrawing.GetAnchorList(); foreach (CT_Anchor anchor in anchorList) { if (anchor.docPr != null) { this.Document.DrawingIdManager.Reserve(anchor.docPr.id); } } List<CT_Inline> inlineList = ctDrawing.GetInlineList(); foreach (CT_Inline inline in inlineList) { if (inline.docPr != null) { this.Document.DrawingIdManager.Reserve(inline.docPr.id); } } } //// Look for any text in any of our pictures or Drawings StringBuilder text = new StringBuilder(); List<object> pictTextObjs = new List<object>(); foreach (CT_Picture pic in r.GetPictList()) pictTextObjs.Add(pic); foreach (CT_Drawing draw in drawingList) pictTextObjs.Add(draw); //foreach (object o in pictTextObjs) //{ //todo:: imlement this //XmlObject[] t = o.SelectPath("declare namespace w='http://schemas.openxmlformats.org/wordprocessingml/2006/main' .//w:t"); //for (int m = 0; m < t.Length; m++) //{ // NodeList kids = t[m].DomNode.ChildNodes; // for (int n = 0; n < kids.Length; n++) // { // if (kids.Item(n) is Text) // { // if (text.Length > 0) // text.Append("\n"); // text.Append(kids.Item(n).NodeValue); // } // } //} //} pictureText = text.ToString(); // Do we have any embedded pictures? // (They're a different CT_Picture, under the Drawingml namespace) pictures = new List<XWPFPicture>(); foreach (object o in pictTextObjs) { foreach (OpenXmlFormats.Dml.Picture.CT_Picture pict in GetCTPictures(o)) { XWPFPicture picture = new XWPFPicture(pict, this); pictures.Add(picture); } } }
public XWPFRun(CT_R r, XWPFParagraph p) : this(r, (IRunBody)p) { }
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); }
public XWPFHyperlinkRun(CT_Hyperlink1 hyperlink, CT_R Run, IRunBody p) : base(Run, p) { this.hyperlink = hyperlink; }
public XWPFHyperlinkRun(CT_Hyperlink1 hyperlink, CT_R Run, XWPFParagraph p) : base(Run, p) { this.hyperlink = hyperlink; }
/** * add a new run at the end of the position of * the content of parameter run * @param run */ protected internal void AddRun(CT_R run) { int pos= paragraph.GetRList().Count; paragraph.AddNewR(); paragraph.SetRArray(pos, run); }
/** * @param r the CT_R bean which holds the run.attributes * @param p the parent paragraph */ public XWPFRun(CT_R r, IRunBody p) { this.run = r; this.parent = p; /** * reserve already occupied Drawing ids, so reserving new ids later will * not corrupt the document */ IList <CT_Drawing> drawingList = r.GetDrawingList(); foreach (CT_Drawing ctDrawing in drawingList) { List <CT_Anchor> anchorList = ctDrawing.GetAnchorList(); foreach (CT_Anchor anchor in anchorList) { if (anchor.docPr != null) { this.Document.DrawingIdManager.Reserve(anchor.docPr.id); } } List <CT_Inline> inlineList = ctDrawing.GetInlineList(); foreach (CT_Inline inline in inlineList) { if (inline.docPr != null) { this.Document.DrawingIdManager.Reserve(inline.docPr.id); } } } //// Look for any text in any of our pictures or Drawings StringBuilder text = new StringBuilder(); List <object> pictTextObjs = new List <object>(); foreach (CT_Picture pic in r.GetPictList()) { pictTextObjs.Add(pic); } foreach (CT_Drawing draw in drawingList) { pictTextObjs.Add(draw); } //foreach (object o in pictTextObjs) //{ //todo:: imlement this //XmlObject[] t = o.SelectPath("declare namespace w='http://schemas.openxmlformats.org/wordprocessingml/2006/main' .//w:t"); //for (int m = 0; m < t.Length; m++) //{ // NodeList kids = t[m].DomNode.ChildNodes; // for (int n = 0; n < kids.Length; n++) // { // if (kids.Item(n) is Text) // { // if (text.Length > 0) // text.Append("\n"); // text.Append(kids.Item(n).NodeValue); // } // } //} //} pictureText = text.ToString(); // Do we have any embedded pictures? // (They're a different CT_Picture, under the Drawingml namespace) pictures = new List <XWPFPicture>(); foreach (object o in pictTextObjs) { foreach (OpenXmlFormats.Dml.Picture.CT_Picture pict in GetCTPictures(o)) { XWPFPicture picture = new XWPFPicture(pict, this); pictures.Add(picture); } } }
public XWPFParagraph(CT_P prgrph, IBody part) { this.paragraph = prgrph; this.part = part; this.document = part.GetXWPFDocument(); if (document == null) { throw new NullReferenceException(); } // Build up the character runs runs = new List <XWPFRun>(); iRuns = new List <IRunElement>(); BuildRunsInOrderFromXml(paragraph.Items); // Look for bits associated with the runs foreach (XWPFRun run in runs) { CT_R r = run.GetCTR(); if (document != null) { for (int i = 0; i < r.Items.Count; i++) { object o = r.Items[i]; if (o is CT_FtnEdnRef) { CT_FtnEdnRef ftn = (CT_FtnEdnRef)o; footnoteText.Append("[").Append(ftn.id).Append(": "); XWPFFootnote footnote = null; if (r.ItemsElementName.Count > i && r.ItemsElementName[i] == RunItemsChoiceType.endnoteReference) { footnote = document.GetEndnoteByID(int.Parse(ftn.id)); if (footnote == null) { footnote = document.GetFootnoteByID(int.Parse(ftn.id)); } } else { footnote = document.GetFootnoteByID(int.Parse(ftn.id)); if (footnote == null) { footnote = document.GetEndnoteByID(int.Parse(ftn.id)); } } if (footnote != null) { bool first = true; foreach (XWPFParagraph p in footnote.Paragraphs) { if (!first) { footnoteText.Append("\n"); first = false; } footnoteText.Append(p.Text); } } footnoteText.Append("]"); } } } } }
/// <summary> /// this methods parse the paragraph and search for the string searched. /// If it finds the string, it will return true and the position of the String will be saved in the parameter startPos. /// </summary> /// <param name="searched"></param> /// <param name="startPos"></param> /// <returns></returns> public TextSegement SearchText(String searched, PositionInParagraph startPos) { int startRun = startPos.Run, startText = startPos.Text, startChar = startPos.Char; int beginRunPos = 0, candCharPos = 0; bool newList = false; for (int runPos = startRun; runPos < paragraph.GetRList().Count; runPos++) { int beginTextPos = 0, beginCharPos = 0, textPos = 0, charPos = 0; CT_R ctRun = paragraph.GetRList()[runPos]; foreach (object o in ctRun.Items) { if (o is CT_Text) { if (textPos >= startText) { String candidate = ((CT_Text)o).Value; if (runPos == startRun) { charPos = startChar; } else { charPos = 0; } for (; charPos < candidate.Length; charPos++) { if ((candidate[charPos] == searched[0]) && (candCharPos == 0)) { beginTextPos = textPos; beginCharPos = charPos; beginRunPos = runPos; newList = true; } if (candidate[charPos] == searched[candCharPos]) { if (candCharPos + 1 < searched.Length) { candCharPos++; } else if (newList) { TextSegement segement = new TextSegement(); segement.BeginRun = (beginRunPos); segement.BeginText = (beginTextPos); segement.BeginChar = (beginCharPos); segement.EndRun = (runPos); segement.EndText = (textPos); segement.EndChar = (charPos); return(segement); } } else { candCharPos = 0; } } } textPos++; } else if (o is CT_ProofErr) { //c.RemoveXml(); } else if (o is CT_RPr) { //do nothing } else { candCharPos = 0; } } } return(null); }
/** * add a new run at the end of the position of * the content of parameter run * @param run */ protected void AddRun(CT_R Run) { int pos; pos = paragraph.GetRList().Count; paragraph.AddNewR(); paragraph.SetRArray(pos, Run); }
public void TestRuns() { XWPFDocument doc = new XWPFDocument(); XWPFParagraph p = doc.CreateParagraph(); CT_R run = new CT_R(); XWPFRun r = new XWPFRun(run, doc.CreateParagraph()); p.AddRun(r); p.AddRun(r); Assert.IsNotNull(p.GetRun(run)); Assert.IsNull(p.GetRun(null)); }
/** * return the XWPFRun-Element which owns the CTR Run-Element * * @param r */ public XWPFRun GetRun(CT_R r) { for (int i = 0; i < Runs.Count; i++) { if (Runs[i].GetCTR() == r) { return Runs[i]; } } return null; }
public XWPFSharedRun(CT_R ctR, IRunBody p) { this.run = ctR; this.parent = p; SetFontFamily("Cambria Math", FontCharRange.None); }