Abdullah Khan
Abdullah Khan

Reputation: 49

I want to add a hyperlink to an existing paragraph or text in word using openxml

        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.

Upvotes: 0

Views: 2681

Answers (2)

JCO9
JCO9

Reputation: 990

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.

Upvotes: 2

Yves Israel
Yves Israel

Reputation: 408

Did you tried this?

if (para.InnerText.Equals("Functional Requirements:"))
{
    para.Append(hp);
}

Upvotes: -1

Related Questions