![](/img/trans.png)
[英]How to attach a new bookmark to an existing paragraph in word 2010 using OpenXml?
[英]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);
}
}
}
您好,我不知道您想要什么樣的超鏈接,但是我將以超鏈接為例向同一文檔中的書簽提供示例,讓我們假設我們將一個書簽附加到名為“ Mop”的段落上,如下所示:
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
}
基本上,我刪除了已有的文本,並在段落中添加了到超鏈接的書簽,其中包含文本所具有的字符串。
要刪除超鏈接幾乎相同,請刪除當前文本並添加普通文本:
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);
}
希望對您有幫助,如果您認為是的話,請標記為答案,謝謝。
你有嘗試過嗎?
if (para.InnerText.Equals("Functional Requirements:"))
{
para.Append(hp);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.