0
        using (WordprocessingDocument myDoc = WordprocessingDocument.Open(@"E:\abdullah\import1.docx", true))
        {
            MainDocumentPart mainPart = myDoc.MainDocumentPart;
            Hyperlink hp = new Hyperlink();
            hp.Anchor = "artifact location";
            hp.DocLocation = @"E:\abdullah\test123.docx";
            foreach (var para in mainPart.Document.Descendants<Paragraph>())
            {
                //Console.WriteLine(para.InnerText);
                if (para.InnerText.Equals("Functional Requirements:"))
                {
                    Console.WriteLine(para.InnerText);
                }
            }
        }
  1. I want to add hyperlink to string "functional requirement" which I already accessed.
  2. When hyperlink created than I also want a method to remove it.

2 Answers2

2

Hello well I do not know what kind of hyperlink you want to have but I will give the example of a Hyperlink to a bookmark inside the same document, let us supose we have a bookmark appended to a Paragraph named "Mop" like this:

OpenXmlProcess.BookmarkStart bMrkS = new OpenXmlProcess.BookmarkStart() { Name = "Mop", Id = "1" };
OpenXmlProcess.BookmarkEnd bMrkE = new OpenXmlProcess.BookmarkEnd() { Id = "1" };
myParagraph.Append(bMrkS);
myParagraph.Append(bMrkE);

Then this way we can add a Hyperlink to the text "functional requirement":

if (para.InnerText == "Functional Requirements:")
{
    //--We remove the current texts of the paragraph, a new one will be added within the hyperlink
    foreach (OpenXmlProcess.Text tes in para.Descendants<OpenXmlProcess.Text>().ToList()) 
    {
        tes.Remove();
    }
     //-------------Apply some style--------------
     OpenXmlProcess.RunFonts runFont = new OpenXmlProcess.RunFonts();
     runFont.EastAsia = "Arial";
     OpenXmlProcess.FontSize size = new OpenXmlProcess.FontSize();
     size.Val = new OpenXML.StringValue("20");
     //-------------------------------------------
     OpenXmlProcess.Hyperlink hyp = new OpenXmlProcess.Hyperlink() { History = true, Anchor = "Mop" }; //--Point to the bookmark
     OpenXmlProcess.Run ruG = new OpenXmlProcess.Run() { RsidRunProperties = "00D56462" };
     OpenXmlProcess.RunProperties runProp = new OpenXmlProcess.RunProperties();
     OpenXmlProcess.RunStyle rnStyl = new OpenXmlProcess.RunStyle() { Val = "Hyperlink" };
     runProp.Append(rnStyl);
     runProp.Append(runFont);
     runProp.Append(size);
     //----Create a new text with our original string and append it to the hyperlink
     OpenXmlProcess.Text txL = new OpenXmlProcess.Text();
     txL.Text = "Functional Requirements:";

     ruG.Append(runProp);
     ruG.Append(txL);

     hyp.Append(ruG);
     para.Append(hyp); //Append the hyperlink to our paragraph
}

Basically I remove the existen text and add to the paragraph a hyperlink to the bookmark with the string the text had.

To remove the Hyperlink is nearly the same, remove the current text and add a normal one:

if (para.InnerText == "Functional Requirements:")
{
     //--We remove the current text, a new one will be added within the hyperlink
     foreach (OpenXmlProcess.Text tes in para.Descendants<OpenXmlProcess.Text>().ToList()) 
     {
          tes.Remove();
     }
     //-------------Apply some style--------------
     OpenXmlProcess.RunFonts runFont = new OpenXmlProcess.RunFonts();
                runFont.EastAsia = "Arial";
     OpenXmlProcess.FontSize size = new OpenXmlProcess.FontSize();
     size.Val = new OpenXML.StringValue("20");
     //-------------------------------------------
     OpenXmlProcess.Run ruG = new OpenXmlProcess.Run() { RsidRunProperties = "00D56462" };
     OpenXmlProcess.RunProperties runProp = new OpenXmlProcess.RunProperties();
     runProp.Append(runFont);
     runProp.Append(size);
     //----Create a new text with our original string
     OpenXmlProcess.Text txL = new OpenXmlProcess.Text();
     txL.Text = "Functional Requirements:";

     ruG.Append(runProp);
     ruG.Append(txL);
     para.Append(ruG);
}

Hope it helps you, please mark it as answer if you think it is, thanks.

JCO9
  • 960
  • 1
  • 15
  • 24
-1

Did you tried this?

if (para.InnerText.Equals("Functional Requirements:"))
{
    para.Append(hp);
}
Yves Israel
  • 408
  • 3
  • 5
  • Hi. If you want to remove an hyperlink contained in the paragraph children, you can use the RemoveChild() method. – Yves Israel Aug 16 '16 at 16:49
  • Answers should generally be longer. You didn't show what hp and para is. Or even what type they are. What are you trying to do? find the paragraph that only contains this text? where are you looking? – Little geek Aug 10 '18 at 08:21