public void TestHyperlink() { XWPFParagraph ps; XWPFParagraph ph; Assert.AreEqual(7, simple.Paragraphs.Count); Assert.AreEqual(5, hyperlink.Paragraphs.Count); // Simple text ps = simple.Paragraphs[(0)]; Assert.AreEqual("I am a test document", ps.ParagraphText); Assert.AreEqual(1, ps.Runs.Count); ph = hyperlink.Paragraphs[(4)]; Assert.AreEqual("We have a hyperlink here, and another.", ph.ParagraphText); Assert.AreEqual(3, ph.Runs.Count); // The proper way to do hyperlinks(!) Assert.IsFalse(ps.Runs[(0)] is XWPFHyperlinkRun); Assert.IsFalse(ph.Runs[(0)] is XWPFHyperlinkRun); Assert.IsTrue(ph.Runs[(1)] is XWPFHyperlinkRun); Assert.IsFalse(ph.Runs[(2)] is XWPFHyperlinkRun); XWPFHyperlinkRun link = (XWPFHyperlinkRun)ph.Runs[(1)]; Assert.AreEqual("http://poi.apache.org/", link.GetHyperlink(hyperlink).URL); }
public void TestHyperlink() { XWPFParagraph ps; XWPFParagraph ph; Assert.AreEqual(7, simple.Paragraphs.Count); Assert.AreEqual(5, hyperlink.Paragraphs.Count); // Simple text ps = simple.Paragraphs[(0)]; Assert.AreEqual("I am a test document", ps.ParagraphText); Assert.AreEqual(1, ps.Runs.Count); ph = hyperlink.Paragraphs[(4)]; Assert.AreEqual("We have a hyperlink here, and another.", ph.ParagraphText); Assert.AreEqual(3, ph.Runs.Count); // The proper way to do hyperlinks(!) Assert.IsFalse(ps.Runs[(0)] is XWPFHyperlinkRun); Assert.IsFalse(ph.Runs[(0)] is XWPFHyperlinkRun); Assert.IsTrue(ph.Runs[(1)] is XWPFHyperlinkRun); Assert.IsFalse(ph.Runs[(2)] is XWPFHyperlinkRun); XWPFHyperlinkRun link = (XWPFHyperlinkRun)ph.Runs[(1)]; Assert.AreEqual("http://poi.apache.org/", link.GetHyperlink(hyperlink).URL); // Test the old style decorator // You probably don't want to still be using it... Assert.AreEqual( "I am a test document", (new XWPFHyperlinkDecorator(ps, null, false)).Text ); Assert.AreEqual( "I am a test document", (new XWPFHyperlinkDecorator(ps, null, true)).Text ); Assert.AreEqual( "We have a hyperlink here, and another.hyperlink", (new XWPFHyperlinkDecorator(ph, null, false)).Text ); Assert.AreEqual( "We have a hyperlink here, and another.hyperlink <http://poi.apache.org/>", (new XWPFHyperlinkDecorator(ph, null, true)).Text ); }
static void Main(string[] args) { XWPFDocument doc = new XWPFDocument(); XWPFParagraph paragraph = doc.CreateParagraph(); XWPFRun run = paragraph.CreateRun(); run.SetText("This is a text paragraph having "); XWPFHyperlinkRun hyperlinkrun = CreateHyperlinkRun(paragraph, "https://www.google.com"); hyperlinkrun.SetText("a link to Google"); hyperlinkrun.SetColor("0000FF"); hyperlinkrun.SetUnderline(UnderlinePatterns.Single); run = paragraph.CreateRun(); run.SetText(" in it."); using (FileStream out1 = new FileStream("hyperlink.docx", FileMode.Create)) { doc.Write(out1); } }