创建书签打开XML [英] Create Bookmarks Open XML
问题描述
是否可以在开放XML中创建书签c# 并插入交叉引用?
Is it possible to create a bookmark in open XML c# and insert cross references?
推荐答案
你好,
我建议你
下载Open XML SDK工具以比较文件并生成代码。
I would suggest you download Open XML SDK Tool to compare files and generate code.
您可以创建一个没有书签和交叉引用的源文件,然后创建一个插入书签和交叉引用的目标文件。比较这两个文件后,您可以查看不同的包并查看包裹代码。
You could create a source file which doesnt have bookmark and cross reference, then create a target file which inserts a bookmark and cross references. After comparing the two files, you could view different package and view package code.
以下是将书签插入第1段并插入 的示例。在第3段之后链接到书签的交叉引用。
Here is the example for inserting bookmark to paragraph 1 and inserting a cross reference linked to the bookmark after paragraph 3.
Body body = doc.MainDocumentPart.Document.Body;
Paragraph paragraph1 = doc.MainDocumentPart.Document.Body.Elements<Paragraph>().First();
BookmarkStart bookmarkStart = new BookmarkStart() { Name = "p1", Id = "1" };
BookmarkEnd bookmarkEnd = new BookmarkEnd() { Id = "1" };
body.InsertBefore<BookmarkStart>(bookmarkStart, paragraph1);
body.InsertAfter<BookmarkEnd>(bookmarkEnd, paragraph1);
Paragraph paragraph3 = doc.MainDocumentPart.Document.Body.Elements<Paragraph>().ElementAt(2);
Run run2 = new Run() { RsidRunAddition = "009B0519" };
FieldChar fieldChar1 = new FieldChar() { FieldCharType = FieldCharValues.Begin };
run2.Append(fieldChar1);
Run run3 = new Run() { RsidRunAddition = "009B0519" };
FieldCode fieldCode1 = new FieldCode() { Space = SpaceProcessingModeValues.Preserve };
fieldCode1.Text = " REF p1 \\h "; //Link to bookmark p1
run3.Append(fieldCode1);
Run run4 = new Run() { RsidRunAddition = "009B0519" };
FieldChar fieldChar2 = new FieldChar() { FieldCharType = FieldCharValues.Separate };
run4.Append(fieldChar2);
Run run5 = new Run() { RsidRunAddition = "009B0519" };
Text text2 = new Text();
text2.Text = "Link To Paragraph1";
run5.Append(text2);
Run run6 = new Run();
FieldChar fieldChar3 = new FieldChar() { FieldCharType = FieldCharValues.End };
run6.Append(fieldChar3);
paragraph3.Append(run2);
paragraph3.Append(run3);
paragraph3.Append(run4);
paragraph3.Append(run5);
paragraph3.Append(run6);
问候,
Celeste
这篇关于创建书签打开XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!