我正在尝试添加一个链接到同一 Word 文档内的标题的超链接。
这是我到目前为止的代码:
首先我添加超链接
Paragraph p = new Paragraph(); Hyperlink h = new Hyperlink(){ Anchor = new StringValue("_Link") }; Run r = new Run(); RunProperties rp = new RunProperties(){ Val = "Hyperlink" }; Text t = new Text("Click here"); r.Append(rp); r.Append(t); p.Append(r); body.Append(p);
然后添加标题(带有必要的书签)
Paragraph p = new Paragraph(); Run r = new Run(new Text("My Heading")); ParagraphProperties pp = new ParagraphProperties(); ParagraphStyleId psi = new ParagraphStyleId(){ Val = new StringValue("Heading1") }; p.Append(r); p.Append(pp); p.Append(psi); p.Append(new BookmarkStart() { Name = new StringValue("_Link") }; p.Append(new BookmarkEnd()); body.Append(p);
我不明白我错过了什么。我在超链接中设置了 anchor ,该 anchor 应该链接到带有具有相同名称的书签的标题。 (超链接中的 anchor == 标题中书签中的名称)。
或者我是否需要将 HyperLinkRelationship 添加到 MainDocumentPart.HyperlinkRelationship,就像我想要向网站添加带有 URI 的超链接时所做的那样?
最佳答案
您要将 header
作为段落添加到 body
中,您需要创建 header 部分 -
HeaderPart headerPart = mainDocumentPart.AddNewPart<HeaderPart>();
string headerPartId = mainDocumentPart.GetIdOfPart(headerPart);
GenerateHeaderPartContent(headerPart);
以及GenerateHeaderPartContent
的代码
private void GeneratePartContent(HeaderPart part)
{
Header header1 = new Header(){ MCAttributes = new MarkupCompatibilityAttributes(){ Ignorable = "w14 w15 wp14" } };
header1.AddNamespaceDeclaration("wpc", "http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas");
header1.AddNamespaceDeclaration("mc", "http://schemas.openxmlformats.org/markup-compatibility/2006");
header1.AddNamespaceDeclaration("o", "urn:schemas-microsoft-com:office:office");
header1.AddNamespaceDeclaration("r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships");
header1.AddNamespaceDeclaration("m", "http://schemas.openxmlformats.org/officeDocument/2006/math");
header1.AddNamespaceDeclaration("v", "urn:schemas-microsoft-com:vml");
header1.AddNamespaceDeclaration("wp14", "http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing");
header1.AddNamespaceDeclaration("wp", "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing");
header1.AddNamespaceDeclaration("w10", "urn:schemas-microsoft-com:office:word");
header1.AddNamespaceDeclaration("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main");
header1.AddNamespaceDeclaration("w14", "http://schemas.microsoft.com/office/word/2010/wordml");
header1.AddNamespaceDeclaration("w15", "http://schemas.microsoft.com/office/word/2012/wordml");
header1.AddNamespaceDeclaration("wpg", "http://schemas.microsoft.com/office/word/2010/wordprocessingGroup");
header1.AddNamespaceDeclaration("wpi", "http://schemas.microsoft.com/office/word/2010/wordprocessingInk");
header1.AddNamespaceDeclaration("wne", "http://schemas.microsoft.com/office/word/2006/wordml");
header1.AddNamespaceDeclaration("wps", "http://schemas.microsoft.com/office/word/2010/wordprocessingShape");
Paragraph paragraph1 = new Paragraph(){ RsidParagraphAddition = "00225DC9", RsidRunAdditionDefault = "00225DC9" };
ParagraphProperties paragraphProperties1 = new ParagraphProperties();
ParagraphStyleId paragraphStyleId1 = new ParagraphStyleId(){ Val = "Header" };
paragraphProperties1.Append(paragraphStyleId1);
BookmarkStart bookmarkStart1 = new BookmarkStart(){ Name = "HeadingBookmark", Id = "1" };
Run run1 = new Run();
Text text1 = new Text();
text1.Text = "Test";
run1.Append(text1);
paragraph1.Append(paragraphProperties1);
paragraph1.Append(bookmarkStart1);
paragraph1.Append(run1);
BookmarkEnd bookmarkEnd1 = new BookmarkEnd(){ Id = "1" };
header1.Append(paragraph1);
header1.Append(bookmarkEnd1);
part.Header = header1;
}
标题部分准备好后,将其添加到文档部分属性 -
HeaderReference headerReference1 = new HeaderReference(){ Type = HeaderFooterValues.Default, Id = headerPartId };
也是一个很好的检查引用 - https://msdn.microsoft.com/en-us/library/office/cc546917.aspx
关于c# - 如何使用openxml添加链接到word中标题的超链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37573408/