Hi all.
We want to get all of paragraph's page number from a big word.
I do it like this:https://forum.aspose.com/t/65265
the code below use about 10 minutes. how to improve this problem.
public class CatalogInfo
{
public int PageNum;
public double Left;
public double Top;
public string Capiton;
public OutlineLevel Outline;
}
public void split_test(string docPath)
{
Aspose.Words.Document doc = new Aspose.Words.Document(docPath);
//get nodes
NodeCollection nc = doc.GetChildNodes(NodeType.Paragraph, true);
Aspose.Words.DocumentBuilder builder = new Aspose.Words.DocumentBuilder(doc);
Dictionary fieldList = new System.Collections.Generic.Dictionary();
foreach (Aspose.Words.Paragraph p in nc)
{
if (p.ParagraphFormat.OutlineLevel != OutlineLevel.BodyText)
{
if (p.GetAncestor(NodeType.HeaderFooter) != null)
continue;
builder.MoveTo(p);
CatalogInfo c = new CatalogInfo();
c.Capiton = p.Range.Text;
c.PageNum = 0;
c.Outline = p.ParagraphFormat.OutlineLevel;
c.Top = builder.PageSetup.LeftMargin;//HeaderDistance
//slowly
Aspose.Words.Fields.Field field = builder.InsertField("PAGE");//this code use 2-3s.
fieldList.Add(field, c);//fieldList.length is 250.
}
}
doc.UpdatePageLayout();//it's slowly but only once.
foreach (KeyValuePair field in fieldList)
{
string number = GetFieldValue(field.Key.Start);
if (number.Equals("XXX"))
throw new Exception("Can't find the page number of a node inside a field or bookmark");
int pageNum = int.Parse(number);
field.Key.Remove();
Console.WriteLine(field.Value.Capiton);
Console.WriteLine(field.Value.Outline);
Console.WriteLine(pageNum);
Console.WriteLine(System.Environment.NewLine);
}
}
private static string GetFieldValue(Aspose.Words.Fields.FieldStart fieldStart)
{
//StringBuilder builder = new StringBuilder();
string rtn = "";
bool isAtSeparator = false;
for (Node node = fieldStart; node != null && node.NodeType != NodeType.FieldEnd; node = node.NextPreOrder(node.Document))
{
if (node.NodeType == NodeType.FieldSeparator)
isAtSeparator = true;
if (isAtSeparator && node.NodeType == NodeType.Run)
rtn = node.GetText();
//builder.Append(node.GetText());
}
return rtn;
}
----
Thanks.