Assigning a custom style name to a Run or paragraph throws error

Assigning a custom style name to a Run or paragraph throws error
Error:
Cannot find style ‘Cover Document Title’

Aspose.Words.Document wordDoc = new Aspose.Words.Document();
// wordDoc.RemoveAllChildren();
Aspose.Words.Section wordSection = new Aspose.Words.Section(wordDoc);
wordSection.PageSetup.SectionStart = SectionStart.Continuous;
wordDoc.Sections.Add(wordSection);

Body body = new Body(wordDoc);

wordSection.PageSetup.TopMargin = 42.0f;
wordSection.PageSetup.LeftMargin = 56.0f;
wordSection.PageSetup.BottomMargin = 72.0f;
wordSection.PageSetup.RightMargin = 72.0f;
wordSection.PageSetup.DifferentFirstPageHeaderFooter = true;
wordSection.PageSetup.HeaderDistance = 20;

Aspose.Words.Paragraph para1 = new Aspose.Words.Paragraph(wordDoc);
// listpara1.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1;
// listpara1.ParagraphFormat.StyleName = "Cover Document Title";
Run textSegment = new Run(wordDoc);
textSegment.Text = "First paragraph";
textSegment.Font.Size = 12;
// textSegment.Font.StyleIdentifier = StyleIdentifier.;
textSegment.Font.StyleName = "Cover Document Title";
para1.AppendChild(textSegment);

body.AppendChild(para1);

wordSection.AppendChild(body);
wordDoc.Save(@"C:\temp\docx\SimpleDocument.docx", SaveFormat.Docx);

Hi Prashant,

Thanks for your inquiry. You are getting the exception because there is no custom style in empty document with name ‘Cover Document Title’. Following code example shows how to create custom style. Hope this helps you.

Aspose.Words.Document wordDoc = new Aspose.Words.Document();
Style style = wordDoc.Styles.Add(StyleType.Character, "Cover Document Title");
style.Font.Size = 24;
style.Font.Name = "Verdana";

wordDoc.RemoveAllChildren();
Aspose.Words.Section wordSection = new Aspose.Words.Section(wordDoc);
wordSection.PageSetup.SectionStart = SectionStart.Continuous;
wordDoc.Sections.Add(wordSection);

Body body = new Body(wordDoc);
wordSection.PageSetup.TopMargin = 42.0f;
wordSection.PageSetup.LeftMargin = 56.0f;
wordSection.PageSetup.BottomMargin = 72.0f;
wordSection.PageSetup.RightMargin = 72.0f;
wordSection.PageSetup.DifferentFirstPageHeaderFooter = true;
wordSection.PageSetup.HeaderDistance = 20;

Aspose.Words.Paragraph para1 = new Aspose.Words.Paragraph(wordDoc);
// listpara1.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1;
// listpara1.ParagraphFormat.StyleName = "Cover Document Title";
Run textSegment = new Run(wordDoc);
textSegment.Text = "First paragraph";
textSegment.Font.Size = 12;
// textSegment.Font.StyleIdentifier = StyleIdentifier.;
textSegment.Font.StyleName = "Cover Document Title";
para1.AppendChild(textSegment);
body.AppendChild(para1);

wordSection.AppendChild(body);
wordDoc.Save(MyDir + "Out.docx");

Thanks, it works fine with the example you’ve provided. I appreciate your help.

Hi Prashant,

Thanks for your feedback. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.