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

Free Support Forum - aspose.com

Styles copied and not displaying from .dot template

Hi,


I have a template.dot file and a body.rtf file. The template.dot has my style definitions in it but no content (on purpose - it should be just styles):

OSFT-BodyText = Calibri, 10pt
OSFT-BodyTextBold = Calibri, 10pt Bold

The body.rtf (modelling a mail merge template document, and therefore has content) has the same style names, but different settings (these are base settings, certain customers have a different.dot file to change the styles of the base documents).

OSFT-BodyText = Arial, 11pt
OSFT-BodyTextBold = Arial, 11pt Bold

I expect the output document to have overridden the arial 11 with calibri 10, however the text is still arial 11! Also, when looking at the style manager in word, the style itself is set to calibri 10!!!

What am I doing wrong please? code below (I have enclosed screenshots of the word styles etc too). Dll version is Aspose.Words 14.1

Document doc = null;
Document template = new Document(“template.dot”);
Document body = new Document(“body.rtf”);

template.RemoveAllChildren();

template.AppendDocument( body, ImportFormatMode.UseDestinationStyles );

doc = template;

doc.Save( “outputStyles.rtf”, SaveFormat.Rtf);


Thanks

Chris

Hi Chris,


Thanks for your inquiry. The problem occurs because text in your body.rtf is formatted with direct formatting using the Font properties. Please try executing the following code to fix this issue:

Document doc = null;

Document template = new Document(MyDir + @“template.dot”);<o:p></o:p>

Document body = new Document(MyDir + @"body.rtf");

template.RemoveAllChildren();

template.AppendDocument(body, ImportFormatMode.UseDestinationStyles);

doc = template;

foreach (Run run in doc.GetChildNodes(NodeType.Run, true))

run.Font.ClearFormatting();

doc.Save(MyDir + @“out.rtf”, SaveFormat.Rtf);


I hope, this helps.

Best regards,

Thanks Awais,


That works!!

Could I ask, is it possible to not have the direct formatting with RTF? is it my source document that means I need to use this approach, or is it just not possible to have styles and no direct formatting with RTF? Basically, that code looks expensive in runtime for large documents…

Also, is just styles and no direct formatting possible with doc and docx formats?

Many Thanks

Chris

Just looking at this, I think that RTF and Aspose should support non-direct formatting:


http://www.aspose.com/docs/display/wordsjava/Style+Features+Supported+on+Rich+Text+Import

Am I doing something wrong here? how do I make sure the direct formatting is removed using word, and that I import the RTF into Aspose in a way that this is preserved please?

I expect to be able to load in an RTF with no direct formatting to use sytles within that RTF, and then save the RTF without any direct formatting (to also use the styles).

Many Thanks

Chris

Hi Chris,


Thanks for your inquiry. Formatting is applied on a few different levels. For example, let’s consider formatting of simple text. Text in documents is represented by Run element and a Run can only be a child of a Paragraph. You can apply formatting 1) to Run nodes by using ‘character Styles’ e.g. a Glyph Style, 2) to the parent of those Run nodes i.e. a Paragraph node (possibly via ‘paragraph Styles’) and 3) you can also apply direct formatting to Run nodes by using Run attributes (Font). In this case the Run will inherit formatting of Paragraph Style, a Glyph Style and then direct formatting. If you only want to keep formatting specified via Styles then you need to remove direct formatting by executing the code from my previous post.

Best regards,

Thanks for that Awais,

That’s a good description and makes sense to me. Is there a way to make sure that my incoming document does not have the lower down formatting applied, i.e. it’s just the paragraph format that looks at a style, or should I always run your code regardless?

I wasn’t sure what happens on the loading of any document type by Aspose, i.e. is it always necessary to run that code to remove direct formatting regardless, when loading a document?

Thanks

Chris

Hi Chris,


Thanks for your inquiry. It seems that you will need to always execute this code; however, you can slightly improve performance by executing this code only for the Run nodes of source document. Please see the following change:

Document doc = null;

Document template = new Document(MyDir + @"template.dot");

Document body = new Document(MyDir + @"body.rtf");

foreach (Run run in body.GetChildNodes(NodeType.Run, true))

run.Font.ClearFormatting();

template.RemoveAllChildren();

template.AppendDocument(body, ImportFormatMode.UseDestinationStyles);

doc = template;

doc.Save(MyDir + @“output.rtf”, SaveFormat.Rtf);


I hope, this helps.

Best regards,

Hi Awais,

That’s great thanks for clarifying.

Can I ask one further point please. Is this an Aspose limitation with RTF handling or is it part of the RTF specification?

I don’t mind either way as there is a suitable solution, however I would like to know!!

Also, does this affect other formats such as doc and docx? I ask, because I could always switch my source documents to this format…

Thanks

Chris

Hi Chris,


Thanks for your inquiry. For the sake of any correction, I have logged this problem in our issue tracking system as WORDSNET-10532. Our development team will further look into the details of this problem and we will keep you updated on the status of correction. We apologize for your inconvenience.

Best regards,

Hi Chris,


Thanks for being patient. Regarding WORDSNET-10532, our development team has completed the analysis of this issue and has come to a conclusion that they won’t be able to implement the fix to your issue. Your issue (WORDSNET-10532) will be closed with ‘Won’t Fix’ resolution.

This is part of RTF specification i.e. each run which contains style must have direct attributes (from this style). This issue is for RTF format only. Please use other formats such as DOCX, if you can switch source documents to other formats.

Best regards,