gpt4 book ai didi

.net - 在新文档中复制项目符号列表编号?

转载 作者:行者123 更新时间:2023-12-01 20:00:44 26 4
gpt4 key购买 nike

我有一个像这样的节点导入器

Dim nodeImporter As New Aspose.Words.NodeImporter(_wordDocument, documentComponentDocument,
Aspose.Words.ImportFormatMode.UseDestinationStyles)

我正在使用它将子节点从一个文档复制到另一个文档。我的子节点是项目符号列表。

documentComponentSection.Body.AppendChild(nodeImporter.ImportNode(childNode, True))

但我的问题是子节点的一些属性,例如ListLabel,即项目符号列表编号未被复制

根据您的回答,我尝试遵循。但当我为每个节点创建新文档时它不起作用。

Aspose.Words.Document srcDoc = new Aspose.Words.Document(Mydir + "input.docx");

Aspose.Words.Document dstDoc = new Aspose.Words.Document();
var ctr = 0;
int listid = 0;
Aspose.Words.Lists.List dstList = null;
foreach (Aspose.Words.Paragraph paragraph in srcDoc.GetChildNodes(Aspose.Words.NodeType.Paragraph, true))
{
Aspose.Words.NodeImporter imp = new Aspose.Words.NodeImporter(srcDoc, dstDoc, Aspose.Words.ImportFormatMode.KeepSourceFormatting);
Aspose.Words.Node impNode = imp.ImportNode(paragraph, true);
if (((Aspose.Words.Paragraph)impNode).IsListItem)
{
((Aspose.Words.Paragraph)impNode).ListFormat.ListLevel.StartAt = paragraph.ListFormat.List.ListId;
if (listid != paragraph.ListFormat.List.ListId)
{
listid = paragraph.ListFormat.List.ListId;
dstList = dstDoc.Lists.AddCopy(paragraph.ListFormat.List);
}


((Aspose.Words.Paragraph)impNode).ListFormat.List = dstList;
}
dstDoc.FirstSection.Body.RemoveAllChildren();
dstDoc.FirstSection.Body.AppendChild(impNode);
var index = ctr++;
dstDoc.Save(MyDir + index.ToString() + ".docx");
}

每个输出文档包含的列表索引为 1。

最佳答案

以下代码示例将列表项从源文档导入到新的空文档中,并保留列表标签(编号)值。

Aspose.Words.Document srcDoc = new Aspose.Words.Document(MyDir  + "input.docx");
DocumentBuilder builder = new DocumentBuilder(srcDoc);
srcDoc.UpdateListLabels();

Aspose.Words.Document dstDoc = new Aspose.Words.Document();
int ctr = 0;
Aspose.Words.NodeImporter imp = new Aspose.Words.NodeImporter(srcDoc, dstDoc, Aspose.Words.ImportFormatMode.KeepSourceFormatting);

foreach (Aspose.Words.Paragraph paragraph in srcDoc.GetChildNodes(Aspose.Words.NodeType.Paragraph, true))
{
if (paragraph.IsListItem)
{
ListLabel label = paragraph.ListLabel;
builder.MoveTo(paragraph);
builder.StartBookmark("bookmark_" + label.LabelValue);
builder.EndBookmark("bookmark_" + label.LabelValue);

Aspose.Words.Node impNode = imp.ImportNode(paragraph, true);

dstDoc.FirstSection.Body.RemoveAllChildren();
dstDoc.FirstSection.Body.AppendChild(impNode);

foreach (Bookmark bookmark in ((Aspose.Words.Paragraph)impNode).Range.Bookmarks)
{
if (!bookmark.Name.StartsWith("bookmark_"))
continue;

String listLabel = bookmark.Name.Replace("bookmark_", "");

try
{
((Aspose.Words.Paragraph)impNode).ListFormat.ListLevel.StartAt = Convert.ToInt32(listLabel);
break;
}
catch (Exception ex)
{
}
}

ctr++;
dstDoc.Range.Bookmarks.Clear();
dstDoc.Save(MyDir + ctr.ToString() + ".docx");
}
}

如果问题仍然存在,请在 Aspose.Words forum 中报告问题包含输入和预期输出文档。

我作为开发者布道者与 Aspose 合作。

关于.net - 在新文档中复制项目符号列表编号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32737818/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com