[英]How to add hyperlink that links to heading in word with openxml
我正在嘗試添加一個超鏈接,該超鏈接鏈接到同一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);
我看不到我想念的東西。 我在超鏈接中設置了錨點,該錨點應鏈接到具有相同名稱的帶有書簽的標題。 (來自超鏈接的錨==標題中書簽的名稱)。
還是我需要向MainDocumentPart.HyperlinkRelationship添加一個HyperLinkRelationship,就像我想向網站添加帶有URI的超鏈接一樣?
您是將header
作為段落添加到body
,需要創建標頭部分-
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/zh-cn/library/office/cc546917.aspx
對於開放的XML(摘要)
// Your new paragraph with the hyperlink
Paragraph hyperlinkParagraph = new Paragraph();
// Run for the hyperlink
Run hyperlinkRun = new Run();
// Styling for the hyperlink
RunProperties runPropertiesHyperLink = new RunProperties(
new RunStyle { Val = "Hyperlink", },
new Underline { Val = UnderlineValues.Single },
new Color { ThemeColor = ThemeColorValues.Hyperlink });
// Actual hyperlink
Hyperlink xmlHyperLink = new Hyperlink()
{
Anchor = "https://stackoverflow.com",
DocLocation = "https://stackoverflow.com"
};
// Text that you see to click on
Text hyperLinkText = new Text("Click for going to StackOverflow!");
// Apply the styling in this order to the run
hyperlinkRun.Append(runPropertiesHyperLink);
hyperlinkRun.Append(hyperLinkText);
// Now add the run to the hyperlink
xmlHyperLink.Append(hyperlinkRun);
// Add the hyperlink to the paragraph
hyperlinkParagraph.Append(xmlHyperLink);
// Add paragraph to the body
_mainBody.Append(hyperlinkParagraph );
DocIO是一個.NET庫,可以讀取,寫入,修改和呈現Microsoft Word文檔。 使用DocIO,您可以非常輕松地添加書簽和超鏈接。 您不必擔心超鏈接存在於何處(在頁眉,頁腳或文檔正文中),而無需決定在何處添加引用。
如果您有資格,則可以通過社區許可計划免費獲得整套控件(也包括商業應用程序)。 社區許可證是完整的產品,沒有任何限制或水印。
步驟1 :創建控制台應用程序
步驟2 :添加對這3個程序集(Syncfusion.DocIO.Base,Syncfusion.Compression.Base和Syncfusion.OfficeChart.Base)的引用。 您還可以通過NuGet添加這些引用
步驟3 :復制並粘貼以下代碼段,以打開現有的Word文檔,替換內容並將其另存為Word文檔。
using Syncfusion.DocIO.DLS;
using Syncfusion.DocIO;
namespace DocIO_HyperlinkinkToBookmark
{
class Program
{
static void Main(string[] args)
{
using (WordDocument document = new WordDocument())
{
document.EnsureMinimal();
IWSection section = document.LastSection;
IWParagraph paragraph = document.LastParagraph;
//add text enclosed by BookmarkStart and BookmarkEnd into a paragraph
paragraph.AppendBookmarkStart("Title1Mark");
paragraph.AppendText("Title paragraph");
paragraph.AppendBookmarkEnd("Title1Mark");
//Add few paragraph of textual data
for (int i = 0; i < 10; i++)
section.AddParagraph().AppendText("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.");
paragraph = section.AddParagraph();
//Add a hyperlink with the specified display text and targets to Bookmark named "Title1Mark"
paragraph.AppendHyperlink("Title1Mark", "Link to Title", HyperlinkType.Bookmark);
document.Save("output.docx", FormatType.Docx);
}
}
}
}
有關DocIO的更多信息,請參閱我們的幫助文檔。
注意:我為Syncfusion工作
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.