我想使用openxml为单词中的现有段落或文本添加超链接

时间:2016-05-17 08:20:07

标签: c# ms-word openxml openxml-sdk

        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. 我想添加超链接到字符串&#34;功能要求&#34;我已经访问过了。
  2. 创建超链接时,我还想要一种方法将其删除。

2 个答案:

答案 0 :(得分:1)

你好,我不知道你想要什么样的超链接,但是我会给同一文档里面的书签提供一个超链接的例子,让我们假设我们有一个附加到名为&#34的段落的书签。拖把&#34;像这样:

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

然后通过这种方式我们可以在文本中添加超链接&#34;功能要求&#34;:

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
}

基本上我删除了存在文本,并在段落中添加了带有文本字符串的书签的超链接。

要删除超链接几乎相同,请删除当前文本并添加正常文本:

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);
}

希望对您有所帮助,请将其标记为答案,如果您认为是,谢谢。

答案 1 :(得分:-1)

你试过这个吗?

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