Undesired renaming of bookmarks in building block while importing building blocks from one document to another

Hello,

As explained in the title, I need to copy Building Blocks from one document to another. I need a “perfect” copy including bookmarks names.
Unfortunately, sometimes (but not always), Aspose.Word adds a leading “_0” or “1" or "” to bookmarks names.
I already had raised this ticket and I was wondering if it’s related, but it seems not to be related. I tested with 22.11, 22.12 and 23.1 and I’m getting the same behavior.

Here is a xunit test to reproduce (no assertions, just text output) :

using System;
using System.Collections.Generic;
using System.Linq;
using Aspose.Words;
using Aspose.Words.BuildingBlocks;
using Xunit;
using Xunit.Abstractions;

public class BookmarksInGlossaryIssue
{
    [Fact]
    void Test()
    {
        var source = new Document(@"c:\temp\demo.dotx");
        var destination = new Document();
        destination.GlossaryDocument ??= new GlossaryDocument();

        var importer = new NodeImporter(
            source.GlossaryDocument,
            destination.GlossaryDocument,
            ImportFormatMode.UseDestinationStyles);

        foreach (var b in source.GlossaryDocument.OfType<BuildingBlock>())
            destination.GlossaryDocument.AppendChild(importer.ImportNode(b, true));

        Display("Source", source);
        Display("Destination", destination);
    }

    private void Display(string name, Document document)
    {
        _testOutputHelper.WriteLine(name);

        _testOutputHelper.WriteLine("\tBookmarks");
        Bookmarks(document)
            .DefaultIfEmpty("Aucun")
            .ToList()
            .ForEach(x => _testOutputHelper.WriteLine($"\t\t{x}"));

        _testOutputHelper.WriteLine("\tQuickParts");
        Glossary(document)
            .DefaultIfEmpty((Name: "Aucun", Bookmarks: Enumerable.Empty<string>()))
            .ToList()
            .ForEach(x
                => _testOutputHelper.WriteLine(
                    $"\t\t{x.Name}{x.Bookmarks.Select(b => $"\n\t\t\tBookmark:\t{b}").Aggregate("", string.Concat)}"));
    }

    private static IEnumerable<(string Name, IEnumerable<string> Bookmarks)> Glossary(Document d)
        => d.GlossaryDocument
               ?.OfType<BuildingBlock>()
               .Where(x => x.Name.StartsWith("A26", StringComparison.InvariantCultureIgnoreCase))
               .Select(x => (
                       Nom: x.Name,
                       Bookmarks: x
                           .Range
                           .Bookmarks
                           .Select(b => b.Name)
                   )
               )
               .OrderBy(x => x.Nom)
           ?? Enumerable.Empty<(string Nom, IEnumerable<string> Bookmarks)>();

    private static List<string> Bookmarks(Document d)
        => d.Range
            .Bookmarks
            .Select(x => x.Name)
            .OrderBy(x => x)
            .ToList();

    private readonly ITestOutputHelper _testOutputHelper;

    public BookmarksInGlossaryIssue(ITestOutputHelper testOutputHelper) =>
        _testOutputHelper = testOutputHelper;
}

Here is the output I’m getting

Source
	Bookmarks
		_GoBack
		A26SAgendapasses01
		A26SAgendapasses01A26NParticipant01
	QuickParts
		A26Insertions001
			Bookmark:	A26SAgendapasses01A26NParticipant01
		A26SAgendapasses01
			Bookmark:	A26SAgendapasses01
			Bookmark:	A26SAgendapasses01A26NParticipant01
Destination
	Bookmarks
		Aucun
	QuickParts
		A26Insertions001
			Bookmark:	A26SAgendapasses01A26NParticipant01_0
		A26SAgendapasses01
			Bookmark:	A26SAgendapasses01
			Bookmark:	A26SAgendapasses01A26NParticipant01

You can notice a leading “_0” has been added to A26SAgendapasses01A26NParticipant01 in the destination document.

Here is the source document to reproduce the issue:
demo.zip (35.0 KB)

Thanks for your help

@gilles.figuiere.pro the issue happen because the GlosaryDocument document of your test file have bookmarks with the same name, you can check it easily running the following code:

var source = new Document(@"c:\temp\demo.dotx");
Console.WriteLine("=============Source GlosaryDocument Bookmarks===============");
foreach (Bookmark bk in source.GlossaryDocument.Range.Bookmarks)
   Console.WriteLine(bk.Name);
Console.WriteLine("=============Source Document Bookmarks===============");
foreach (Bookmark bk in source.Range.Bookmarks)
   Console.WriteLine(bk.Name);

And MS Word document cannot contain bookmarks with duplicated names. So after appending documents the duplicated bookmarks are renamed - _0 suffix is added.
We are working to get a workaround for you.

Hi @Eduardo_Canal, thanks for your answer :slight_smile:

I understand it, however I don’t believe MS Word doesn’t support having duplicated bookmarks in the main document and the glossary document. It is true that MS Word doesn’t support having duplicated bookmarks in the main document OR inside each “building blocks” of the glossary. We have plenty of documents (dotx templates), generated with MS Word, having the SAME bookmarks in the main document and insides building blocks but UNIQUE in each scope (main, building block 1, 2, …N).

I personally created one dotx with one bookmark in the main document and one building block based on the main document. After saving I can see the bookmark exists with the same name in the main document and the building block (see attached file) without any renaming.
demo2.zip (17.2 KB)

In our process, we have a post process software (after Aspose) that relies on bookmarks names. This renaming leads to issues during this post process.

Thanks for your help.

We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): WORDSNET-24897

You can obtain Paid Support services if you need support on a priority basis, along with the direct access to our Paid Support management team.

1 Like

Hi @alexey.noskov, I see the issue is now at state “Analysis Complete” what is the next step? Can we expect a fix?
Thanks

@gilles.figuiere.pro Yes, we have completed analyzing the issue. But the issue is not yet scheduled for development. We will keep you posted and let you know once the issue is resolved or we have more information for you.

1 Like

Hi @alexey.noskov, I saw the issue status changed to “planned”, does that means it will be fixed ? Can we hope it will be fixed and published in April? Thanks

@gilles.figuiere.pro The issue is currently scheduled for development in 23.5 (May 2023) version of Aspose.Words. So, I am afraid, the issue most likely will not be fixed in the upcoming 23.4 release.

1 Like

The issues you have found earlier (filed as WORDSNET-24897) have been fixed in this Aspose.Words for .NET 23.4 update also available on NuGet.

Hi Alexey I’m glad to see the fix has been published :slight_smile: unfortunately I can’t test it because I’m getting a System.InvalidOperationException
The subscription included in this license allows free upgrades until 11 Jan 2023, but this version of the product was released on 01 Apr 2023. Please renew the subscription or use a previous version of the product.
Can you do something?

@gilles.figuiere.pro You can request a free 30-days temporary license to test the latest version on your side.