We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Clone method keeps the reference on original document

Hi Aspose,

The issue is appeared in 18.6 version.

Document clone method doesn’t work correctly.
Issue: the cloned document is linked with the original document.

Source code:

var document = new Document();`

    			var builder = new DocumentBuilder(document);

    			for (var i = 0; i < 10; i++)
    			{
    				builder.StartBookmark(i.ToString());
    				builder.Write(i.ToString());
    				builder.EndBookmark(i.ToString());
    				builder.Writeln();
    			}

    			builder.Writeln();
    			builder.Writeln("Bookmarks count | original: " + document.Range.Bookmarks.Count);

    			var count = document.Range.Bookmarks.Count;

    			var documentClone = document.Clone();
    			documentClone.Range.Bookmarks.Clear();

    			builder.Writeln("Bookmarks count | original + clearing the clone: " + document.Range.Bookmarks.Count);

    			var builderClone = new DocumentBuilder(documentClone);
    			for (var i = 0; i < 10; i++)
    			{
    				builderClone.StartBookmark(i.ToString());
    				builderClone.Write(i.ToString());
    				builderClone.EndBookmark(i.ToString());
    				builderClone.Writeln();
    			}

    			builder.Write("Bookmarks count | clone: " + documentClone.Range.Bookmarks.Count);

    			document.Save("C:\\test.docx", SaveFormat.Docx);

Result:

Bookmarks count | original: 10
Bookmarks count | original + clearing the clone: 0
Bookmarks count | clone: 20

Thanks.

@spectr100,

Thanks for your inquiry. Please call Document.UpdatePageLayout method as shown below to get the desired output.

var document = new Document();

var builder = new DocumentBuilder(document);

for (var i = 0; i < 10; i++)
{
    builder.StartBookmark(i.ToString());
    builder.Write(i.ToString());
    builder.EndBookmark(i.ToString());
    builder.Writeln();
}

builder.Writeln();
builder.Writeln("Bookmarks count | original: " + document.Range.Bookmarks.Count);

var count = document.Range.Bookmarks.Count;

document.UpdatePageLayout();

var documentClone = document.Clone();
documentClone.Range.Bookmarks.Clear();

builder.Writeln("Bookmarks count | original + clearing the clone: " + document.Range.Bookmarks.Count);

var builderClone = new DocumentBuilder(documentClone);
for (var i = 0; i < 10; i++)
{
    builderClone.StartBookmark(i.ToString());
    builderClone.Write(i.ToString());
    builderClone.EndBookmark(i.ToString());
    builderClone.Writeln();
}

documentClone.UpdatePageLayout();
builder.Write("Bookmarks count | clone: " + documentClone.Range.Bookmarks.Count);

document.Save(MyDir + "18.6.docx", SaveFormat.Docx);

Hi Tahir,

This workaround doesn’t work:

Bookmarks count | original: 10
Bookmarks count | original + clearing the clone: 0
Bookmarks count | clone: 10

Expected result:

Bookmarks count | original: 10
Bookmarks count | original + clearing the clone: 10
Bookmarks count | clone: 10

Also on the real project UpdatePageLayout() doesn’t help at all.

18.5 version works fine.

@spectr100,

Thanks for your inquiry. We have tested the scenario and have managed to reproduce the same issue at our side. For the sake of correction, we have logged this problem in our issue tracking system as WORDSNET-17030. You will be notified via this forum thread once this issue is resolved.

We apologize for your inconvenience.

The issues you have found earlier (filed as WORDSNET-17030) have been fixed in this Aspose.Words for .NET 18.7 update and this Aspose.Words for Java 18.7 update.