Free Support Forum - aspose.com

Numerotation problem when coping content of a document into another

Hi,
When I copy the content of one document into another, the paragraph numerotation is messed up.
To locate where we want to insert the document, we use this expression

<%CLAUSE CLAUSE_AMIANTE%>

with CLAUSE_AMIANTE the name of the doc to insert.
Since I’m not sure whether the problem comes from the code we use or Aspose.Words, I attached with the source and generated doc the code we use.
Thank you.

Hi,

Please try the improved version of the InsertDocument method which supports correct insertion of list items. It was published on the forums before, here's its Java version:

/**
* Inserts content of the external document after the specified node.
*
* @param node Node in the destination document where the external document content should be inserted.
* @param doc Document to insert.
*/
public void insertDocument(Node node, Document doc) throws Exception
{
Document dstDoc = node.getDocument();
Section insertedSection;
int index = node.getParentNode().getChildNodes().indexOf(node);
for(Object sect : doc.getSections())
{
Section section = (Section) sect;
insertedSection = (Section) dstDoc.importNode(section, true, ImportFormatMode.KEEP_SOURCE_FORMATTING);
for(Object inserted : insertedSection.getBody().getChildNodes())
{
Node insertedNode = (Node) inserted;
// Only Paragraph or Table nodes can be inserted into Cell or Shape
if (insertedNode instanceof Paragraph || insertedNode instanceof Table)
{
// Do not insert node if it is a last empty paragarph in the section.
if (insertedNode instanceof Paragraph &&
insertedNode == section.getBody().getLastChild() &&
"".equals(insertedNode.getText()))
break;

node.getParentNode().getParentNode().getChildNodes().insert(++index, insertedNode.deepClone(true));
}
}
}
}

Hi,

Thank you for your answer.
It now works except when some elements of
the list items are seperated by a section break. In this case, the
numerotation is restarted. See the attached result file.

Although I cannot find anything attached, I understand the problem. Basically, it requires some code improvements on our side, so I have logged this as issue #1273. Meanwhile you can use the following workaround. Process the source document with the following procedure before insertion:

DocumentBuilder builder = new DocumentBuilder(srcDoc);
for (int i = 0; i < srcDoc.getSections().getCount() - 1; i++)
{
Section section = srcDoc.getSections().get(0);
Section nextSection = srcDoc.getSections().get(1);
if (nextSection.getPageSetup().getSectionStart() == SectionStart.NEW_PAGE)
{
builder.moveTo(section.getBody().getLastChild());
builder.insertBreak(BreakType.PAGE_BREAK);
}
section.appendContent(nextSection);
nextSection.remove();
}
This code removes all section breaks from the document and replaces "new page" section breaks with page breaks. This should work until we improve ImportNode.

Thank you for this workaround but we cannot use it since we actually
need to insert the document with its section breaks. Is there a way to
renumber the list items once they are inserted?
I also attached the files for you to see the problem but you got it.

I'm afraid renumbering won't be an applicable solution here. The point is we don't know the actual numbers and counting them "manually" to set up the remainder of the list would be too complex task to just properly insert a document. The reason why the numbering resets is that the list formatting is created anew each time one imports a paragraph into a document. That's the current behaviour; we are planning to make it possible to let ImportNode know about previous importing context so that it might resume list numbering if needed.

I think you should wait until we improve the importing of nodes. We'll try to do that ASAP.

do you have a time estimate of when this function will be fixed?

Next week probably.

To fix the problems with loosing list numbering context when inserting one document to another the new NodeImporter class was introduced in Aspose.Words 4.0. Here is an example of InsertDocument procedure which uses this new class:

///

/// Inserts content of the external document after the specified node.

/// NodeImporter is used to maintain import context for each inserted node.

/// That is useful when importing documnet with lists for example.

///

/// Node in the destination document where the external document content should be inserted.

/// Document to insert.

public void InsertDocumentWithImporter(Node node, Document doc)

{

CompositeNode parentNode = node.ParentNode;

while (true)

{

if (parentNode == null)

throw new Exception("Document cannot be inserted after the specified node.");

if (parentNode is Story || parentNode is Cell || parentNode is Shape)

break;

node = parentNode;

parentNode = node.ParentNode;

}

int index = node.ParentNode.ChildNodes.IndexOf(node);

Document dstDoc = node.Document;

Section insertedSection;

NodeImporter nodeImporter = new NodeImporter(doc, dstDoc, ImportFormatMode.KeepSourceFormatting);

foreach (Section section in doc.Sections)

{

insertedSection = (Section)nodeImporter.ImportNode(section, true);

foreach (Node insertedNode in insertedSection.Body.ChildNodes)

{

// Do not insert node if it is a last empty paragarph in the section.

if (insertedNode is Paragraph && insertedNode == section.Body.LastChild && insertedNode.ToTxt().Equals(string.Empty))

break;

parentNode.ChildNodes.Insert(++index, insertedNode.Clone(true));

}

}

}

Please try it and let me know if it solves your current problems with inserting documents with lists.

Best regards,

Thank you for your answer but we just cannot use it since we are using the JAVA version of Aspose.Words. When will this new NodeImporter class be available for the Java version?

It will be available in the forthcoming release that will be published in 1-2 days.