Free Support Forum - aspose.com

Problem updating style to destination styles when using importformat.usedestinationstyles

I have two methods as below
void IFieldMergingCallback.FieldMerging(FieldMergingArgs e)
{
switch (e.FieldName)
{
case “Description”:

if (!(e.FieldValue == null || e.FieldValue == DBNull.Value || e.FieldValue.ToString() == String.Empty))
{
mbuilder.MoveToMergeField(e.FieldName, false, false);
MemoryStream stream = new MemoryStream(System.Text.Encoding.ASCII.GetBytes(e.FieldValue.ToString()));
LoadOptions options = new LoadOptions(LoadFormat.Rtf, null, null);
Document rtf = new Document(stream, options);
Paragraph pa = mbuilder.CurrentParagraph;
Style style = null;

style = mbuilder.Document.Styles[e.TableName];
InsertDocument(pa, rtf.FirstSection, ImportFormatMode.UseDestinationStyles, style, false);
mbuilder.MoveToMergeField(e.FieldName);

}
}


and
static void InsertDocument(Node insertAfterNode, Section srcSection, ImportFormatMode importMode, Style style, bool insertBeforeNodeOption)
{

// We will be inserting into the parent of the destination paragraph.
int i = 0;

CompositeNode dstStory = insertAfterNode.ParentNode;

// This object will be translating styles and lists during the import.
srcSection.Document.Styles[“Normal”].Font.Name = insertAfterNode.Document.Styles[“Normal”].Font.Name;

NodeImporter importer = new NodeImporter(srcSection.Document, insertAfterNode.Document, importMode);


// Loop through all sections in the source document.

//foreach (Section src in srcSection)
//{
// //src.
//}
{

// Loop through all block level nodes (paragraphs and tables) in the body of the section.

foreach (Node srcNode in srcSection.Body)
{

// Let’s skip the node if it is a last empty paragraph in a section.

if (srcNode.NodeType.Equals(NodeType.Paragraph))
{

Paragraph para = (Paragraph)srcNode;
//if (style != null)
// para.ParagraphFormat.Style = style;

if (para.IsEndOfSection && !para.HasChildNodes)

continue;

}
//foreach (Style styl in srcNode.Document.Styles)
//{
// if (style!= null)
// {
// styl.Font.Name = “Calibri”;
// }
//}
// This creates a clone of the node, suitable for insertion into the destination document.

Node newNode = importer.ImportNode(srcNode, true);

if (style != null)
{
if (newNode.NodeType.Equals(NodeType.Paragraph))
{
Paragraph pp = (Paragraph)newNode;
pp.ParagraphFormat.Style = style;

}
}

// Insert new node after the reference node.
//dstStory.AppendChild(newNode);
if (insertBeforeNodeOption && i == 0)
{
dstStory.InsertBefore(newNode, insertAfterNode);
i++;
}
else
dstStory.InsertAfter(newNode, insertAfterNode);

insertAfterNode = newNode;

}

}

}


I have set my destination document to arial and works for most except for the rtf data it keeps the format of the rtf but i have used UseDestinationStyles. When I look in the style that am passing i see the paragraFormat has layers of styles and the first layer show my font set to arial and the subsequent layers have a different font calibri , and I would like to know how these layer fonts are set
I.e ParagraphFormat.Style.Font.StyleName is Arial but
ParagraphFormat.Style.Font.Style.Font.StyleName is calibri
ParagraphFormat.Style.Font.Style.Style.Font.Style.Font is calibri

Any help will be greatly appreciated

Hi Phillip,

Thanks for your inquiry. Could you please attach your input Word documents (.doc file and rtf string) here for testing? I will investigate the issue on my side and provide you more information.

Best regards,

Thanks for the response , I have attached the two files u requested

Hi Phillip,

Thanks for the additional information. We are checking with this scenario and will get back to you as soon as possible.

Best regards,

Hi,


Thanks for your patience. I am afraid, I could not see any issue with the output document; could you please clarify where the issue is? I have attached a sample output document that is produced on my side using the latest version of Aspose.Words (13.3.0) here for your reference.

PS: In case you’re using an older version of Aspose.Words, I would suggest you please upgrade to Aspose.Words 13.3.0 from here. I hope, this helps.

Best regards,

Hi

Thanks for the response
The issue lies in adapting the document styles , the rtf being passed is not adapting to the document style for example the output document you have attached still shows the rtf format on the document instead of using the document styles it comes out
calibri 10 , instead of calibri 11 which is the document font size
The text color in the original document is normal and the one that is shown on the description comes from the rtf is different but the output is showing the rtf styling .

So the issue here is how do I make the descriptions to adapt to the output document style am using importFormat.Usedocumentstyles. but it still retains the styling within the rtf.

Regards



Hi,


Thanks for the additional information.

If you want RTF data be inserted using the formatting of the destination style (i.e the formatting of the merge field) or as plain text without any formatting then please try adding the following lines in your code:

void IFieldMergingCallback.FieldMerging(FieldMergingArgs e)

{

switch (e.FieldName)

{

case "Description":

if (!(e.FieldValue == null || e.FieldValue == DBNull.Value || e.FieldValue.ToString() == String.Empty))

{

mbuilder.MoveToMergeField(e.FieldName, false, false);

MemoryStream stream = new MemoryStream(System.Text.Encoding.ASCII.GetBytes(e.FieldValue.ToString()));

LoadOptions options = new LoadOptions(LoadFormat.Rtf, null, null);

Document rtf = new Document(stream, options);

foreach (Run r in rtf.GetChildNodes(NodeType.Run, true))

r.Font.ClearFormatting();

Paragraph pa = mbuilder.CurrentParagraph;

Style style = null;

style = mbuilder.Document.Styles[e.TableName];

InsertDocument(pa, rtf.FirstSection, ImportFormatMode.UseDestinationStyles, style, false);

mbuilder.MoveToMergeField(e.FieldName);

}

}

}


I hope, this helps.

Best regards,

Hi


That helps . Thank you very much