Importing listing nodes with destination numbering

Hi,

I am trying to insert a node form one document to another. The nodes are part of list items.
I want to retain the destination numbering style and sequence while importing the node form source to destination document.
Please find attached doc1.doc, doc2.doc and result.doc . Using the follwing code I get the result.doc the listing order is not maintained. Please can you suggest how to take care of the same.

import com.aspose.words.Bookmark;
import com.aspose.words.CompositeNode;
import com.aspose.words.Document;
import com.aspose.words.ImportFormatMode;
import com.aspose.words.Node;
import com.aspose.words.NodeImporter;
import com.aspose.words.NodeType;

public class CompareDocs
{
    public static void main(String[] args)
    {
        try
        {
            Document doc1 = new Document("C:/doc1.doc");
            Document doc2 = new Document("C:/doc2.doc");

            Bookmark srcdb = doc1.getRange().getBookmarks().get("section_402882592b1f586d012b1fc754db0009");
            Node currNode = srcdb.getBookmarkStart().getParentNode();

            Bookmark destbk = doc2.getRange().getBookmarks().get("section_402882592b1f586d012b1fc754db0005");
            Node destNode = destbk.getBookmarkStart().getParentNode();

            insertDocument(destNode, currNode);

            doc2.save("C:/result.doc");
        }
        catch (CloneNotSupportedException e)
        {
            e.printStackTrace();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

    }

    public static void insertDocument(Node insertAfterNode, Node srcDoc) throws Exception
    {
        // Make sure that the node is either a pargraph or table.
        if (insertAfterNode.getNodeType() != NodeType.PARAGRAPH &&
            insertAfterNode.getNodeType() != NodeType.TABLE)
        {
            throw new Exception("The destination node should be either a paragraph or table.");
        }

        // We will be inserting into the parent of the destination paragraph.
        CompositeNode dstStory = insertAfterNode.getParentNode();

        // This object will be translating styles and lists during the import.
        NodeImporter importer = new NodeImporter(srcDoc.getDocument(), insertAfterNode.getDocument(),
            ImportFormatMode.USE_DESTINATION_STYLES);

        Node newNode = importer.importNode(srcDoc, true);

        dstStory.insertAfter(newNode, insertAfterNode);
    }
}

Thanks in advance.

Hi

Thanks for your inquiry. I think, in your case, you can use the technique described here:
https://forum.aspose.com/t/73721
Hope this helps. Please let me know if you need more information, I will be glad to help you.
Best regards,

Hi,

I went through the link specified, the code and the docs there are working fine.
Now my issue is that how do I apply same style names to the docs. Both the docs are created using Aspose.

Source to create document :

import java.awt.Color;
import java.util.List;
import java.util.Vector;

import com.aspose.words.BreakType;
import com.aspose.words.Document;
import com.aspose.words.DocumentBuilder;
import com.aspose.words.NumberStyle;
import com.aspose.words.ProtectionType;

public class CreateDoc
{
    public static void main(String[] args)
    {
        try
        {
            List list = new Vector();

            list.add("Item 1");
            list.add("Item 2");
            list.add("Item 3");
            list.add("Item 4");
            buildDocument(list);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    public static void buildDocument(List paras) throws Exception
    {
        Document doc = new Document();
        DocumentBuilder builder = new DocumentBuilder(doc);

        builder.startBookmark("start_doc");
        builder.getFont().setSize(12);
        builder.getFont().setBold(false);
        builder.getFont().setColor(Color.GRAY);
        builder.writeln("Do not delete this line");
        builder.getFont().clearFormatting();
        builder.endBookmark("start_doc");

        builder.insertBreak(BreakType.SECTION_BREAK_CONTINUOUS);
        builder.getListFormat().applyNumberDefault();
        com.aspose.words.List list = doc.getLists().get(0);

        int i = 0;

        if (paras != null && !paras.isEmpty())
        {
            for (String section: paras)
            {
                i++;

                builder.getFont().setSize(14);
                builder.getFont().setBold(true);
                builder.getFont().setColor(Color.BLACK);

                builder.getListFormat().listOutdent();
                builder.getListFormat().setList(list);
                builder.getListFormat().getList().getListLevels().get(0).getFont().setBold(true);
                builder.getListFormat().getList().getListLevels().get(0).getFont().setSize(14);
                builder.getListFormat().getList().getListLevels().get(0).getFont().setColor(Color.BLACK);
                builder.getListFormat().getList().getListLevels().get(0).setNumberStyle(NumberStyle.ARABIC);
                builder.getListFormat().getList().getListLevels().get(0).setNumberFormat("\u0000)");

                builder.startBookmark("sec_" + i);
                builder.writeln(section);
                builder.endBookmark("sec_" + i);

                builder.getFont().clearFormatting();

                builder.getListFormat().listOutdent();
                builder.getListFormat().removeNumbers();
            }

            builder.insertBreak(BreakType.LINE_BREAK);
            builder.startBookmark("end_doc");
            builder.getFont().setSize(12);
            builder.getFont().setBold(false);
            builder.getFont().setColor(Color.GRAY);
            builder.writeln("Do not delete this line");
            builder.getFont().clearFormatting();
            builder.endBookmark("end_doc");
            builder.insertBreak(BreakType.SECTION_BREAK_CONTINUOUS);
            builder.insertBreak(BreakType.PAGE_BREAK);
        }

        doc.setTrackRevisions(true);
        doc.protect(ProtectionType.ALLOW_ONLY_REVISIONS, "1234");

        doc.save("C:/result.doc");
    }
}

Thanks in advance.

Hi

Thanks for your inquiry. You can try using the following code:

public static void buildDocument(List <String> paras) throws Exception
{
    Document doc = new Document();
    DocumentBuilder builder = new DocumentBuilder(doc);
    // Create a style with a list asociated with it.
    Style listStyle = doc.getStyles().add(StyleType.PARAGRAPH, "myStyle");
    listStyle.getFont().setSize(14);
    listStyle.getFont().setBold(true);
    listStyle.getFont().setColor(Color.BLACK);
    // Create a list.
    com.aspose.words.List list = doc.getLists().add(ListTemplate.NUMBER_ARABIC_DOT);
    list.getListLevels().get(0).getFont().setBold(true);
    list.getListLevels().get(0).getFont().setSize(14);
    list.getListLevels().get(0).getFont().setColor(Color.BLACK);
    list.getListLevels().get(0).setNumberStyle(NumberStyle.ARABIC);
    list.getListLevels().get(0).setNumberFormat("\u0000)");
    // asociate a list with style.
    listStyle.getListFormat().setList(list);
    builder.startBookmark("start_doc");
    builder.getFont().setSize(12);
    builder.getFont().setBold(false);
    builder.getFont().setColor(Color.GRAY);
    builder.writeln("Do not delete this line");
    builder.getFont().clearFormatting();
    builder.endBookmark("start_doc");
    builder.insertBreak(BreakType.SECTION_BREAK_CONTINUOUS);
    int i = 0;
    if (paras != null && !paras.isEmpty())
    {
        for (String section: paras)
        {
            i++;
            builder.getParagraphFormat().setStyle(listStyle);
            builder.startBookmark("sec_" + i);
            builder.writeln(section);
            builder.endBookmark("sec_" + i);
            builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.NORMAL);
        }
        builder.insertBreak(BreakType.LINE_BREAK);
        builder.startBookmark("end_doc");
        builder.getFont().setSize(12);
        builder.getFont().setBold(false);
        builder.getFont().setColor(Color.GRAY);
        builder.writeln("Do not delete this line");
        builder.getFont().clearFormatting();
        builder.endBookmark("end_doc");
        builder.insertBreak(BreakType.SECTION_BREAK_CONTINUOUS);
        builder.insertBreak(BreakType.PAGE_BREAK);
    }
    doc.setTrackRevisions(true);
    doc.protect(ProtectionType.ALLOW_ONLY_REVISIONS, "1234");
    doc.save("C:\\Temp\\result.doc");
}

Hope this helps.
Best regards,

Hi Alexey,
I created two documents using the above code… but i am unable to import nodes in the destination file with its listing order. Please can you go through the file and let me know what went wrong.

Code for importing node

public class CompareDocs
{
    public static void main(String[] args)
    {
        try
        {
            Document doc1 = new Document("C:/srcDoc.doc");
            Document doc2 = new Document("C:/destDoc.doc");

            Bookmark srcdb = doc1.getRange().getBookmarks().get("section_402882592b33f9cf012b33fb494b0002");
            Node currNode = srcdb.getBookmarkStart().getParentNode();

            Bookmark destbk = doc2.getRange().getBookmarks().get("section_402882592b33a6af012b33a7cabd0005");
            Node destNode = destbk.getBookmarkStart().getParentNode();

            insertDocument(destNode, currNode);

            doc2.save("C:/result.doc");
        }
        catch (CloneNotSupportedException e)
        {
            e.printStackTrace();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

    }

    public static void insertDocument(Node insertAfterNode, Node srcDoc) throws Exception
    {
        // Make sure that the node is either a pargraph or table.
        if (insertAfterNode.getNodeType() != NodeType.PARAGRAPH &&
            insertAfterNode.getNodeType() != NodeType.TABLE)
        {
            throw new Exception("The destination node should be either a paragraph or table.");
        }

        // We will be inserting into the parent of the destination paragraph.
        CompositeNode dstStory = insertAfterNode.getParentNode();

        // This object will be translating styles and lists during the import.
        NodeImporter importer = new NodeImporter(srcDoc.getDocument(), insertAfterNode.getDocument(),
            ImportFormatMode.USE_DESTINATION_STYLES);

        Node newNode = importer.importNode(srcDoc, true);

        dstStory.insertAfter(newNode, insertAfterNode);
    }
}

Thanks and Regards,
Tarul

Hi

Thanks for your inquiry. This works fine with documents generate on my side, but does not work with your documents. Could you please create a simple code that will show me how you generate these documents? I will check the code and provide you more information.
Best regards,

Hi,

Please find attached src and ouput.doc

Thanks and Regards,
Tarul

Hi

Thank you for additional information. The problem occurs because you are still use builder.getListFormat().applyNumberDefault(); and builder.getListFormat().removeNumbers();. Please take a look at the code example I provided and note I do not use these methods. Instead, I apply numbering by specifying paragraph style.
Please let me know if you need more assistance, I will e glad to help you.
Best regards.

Hi,

I tried removing builder.getListFormat().applyNumberDefault(); and builder.getListFormat().removeNumbers(); but still gave me the same output. It seems there is a problem in using

com.aspose.words.List list = doc.getLists().get(0);
builder.getListFormat().setList(list);

in the method.
I have multilevel list elements. Please suggest how to handle it.

Please find attached documents and src DocumentUtil.txt – source createdSubDoc.doc – document created using src code subDoc.doc document from where list nodes are imported op.doc output document after importing list nodes

Thanks in advance.

Regards,
Tarul

Hi

Thanks for your inquiry. Please try creating a list and style from scratch, just as I suggested in my code example:

// Create a style with a list asociated with it.
Style listStyle = doc.getStyles().add(StyleType.PARAGRAPH, "myStyle");
listStyle.getFont().setSize(14);
listStyle.getFont().setBold(true);
listStyle.getFont().setColor(Color.BLACK);
// Create a list.
com.aspose.words.List list = doc.getLists().add(ListTemplate.NUMBER_ARABIC_DOT);
list.getListLevels().get(0).getFont().setBold(true);
list.getListLevels().get(0).getFont().setSize(14);
list.getListLevels().get(0).getFont().setColor(Color.BLACK);
list.getListLevels().get(0).setNumberStyle(NumberStyle.ARABIC);
list.getListLevels().get(0).setNumberFormat("\u0000)");
// asociate a list with style.
listStyle.getListFormat().setList(list);

Also in your code you still set list instead of setting style as I suggested. Here is line of your code:

builder.getListFormat().setList(list);

Here is what I suggested:

builder.getParagraphFormat().setStyle(listStyle);

Best regards,

Hi Alexey,

Thanks alot… its working

Warm Regards,
Tarul

Hi Tarul,

It is perfect that you managed to resolve the problem. Please feel free to ask in case of any issues, I will be glad to help you.
Best regards,

Hi Alexey,

I am trying to insert list items form MS Word Application in my document (Output.doc) which I have created using Aspose.
Now when I do not apply the style to the list (i.e. using list.getListLevels().get(0).setLinkedStyle(listStyleLevel1) and i press enter in the document after a list element the style formatting is lost.
Even if I apply the style to the list (i.e. using list.getListLevels().get(0).setLinkedStyle(listStyleLevel1) that is not properly applied. It loses its format in the level 2.
And if I apply it manually through MS Word as in the second document (Output_formatted.doc) the level is not identified properly in the document.

Can you please explain how to handle this. I have to provide inserting list element both through the document as well as from the code.

Please find attached code and document.

Thanks in advance.

Regards,
Tarul

And also the doucment is created in two parts and inserted after a node. Let me know if this may be a problem.

Thanks in advance.

Regards,
Tarul

Hi

Thank you for additional information. You do not need to apply LinkedStyle of list level. What I suggested is applying paragraph style. I simplified code to show you the techniques you should use. Here are steps you should follow:

  1. Create a list.
  2. Configure each level of the list is necessary.
  3. Create a paragraph style.
  4. Associate the created list with the style.
  5. Set the created style to the paragraph that should be a list item.
  6. If you need to increase level you should just call getListFormat().listOutdent();

Here is simple code that demonstrates the technique.

// Create a document and docuemnt builder.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Create a list.
com.aspose.words.List list = doc.getLists().add(ListTemplate.NUMBER_ARABIC_DOT);
// Configure list if necessary
// Configure first level.
list.getListLevels().get(0).setNumberStyle(NumberStyle.ARABIC);
list.getListLevels().get(0).setNumberFormat("\u0000.");
// Configure second level
list.getListLevels().get(1).setNumberStyle(NumberStyle.ARABIC);
list.getListLevels().get(1).setNumberFormat("\u0000.\u0001.");
// Configure next levels if neccessary
// ........................................
// Create a style with a list asociated with it.
Style listStyle = doc.getStyles().add(StyleType.PARAGRAPH, "myStyle");
// Asociate a list with style.
listStyle.getListFormat().setList(list);
// Apply the list to the current paragraph.
builder.getParagraphFormat().setStyle(listStyle);
builder.writeln("item 1");
// Incerease level.
builder.getListFormat().listIndent();
builder.writeln("item 1.1");
builder.writeln("item 1.2");
builder.writeln("item 1.3");
// Decrease level
builder.getListFormat().listOutdent();
builder.writeln("item 2");
// Disable numbering.
builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.NORMAL);
// Save output.
doc.save("C:\\Temp\\out.doc");

Hope this helps.
Best regards,

Hi Alexey,

As you would find in my code i have already applied the paragaraph formatting to the list.
I have different paragraph formatting for different levels.

The formatting is not maintained whn the document is being edited in MS Word application. Please suggest how to solve this.

Hi

Thanks for your request. You can do the same using multiple styles linked to the same list. Please see the following code example:

// Create a document and docuemnt builder.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Create a list.
com.aspose.words.List list = doc.getLists().add(ListTemplate.NUMBER_ARABIC_DOT);
// Configure list if necessary
// Configure first level.
list.getListLevels().get(0).setNumberStyle(NumberStyle.ARABIC);
list.getListLevels().get(0).setNumberFormat("\u0000.");
// Configure second level
list.getListLevels().get(1).setNumberStyle(NumberStyle.ARABIC);
list.getListLevels().get(1).setNumberFormat("\u0000.\u0001.");
// Configure next levels if neccessary
// ........................................
// First level styel.
// Create a style with a list asociated with it.
Style firstLevelStyel = doc.getStyles().add(StyleType.PARAGRAPH, "FirstLevel");
// Asociate a list with style.
firstLevelStyel.getListFormat().setList(list);
firstLevelStyel.getListFormat().setListLevelNumber(0);
firstLevelStyel.getFont().setSize(20);
// Second level styel.
Style secondLevelStyel = doc.getStyles().add(StyleType.PARAGRAPH, "SecondLevel");
secondLevelStyel.getListFormat().setList(list);
secondLevelStyel.getListFormat().setListLevelNumber(1);
// Apply the list to the current paragraph.
builder.getParagraphFormat().setStyle(firstLevelStyel);
builder.writeln("item 1");
// Incerease level.
builder.getParagraphFormat().setStyle(secondLevelStyel);
builder.writeln("item 1.1");
builder.writeln("item 1.2");
builder.writeln("item 1.3");
// Decrease level
builder.getParagraphFormat().setStyle(firstLevelStyel);
builder.writeln("item 2");
// Disable numbering.
builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.NORMAL);
// Save output.
doc.save("C:\\Temp\\out.doc");

Hope this helps.
Best regards,

Hey,
Thanks. The issue is resolved.

Regards,
Tarul