2016-05-17 71 views
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. 我要添加超链接,我已经访问字符串“功能需求”的超链接添加到Word中现有的段落或文本。
  2. 当超链接创建比我还想要一个方法来删除它。
+0

什么不在这里工作?它不会打印到要打印的行吗? –

+0

我已经删除了我的答案,因为这不是你的问题,我真的不知道使用openxml sdk管理这个,但是从文档中,你可能能够通过PrependChild()链接,然后调用Remove()删除现有的段落。希望这可以帮助。 –

+0

我会试试看,谢谢指出 –

回答

0

你试过这个吗?

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

嗨。如果要删除段落儿童中包含的超链接,可以使用RemoveChild ()方法。 –

1

喂好,我不知道你想拥有什么样的超链接,但我会给出一个超链接的例子同一文档内的书签,让我们supose我们书签添加到命名段落“猫扑”是这样的:

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

那么这种方式,我们可以添加一个超链接的文本“功能需求”:

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 
} 

基本上我删除existen文本,并加入到对使用文本所具有的字符串绘制超链接到书签。

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

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

希望它可以帮助你,请把它标记为答案,如果你认为它是,谢谢。