Split word by table of content with C# .net

Hi all,
I worked with Aspose word with C# .net to split the document with sections, it’s working good, but now i want to split my document with the table of content, i don’t know if i can do this with Aspose or no.
Exemple:

       Introduction.......................1
       Title 1................................2
       Title 3................................3
       Conclusion........................4

this document return to me for document after split it.

Thank you.

@jugurtha,

Please ZIP and attach the following resources here for testing:

  • Your simplified source Word document containing the TOC (Table of Contents)
  • Your expected DOCX files showing the desired outputs. You can create these document(s) manually by using MS Word.

As soon as you get these pieces of information ready, we will start further investigation into your scenario and provide you code to achieve the same outputs by using Aspose.Words for .NET.

Hi @awais.hafeez, Thank you for your answer,
I send to you the ZIP containing:
-the Source Word containing the TOC.
-The Docx files showing my desired output.
Aspose Word.zip (825.5 KB)

Thank you.
Jugurtha

@jugurtha,

We are checking this scenario and will get back to you soon.

Hi @awais.hafeez, Thank you for your answer,
I send to you the ZIP containing:
-the Source Word containing the TOC.
-The Docx files showing my desired output.
Aspose Word.zip (825.5 KB)

Thank you.
Jugurtha

Aspose Word.zip (825 KB)

@jugurtha,

You can build logic on following code to get the desired outputs (split document by TOC entries):

Document doc = new Document("C:\\temp\\Aspose Word\\Source Word.docx");
DocumentBuilder builder = new DocumentBuilder(doc);

ArrayList listOfParagraphs = new ArrayList();
foreach (Field field in doc.Range.Fields)
{
    if (field.Type.Equals(Aspose.Words.Fields.FieldType.FieldHyperlink))
    {
        FieldHyperlink hyperlink = (FieldHyperlink)field;
        if (hyperlink.SubAddress != null && hyperlink.SubAddress.StartsWith("_Toc"))
        {
            Paragraph tocItem = (Paragraph)field.Start.GetAncestor(NodeType.Paragraph);
            if (tocItem != null && tocItem.Range.Replace(ControlChar.Tab, ControlChar.Tab) > 1)
            {
                Bookmark bm = doc.Range.Bookmarks[hyperlink.SubAddress];
                // Get the location this TOC Item is pointing to
                Paragraph pointer = (Paragraph)bm.BookmarkEnd.GetAncestor(NodeType.Paragraph);
                listOfParagraphs.Add(pointer);
            }
        }
    }
}

for (int i = 0; i < listOfParagraphs.Count; i++)
{
    Paragraph startPara = (Paragraph)listOfParagraphs[i];
    Paragraph endPara = null;

    if (i + 1 == listOfParagraphs.Count)
        endPara = doc.LastSection.Body.LastParagraph;
    else
        endPara = (Paragraph)listOfParagraphs[i + 1];

    ArrayList extractedNodes = Common.ExtractContent(startPara, endPara, true);

    // Insert the content into a new separate document and save it to disk.
    Document dstDoc = Common.GenerateDocument(doc, extractedNodes);
    dstDoc.LastSection.Body.LastParagraph.Remove();

    dstDoc.Save("C:\\Temp\\Aspose Word\\output" + i + ".docx");
}

Please also refer to the following section of documentation:

Hi Awais,
Thank’s for your response.
I execute the code with the word Source with a table of contents of 4 titles, it’s working fine.
But when i try with another word with a table of content of 52 titles, the bookmarks in null like in the photo below:

and in the i see the bookmarks:
image.png

Thank you.

Thank you @Awais Hafeez via Free Support Forum - aspose.com
i found the solution.

Best regards

image.png (26.9 KB)

image.png (4.67 KB)

@jugurtha,

It is great that you were able to find what you were looking for. Please let us know any time you may have any further queries in future.

Hi Awais,

I had a problem with aspose .net, when i try to split my word with TOC, it’s working but it’s add also the hyperlink of figure:

Table of content:
1 Introduction…1
2 Title 1…2
3 Title 3…4

Figures:
Figure 1: title…8
Figure 2: title…9
Figure 3: title…10

When i execute my code, it creates 6 word (3 of TOC and 3 of figures).
but my desired outputs is just the 3 word of the table of contents.

my code:

foreach (Field field in doc.Range.Fields)
{
    // Field field = doc.Range.Fields[sectionNumber];
    if (field.Type.Equals(Aspose.Words.Fields.FieldType.FieldHyperlink))
    {
        FieldHyperlink hyperlink = (FieldHyperlink)field;
        if (hyperlink.SubAddress != null && hyperlink.SubAddress.StartsWith("_Toc"))
        {
            Paragraph tocItem = (Paragraph)field.Start.GetAncestor(NodeType.Paragraph);
            if (tocItem != null && tocItem.Range.Replace(ControlChar.Tab, ControlChar.Tab) > 1)
            {
                Bookmark bm = doc.Range.Bookmarks[hyperlink.SubAddress];
                // Get the location this TOC Item is pointing to
                Paragraph pointer = (Paragraph)bm.BookmarkEnd.GetAncestor(NodeType.Paragraph);
                listOfParagraphs.Add(pointer);
            }
        }
    }
}

The same code above

Best regards

@jugurtha,

Please ZIP and attach the following resources here for testing:

  • Your simplified source Word document containing the TOC (Table of Contents and Table of Figures)
  • Your expected DOCX files showing the desired outputs. You can create these document(s) manually by using MS Word.

As soon as you get these pieces of information ready, we will start further investigation into your scenario and provide you code to achieve the same outputs by using Aspose.Words for .NET.

A post was merged into an existing topic: Problem hyperlink in table content and figure

A post was merged into an existing topic: Problem hyperlink in table content and figure