Change style (fontname and size) and apply it for the whole document

Hallo,

Is there a way to change the style (fontname and size) and apply it for the whole document?


9 Replies

MR Manikandan Ravichandran Syncfusion Team May 29, 2020 02:56 PM UTC

Hi Vince,

Thank you contacting Syncfusion support.

From the given details, we have found that your requirement is to change the font name and font size of the text ranges. For this requirement, we suggest you to use the below code example.

For change the font size and font name in the document 
WordDocument document = new WordDocument(@"Sample.docx");
WParagraphStyle style = document.Styles.FindByName(
"Normal") as WParagraphStyle;
style.CharacterFormat.FontName =
"Arial";
style.CharacterFormat.FontSize = 20;
document.Save(
"output.docx");
document.Close();
 

For change the font size and font name when create the text range. 
WordDocument document = new WordDocument();
document.EnsureMinimal();

//New character format
WCharacterFormat characterFormat = new WCharacterFormat(document);
characterFormat.FontName =
"Arial";
characterFormat.FontSize = 20;

//First Paragraph
IWParagraph paragraph = document.LastSection.AddParagraph();
IWTextRange textRange = paragraph.AppendText(
"Hello world");
textRange.ApplyCharacterFormat(characterFormat);

//Second paragraph
paragraph = document.LastSection.AddParagraph();
textRange = paragraph.AppendText(
"Hello world Hello world");
textRange.ApplyCharacterFormat(characterFormat);

document.Save(
"output.docx");
document.Close();
 

If you think, we misunderstood your requirement, kindly share the complete requirement or runnable sample and also expected output document/ screenshot, so that we can proceed further to analyse and update you with the appropriate details

Please let us know if you have any other questions.

Regards,
Manikandan Ravichandran



VI Vince May 30, 2020 07:32 AM UTC

Hallo Manikandan,

I converted your code to Visual Basic:

Dim document As WordDocument = New WordDocument()
Dim section As IWSection = document.AddSection()
Dim DefaultStyle As WParagraphStyle = TryCast(document.Styles.FindByName("Normal"), WParagraphStyle)
DefaultStyle.CharacterFormat.FontName = "Arial"
DefaultStyle.CharacterFormat.FontSize = 10

It generates the error "The object reference was not set as to an object" for the code DefaultStyle.CharacterFormat.FontName = "Arial"

Can you tell me what is wrong?

Best regards, Vince.


MR Manikandan Ravichandran Syncfusion Team June 1, 2020 12:50 PM UTC

Hi Vince,

Thank you for your update.

From the given code example, we have found that your are try to get the paragraph style without adding the paragraph in the Word document. So, we must add paragraph first and then get the paragraph style from this. Based on this we have modified the code as below.
 
Dim document As WordDocument = New WordDocument()
document.EnsureMinimal()
Dim DefaultStyle As WParagraphStyle = TryCast(document.Styles.FindByName("Normal"), WParagraphStyle)
DefaultStyle.CharacterFormat.FontName =
"Arial"
DefaultStyle.CharacterFormat.FontSize = 10
Dim paragraph As IWParagraph = document.LastSection.AddParagraph()
paragraph.AppendText(
"Hello World")
paragraph.ApplyStyle(
"Normal")
document.Save(
"output.docx")
document.Close()
 

Please let us know if you have any other questions.

Regards,
Manikandan Ravichandran
 



VI Vince June 2, 2020 07:42 AM UTC

Hallo Manikandan,

Your code is only changing the style of a single paragraph but I want to change the default style of the whole document. Now the default font in the document is Times New Roman with fontsize 12 but I want it to be Arial with fontsize 10 which I use for the paragraphs. If a user wants to add something in the generated docx file the font is Times New Roman with fontsize 12 and  I want it to be Arial with fontsize 10. Is that possible?

Best regards,

Vince



MR Manikandan Ravichandran Syncfusion Team June 3, 2020 12:58 PM UTC

Hi Vince,

Thank you for your update.

Please find the code example to change the default font in the document as below. 
Dim document As WordDocument = New WordDocument("Sample.docx")
Dim style As WParagraphStyle = TryCast(document.Styles.FindByName("Normal"), WParagraphStyle)
style.CharacterFormat.FontName = "Arial"
style.CharacterFormat.FontSize = 10
document.Save("output.docx")
document.Close()
 

Please find the code example to change the default font in the document which is created from scratch. 
Dim document As WordDocument = New WordDocument()
document.EnsureMinimal()
Dim style As WParagraphStyle = TryCast(document.Styles.FindByName("Normal"), WParagraphStyle)
style.CharacterFormat.FontName = "Arial"
style.CharacterFormat.FontSize = 10
document.Save("Output.docx")
 
document.Close() 

Please let us know if you have any other questions.

Regards,
Manikandan Ravichandran 



VI Vince June 3, 2020 04:25 PM UTC

Hallo Manikandan,

Your code is the same as your reply on May 29 which generates the error "The object reference was not set as an object" for the code style.CharacterFormat.FontName = "Arial".

My reply on May 30 shows that I create a document from scratch and that I use a section which - in my option - conflics with your code document.EnsureMinimal().

Do you have another solution?

Best regards,

Vince




MR Manikandan Ravichandran Syncfusion Team June 4, 2020 06:59 PM UTC

Hi Vince,

Thank you for your update.

Without adding at least one paragraph to the word document, you can’t get the paragraph style from the word document. So, you should add atleast one paragraph before retrieving the paragraph style from the document. When using EnsureMinimal API, it will add one section and one paragraph as default. Otherwise, you can add paragraph and set the paragraph style as like below.
 
Dim document As WordDocument = New WordDocument()
Dim section As IWSection = document.AddSection()
Dim paragrpah As IWParagraph = section.AddParagraph()
Dim DefaultStyle As WParagraphStyle = TryCast(document.Styles.FindByName("Normal"), WParagraphStyle)
DefaultStyle.CharacterFormat.FontName =
"Arial"
DefaultStyle.CharacterFormat.FontSize = 10
document.Save(
"Output.docx")
document.Close()
 

Please let us know if you have any other questions.

Regards,
Manikandan Ravichandran
 



VI Vince June 5, 2020 07:19 AM UTC

Hallo Manikandan,

Now it works the way I want it.

Thanks for your support!

Best regards,

Vince


PN Preethi Nesakkan Gnanadurai Syncfusion Team June 5, 2020 07:23 AM UTC

Hi Vince, 
  
Most welcome. 
  
Regards, 
Preethi 


Loader.
Up arrow icon