Fri Jan 05, 2024 10:27 am
您好,
感谢您的询问。
对于你提出的第一个问题,当你添加段落后您可以设置如下代码更新目录:
- Code: Select all
//更新目录
doc.UpdateTableOfContents();
对于你提出的第二个问题,我们的产品支持查看某一个目录节点里面的段落功能,但是不支持导航到这个目录下面的段落功能
我把测试文档和完整的代码放在下面供您参考:
- Code: Select all
// 创建源文档对象
Document sourceDocument = new Document();
sourceDocument.LoadFromFile(@"E:\论坛\论坛35917\模拟文档.docx");
// 创建目标文档对象
Document destinationDoc = new Document();
// 获取源文档的第一个节
Spire.Doc.Section section = sourceDocument.Sections[0];
// 在目标文档中添加一个新的节
Spire.Doc.Section section1 = destinationDoc.AddSection();
// 查找源文档中包含"供应商的综合实力"的所有文本选择
TextSelection[] textSelections = sourceDocument.FindAllString("供应商的综合实力", false, true);
// 查找源文档中包含"质量管理体系认证证书"的所有文本选择
TextSelection[] textSelections1 = sourceDocument.FindAllString("质量管理体系认证证书", false, true);
int index1 = 0;
// 遍历textSelections数组,找到第一个不是"TOC1"样式的段落,并获取其在源文档中的索引
foreach (TextSelection selection in textSelections)
{
if (selection.GetAsOneRange().OwnerParagraph.StyleName != "TOC1")
{
index1 = section.Body.Paragraphs.IndexOf(selection.GetAsOneRange().OwnerParagraph);
Console.WriteLine(index1);
}
}
int index2 = 0;
// 遍历textSelections1数组,找到第一个不是"TOC2"样式的段落,并获取其在源文档中的索引
foreach (TextSelection selection in textSelections1)
{
if (selection.GetAsOneRange().OwnerParagraph.StyleName != "TOC2")
{
index2 = section.Body.Paragraphs.IndexOf(selection.GetAsOneRange().OwnerParagraph);
Console.WriteLine(index2);
}
}
// 调用ExtractBetweenParagraphs方法,将源文档中指定范围内的段落复制到目标文档中
ExtractBetweenParagraphs(sourceDocument, destinationDoc, index1, index2);
// 将目标文档保存为文件
destinationDoc.SaveToFile(@"..\Output14.docx", FileFormat.Docx);
}
private static void ExtractBetweenParagraphs(Document sourceDocument, Document destinationDocument, int startPara, int endPara)
{
// 遍历源文档中指定范围内的段落,并将其复制到目标文档中
for (int i = startPara + 2; i <= endPara; i++)
{
DocumentObject doobj = sourceDocument.Sections[0].Body.ChildObjects[i].Clone();
destinationDocument.Sections[0].Body.ChildObjects.Add(doobj);
}
}
Sincerely,
Ula
E-iceblue support team