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

Free Support Forum - aspose.com

Problem setting paragraph style to custom style

I am having difficulty changing the style of paragraphs in a document to a custom style.



I have attached the project, which shows the particular issue. I am expecting the fonts to be changed to match the fonts as configured in “Create style” through MS Word. It works fine when selecting the style in Word itself.



The code I am running at the minute is paragraph.StyleName = “StyleName”, which sets the colour, justification, etc. properly - the same as when I run the below



var style = doc.Styles[“StyleName”];

paragraph.Style = style;



However, the only way I seem to be able to set the font style I want (expect!), i.e size, bold, italic, underline, etc., is by getting a list of the runs from the paragraph and looping over them setting .Font.Size, .Font.Bold, .Font.Italic, etc., to match that of the in style.Font - which is a bit much! Setting the paragraph style should pull the font settings from the style and set them appropriately… As is done in MS Word.



Am I doing it right or is this a bug? If so, is there an easier workaround?

Hi Rob,

Thanks for your inquiry. We have tested the scenario and have managed to reproduce the same issue at our side. For the sake of correction, we have logged this problem in our issue tracking system as WORDSNET-13395. You will be notified via this forum thread once this issue is resolved.

We apologize for your inconvenience.
Hi Rob,

Thanks for your patience. It is to inform you that our product team has completed the work on issue (WORDSNET-13395) and has come to a conclusion that this issue and the undesired behavior you're observing is actually not a bug in Aspose.Words. So, we have closed this issue as 'Not a Bug'.

The paragraphs contains runs with direct formatting. So, upon setting ParagraphFormat.StyleName you have to clear run formatting as shown below:

var doc = new Document(MyDir + "ParagraphStyleIssue.docx");

foreach (Paragraph para in doc.GetChildNodes(NodeType.Paragraph, true))

{

para.ParagraphFormat.StyleName = "ParagraphStyle";

foreach (Run run in para.Runs)

{

run.Font.ClearFormatting();

}

para.ParagraphBreakFont.ClearFormatting();

}

var output = MyDir + @"Out.docx";

doc.UpdatePageLayout();

doc.Save(output, SaveFormat.Docx);