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