交叉引用是指同一文檔中其餘地方的相關信息,咱們能夠建立對任何現有項目(如標題,腳註,書籤,標題和編號段落)的交叉引用,本文將展現如何使用C#和VB.NET的Spire.Doc建立書籤的交叉引用。ide
Step 1: 建立一個文檔實例。3d
Document doc = new Document(); Section section = doc.AddSection();
Step 2: 插入一個書籤。orm
Paragraph paragraph = section.AddParagraph(); paragraph.AppendBookmarkStart("MyBookmark"); paragraph.AppendText("Text inside a bookmark"); paragraph.AppendBookmarkEnd("MyBookmark");
Step 3: 建立交叉引用字段,並經過書籤名稱將其連接到書籤。對象
Field field = new Field(doc); field.Type = FieldType.FieldRef; field.Code = @"REF MyBookmark \p \h";
Step 4: 添加一個段落,並將該字段插入段落。blog
paragraph = section.AddParagraph(); paragraph.AppendText("For more information, see "); paragraph.ChildObjects.Add(field);
Step 5: 將FieldSeparator對象插入到段落中,該段落在字段中用做分隔符。圖片
FieldMark fieldSeparator= new FieldMark(doc, FieldMarkType.FieldSeparator); paragraph.ChildObjects.Add(fieldSeparator);
Step 6: 設置交叉引用字段的顯示文本。文檔
TextRange tr = new TextRange(doc); tr.Text = "above"; paragraph.ChildObjects.Add(tr);
Step 7: 將FieldEnd對象插入段落,該段落用於標記字段的末尾。it
FieldMark fieldEnd = new FieldMark(doc, FieldMarkType.FieldEnd); paragraph.ChildObjects.Add(fieldEnd);
Step 8: 保存到文件。io
doc.SaveToFile("output.docx", FileFormat.Docx2013);
導出:交叉引用顯示爲將讀者引用到引用項目的連接。form
完整代碼:
[C#]
Document doc = new Document(); Section section = doc.AddSection(); //create a bookmark Paragraph paragraph = section.AddParagraph(); paragraph.AppendBookmarkStart("MyBookmark"); paragraph.AppendText("Text inside a bookmark"); paragraph.AppendBookmarkEnd("MyBookmark"); //insert line breaks for (int i = 0; i < 4; i++) { paragraph.AppendBreak(BreakType.LineBreak); } //create a cross-reference field, and link it to bookmark Field field = new Field(doc); field.Type = FieldType.FieldRef; field.Code = @"REF MyBookmark \p \h"; //insert field to paragraph paragraph = section.AddParagraph(); paragraph.AppendText("For more information, see "); paragraph.ChildObjects.Add(field); //insert FieldSeparator object FieldMark fieldSeparator = new FieldMark(doc, FieldMarkType.FieldSeparator); paragraph.ChildObjects.Add(fieldSeparator); //set display text of the field TextRange tr = new TextRange(doc); tr.Text = "above"; paragraph.ChildObjects.Add(tr); //insert FieldEnd object to mark the end of the field FieldMark fieldEnd = new FieldMark(doc, FieldMarkType.FieldEnd); paragraph.ChildObjects.Add(fieldEnd); //save file doc.SaveToFile("output.docx", FileFormat.Docx2013);
[VB.NET]
Document doc = New Document() Dim section As Section = doc.AddSection() 'create a bookmark Dim paragraph As Paragraph = section.AddParagraph() paragraph.AppendBookmarkStart("MyBookmark") paragraph.AppendText("Text inside a bookmark") paragraph.AppendBookmarkEnd("MyBookmark") 'insert line breaks Dim i As Integer For i = 0 To 4- 1 Step i + 1 paragraph.AppendBreak(BreakType.LineBreak) Next 'create a cross-reference field, and link it to bookmark Dim field As Field = New Field(doc) field.Type = FieldType.FieldRef field.Code = "REF MyBookmark \p \h" 'insert field to paragraph paragraph = section.AddParagraph() paragraph.AppendText("For more information, see ") paragraph.ChildObjects.Add(field) 'insert FieldSeparator object Dim fieldSeparator As FieldMark = New FieldMark(doc, FieldMarkType.FieldSeparator) paragraph.ChildObjects.Add(fieldSeparator) 'set display text of the field Dim tr As TextRange = New TextRange(doc) tr.Text = "above" paragraph.ChildObjects.Add(tr) 'insert FieldEnd object to mark the end of the field Dim fieldEnd As FieldMark = New FieldMark(doc, FieldMarkType.FieldEnd) paragraph.ChildObjects.Add(fieldEnd) 'save file doc.SaveToFile("output.docx", FileFormat.Docx2013)