I am using the DocumentBuilder insert template into another document. The problem that I have is that when insert a template into the target template, it does not use the target font/size.
Here is the code that I am current using:
using var stream = new MemoryStream(content);
var builder = new DocumentBuilder((Document)args.MatchNode.Document);
var node = args.MatchNode.ParentNode;
builder.MoveTo(node);
builder.InsertDocument(new Document(stream),ImportFormatMode.UseDestinationStyles);
Any help would be great.
My version version is: 23.8.0
@pphillipsbiz
To ensure that the inserted document uses the target document’s styles, you can modify your approach by using different ImportFormatMode
options. The ImportFormatMode.UseDestinationStyles
option applies the destination document’s styles to the inserted content, which can lead to unexpected formatting if the styles differ.
Instead, consider using either ImportFormatMode.KeepDifferentStyles
or ImportFormatMode.KeepSourceFormatting
. The first option retains the styles from the source document while allowing for some differences, and the second option keeps the original formatting from the source document entirely.
Here’s how you can adjust your code:
using var stream = new MemoryStream(content);
var builder = new DocumentBuilder((Document)args.MatchNode.Document);
var node = args.MatchNode.ParentNode;
builder.MoveTo(node);
// Use KeepSourceFormatting to retain the original styles from the source document
builder.InsertDocument(new Document(stream), ImportFormatMode.KeepSourceFormatting);
If you want to maintain the destination document’s styles while still allowing for some formatting from the source, you can use ImportFormatMode.KeepDifferentStyles
:
builder.InsertDocument(new Document(stream), ImportFormatMode.KeepDifferentStyles);
This should help you achieve the desired formatting in your final document. If you continue to experience issues, consider reviewing the styles in both documents to ensure they are compatible.
For further details, you might want to check the related discussions on style issues when inserting documents, as they can provide additional insights and solutions.
Sources:
[1]: Destination Document Styles Issue using Insert Document Object
However this does not work, I already have set ImportFormMode to UseDestinationStyles
@pphillipsbiz Could you please attach your input documents here for testing? Most likely formatting in your source document is not set using styles. When you use ImportFormatMode.UseDestinationStyles
, Aspose.Words uses the destination document styles and copy new styles. So if formatting in your source document is applied explicitly or using styles that are not available in the destination document, formatting of the source document will be kept.
I have attached a working solution.
AsposeWordImportKeepStyles.zip (132.4 KB)
@pphillipsbiz You can achieve what you need by inserting pure text from the sub document:
public class InsertDocumentAtReplaceHandler : IReplacingCallback
{
ReplaceAction IReplacingCallback.Replacing(ReplacingArgs args)
{
DocumentBuilder builder = new DocumentBuilder((Document)args.MatchNode.Document);
Document subDoc = new Document(@"C:\Temp\Import.docx");
// Insert a document's text into the paragraph, containing the match text.
Paragraph para = (Paragraph)args.MatchNode.ParentNode;
para.RemoveAllChildren();
builder.MoveTo(para);
// Insert only text.
builder.Write(subDoc.ToString(SaveFormat.Text).Trim());
return ReplaceAction.Skip;
}
}