We are using the Java API to merge Word documents using the insertDocument method. We are noticing that when certain fonts are used within a shape, the font is converted to Times New Roman in the document output. I have provided a sample document with the before and after to highlight this behavior. Notice that the “Arial Black” and “Arial Narrow” fonts are preserved in the shapes but the “Arial” font is not. The “Arial” font appears to be retained when it lives outside of a shape. Any thoughts on what is causing this and ways to resolve?
Arial Font Test - After Aspose.docx (13.8 KB)
Arial Font Test - Before Aspose.docx (39.1 KB)
The example simplified code is as follows:
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.insertDocument(sourceInputDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING)
@shawn.bittinger
When using the Java API to merge Word documents with the insertDocument
method, it is possible to encounter issues with font preservation, particularly when dealing with shapes. In your case, it seems that the “Arial” font is being replaced with “Times New Roman” when it is used within a shape, while other fonts like “Arial Black” and “Arial Narrow” are preserved.
This behavior may be related to how Aspose.Words handles font styles during the merging process. When merging documents, the library attempts to maintain the formatting of the source document, but conflicts can arise if the destination document has different styles or if the font is not available in the destination environment.
To address this issue, you can try the following approaches:
-
Check Font Availability: Ensure that the “Arial” font is installed and available in the environment where the document is being processed. If the font is not available, Aspose.Words may substitute it with a default font.
-
Use ImportFormatOptions: When calling the insertDocument
method, you can specify ImportFormatOptions
to control how styles are handled. For example, you can set the KeepSourceNumbering
option to manage list styles and potentially avoid conflicts with font styles.
-
SmartStyleBehavior: Enable the SmartStyleBehavior
option in ImportFormatOptions
. This can help resolve style clashes by converting source document styles with the same names as destination styles into direct paragraph attributes, which may help preserve the intended formatting.
Here is a simplified code example demonstrating how to use ImportFormatOptions
with insertDocument
:
Document dstDoc = new Document();
DocumentBuilder builder = new DocumentBuilder(dstDoc);
// Load the source document
Document srcDoc = new Document("source.docx");
// Set import format options
ImportFormatOptions options = new ImportFormatOptions();
options.setSmartStyleBehavior(true); // Enable smart style behavior
// Insert the document while preserving formatting
builder.insertDocument(srcDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING, options);
// Save the merged document
dstDoc.save("merged.docx");
By applying these strategies, you may be able to resolve the font substitution issue when merging documents with Aspose.Words. If the problem persists, consider reaching out to Aspose support for further assistance, providing them with specific details about the documents and the fonts involved.
@shawn.bittinger
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-27867
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.
@shawn.bittinger The problematic shape has no direct font specified for text. Then font is taken from the Normal
style. But this is different in source and destination documents. The output in Aspose.Words here is the same as in MS Word. As we mimic MS Word, this issue is Not a Bug
.
Please use our Merger
feature to achieve desired result:
Document src = new Document("C:\\Temp\\in.docx");
Document doc = new Document();
Document merged = Merger.merge(new Document[] { doc, src }, MergeFormatMode.KEEP_SOURCE_LAYOUT);
merged.save("C:\\Temp\\out.docx");
Thanks for the comment. I agree the behavior that I described in the initial post is what Word is doing when data is copied and pasted between the two documents with different fonts for the Normal Style.
1 Like
The issues you have found earlier (filed as WORDSNET-27867) have been fixed in this Aspose.Words for Java 25.3 update.