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

Free Support Forum - aspose.com

Merge content using bookmarks - font family/size issue

Hi Aspose team,

aspose_bookmarks.zip (5.0 MB)

In the attached zip you can find a sample code that should do the following:

  • for bookmark in template find bookmark with the same name in source
  • copy all nodes between BookmarkStart and End in source and import it to output document
  • Inline first and last paragraph with bookmark in template
  • remove bookmark and it’s content from template

All of the above is done, however the output document looks different than the expected one. There are two main points that are my concern:

  • all three merged paragraphs have different font from the ones used in both source and template. This font is coming from Normal style in template (I guess) and paragraphs in source use normal style as well, but with different formatting. The output is unchanged regardless of used ImportFormatMode in NodeImporter. In this case I’d like to have style copied from source and used on imported paragraphs.

  • The middle merged paragraph have also different font size. For paragraphs 1 and 3 I’m merging paragraphs child nodes one by one, and I’d expect the same font size when importing whole paragraph using isImportChildren = true.

Could you please point me in the right direction of how to achieve desired document output?

Thanks,
Mateusz

@acturisaspose

Thanks for your inquiry. In your case, we suggest you please use the following solution.

  1. Extract the bookmark’s content.
  2. Generate the document from extracted content.
  3. Move the cursor to the desired bookmark in destination document.
  4. Insert the document generated at step 2 into document using DocumentBuilder.InsertDocument method.

Hope this helps you.

Hi Tahir,

I’ve applied suggested solution to our case, but there are two problems with this solution:

  • There is additional space before merged content (there are 5 in total, while template has only 1 in this paragraph and source has 3). Looks like the space from before bookmark is doubled (looking at formatting).
  • Font is changed to TimesNewRoman - I’d like to keep the font family used in source.

Could you please where these differences come from and if you have a valid solution for both issues?
Please find the corrected solution attached (with all mentioned documents) so we are on the same page discussing this.
asposeBookmarks.zip (5.0 MB)

Thanks,
Mateusz

@acturisaspose

Thanks for your inquiry.

Please use the following code example to get the desired output. You can get the code of ExtractContent method from Github repository. We have attached the output document with this post for your kind reference. 18.12.zip (29.5 KB)

var template = new Document(MyDir + "POL21_ConfirmNewBusinessInsurer2.dotx", new Aspose.Words.LoadOptions() { AnnotationsAtBlockLevel = false });
var source = new Document(MyDir + "Bookmarks.docx", new Aspose.Words.LoadOptions() { AnnotationsAtBlockLevel = false });
var dstbookmark = template.FirstSection.Range.Bookmarks[0];

var bookmark = source.FirstSection.Range.Bookmarks[0];

ArrayList nodes =  Common.ExtractContent(bookmark.BookmarkStart, bookmark.BookmarkEnd, true);
Document dstDoc = Common.GenerateDocument(source, nodes);

DocumentBuilder builder = new DocumentBuilder(template);
builder.MoveToBookmark(dstbookmark.Name);
builder.InsertDocument(dstDoc, ImportFormatMode.KeepSourceFormatting);

template.Save(MyDir + "18.12.docx");

Please use the following GenerateDocument method to get the desired output.

public static Document GenerateDocument(Document srcDoc, ArrayList nodes)
{
    Document dstDoc = (Document)srcDoc.Clone(true);
    // Remove the first paragraph from the empty document.
    dstDoc.RemoveAllChildren();
    dstDoc.EnsureMinimum();
    dstDoc.FirstSection.Body.RemoveAllChildren();

    // Import each node from the list into the new document. Keep the original formatting of the node.
    NodeImporter importer = new NodeImporter(srcDoc, dstDoc, ImportFormatMode.KeepSourceFormatting);

    foreach (Node node in nodes)
    {
        Node importNode = importer.ImportNode(node, true);
        dstDoc.FirstSection.Body.AppendChild(importNode);
    }

    // Return the generated document.
    return dstDoc;
}

Hi Tahir,

many thanks for the code example, they work (almost) as I’d expect. The only issue still left is the font size - in the template you’ve attached seems that the font size is same as in source document, however in my case they are set to 12. The only difference is that I’m removing bookmark text before inserting document, and afterwards remove parent node of bookmark end. I’ve checked and font size on runs returned by GenerateDocument method you’ve published above is set to 12. Is there any way to set this to the value in source (11)?

Updated sample solution for your reference:
asposeImportFont.zip (5.2 MB)

Thanks,
Mateusz

@acturisaspose

Thanks for your inquiry. We have tested the scenario using the latest version of Aspose.Words for .NET 18.12 and have not found the shared issue. Please use Aspose.Words for .NET 18.12. We have attached the output document with this post for your kind reference. 18.12.zip (29.5 KB)

Hi Tahir,

please note that in the solution attached in my last post I’m actually using 18.12. Code inside common class is the same as in linked github repo and the rest of the code is from your previous post, excluding template bookmark removing part.

Could you please open my solution and point out what do I do incorrectly so fonts in imported documents differs? To spot this just please check output of

dstDoc = Common.GenerateDocument(source, nodes);

First run (and all others for all 3 paragraphs have font set to 12.
Thanks for your support,
Mateusz

@acturisaspose

Thanks for your inquiry. Please use the modified GenerateDocument method shared in this post to get the desired output.

Hi Tahir,

using this method this works great.
Many thanks for your support!

Best regards,
Mateusz