none
Apply or set font size,font name for particular style using open XML in C#.net or VB.NET RRS feed

  • Question

  • Hi,

    I have word document.In that document has many style(like normal,heading1,heading2 etc).I want to Apply font size,font name for particular style using open XML.For example i want to apply font size,font name for normal style in that document using open XML in c#.net or vb.net.please provide solution....

    Wednesday, July 22, 2015 1:24 PM

Answers

  • Hi GC_04,

    I think you could get the StyleDefinitionsPart which style are normal, change the StyleDefinitionsPart, modifty the FontSize, and append the StyleRunProperties to styles. Here is a simple demo to modify the FontSize.

            private void StyleSet_Click(object sender, EventArgs e)
            {
                fileName = @"D:\OfficeDev\OpenXML\Font.docx";
                //var node = ExtractStylesPart(fileName);            
                using (var document = WordprocessingDocument.Open(fileName, true))
                {
                    var docPart = document.MainDocumentPart;
                    StyleDefinitionsPart stylesPart = null;
                    stylesPart = docPart.StyleDefinitionsPart;
                    ChangeStyleDefinitionsPart1(stylesPart);                
                }
            }
            private static void ChangeStyleDefinitionsPart1(StyleDefinitionsPart styleDefinitionsPart1)
            {
                Styles styles1 = styleDefinitionsPart1.Styles;
                Style style1 = styles1.GetFirstChild<Style>(); //get the specifc style
                Rsid rsid1 = new Rsid() { Val = "00B10D4B" };
                style1.Append(rsid1);
                StyleRunProperties styleRunProperties1 = new StyleRunProperties();
                FontSize fontSize1 = new FontSize() { Val = "144" };
                styleRunProperties1.Append(fontSize1);
                style1.Append(styleRunProperties1);
            }

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Thursday, July 23, 2015 6:16 AM

All replies

  • Hi,

    I have word document.In that document has many style(like normal,heading1,heading2 etc).I want to extract font size,font name for particular style using open XML.For example i want to extract font size,font name for normal style in that document using open XML in c#.net or vb.net.please provide solution....

    Wednesday, July 22, 2015 1:20 PM
  • Hi,

    I have word document.In that document has many style(like normal,heading1,heading2 etc).I want to extract font size,font name for particular style using open XML.For example i want to extract font size,font name for normal style in that document using open XML in c#.net or vb.net.please provide solution....

    Wednesday, July 22, 2015 1:21 PM
  • Hi,

    I have word document.In that document has many style(like normal,heading1,heading2 etc).I want to Apply font size,font name for particular style using open XML.For example i want to apply font size,font name for normal style in that document using open XML in c#.net or vb.net.please provide solution....

    Wednesday, July 22, 2015 1:23 PM
  • Hi GC_04,

    I think you could get the StyleDefinitionsPart which style are normal, change the StyleDefinitionsPart, modifty the FontSize, and append the StyleRunProperties to styles. Here is a simple demo to modify the FontSize.

            private void StyleSet_Click(object sender, EventArgs e)
            {
                fileName = @"D:\OfficeDev\OpenXML\Font.docx";
                //var node = ExtractStylesPart(fileName);            
                using (var document = WordprocessingDocument.Open(fileName, true))
                {
                    var docPart = document.MainDocumentPart;
                    StyleDefinitionsPart stylesPart = null;
                    stylesPart = docPart.StyleDefinitionsPart;
                    ChangeStyleDefinitionsPart1(stylesPart);                
                }
            }
            private static void ChangeStyleDefinitionsPart1(StyleDefinitionsPart styleDefinitionsPart1)
            {
                Styles styles1 = styleDefinitionsPart1.Styles;
                Style style1 = styles1.GetFirstChild<Style>(); //get the specifc style
                Rsid rsid1 = new Rsid() { Val = "00B10D4B" };
                style1.Append(rsid1);
                StyleRunProperties styleRunProperties1 = new StyleRunProperties();
                FontSize fontSize1 = new FontSize() { Val = "144" };
                styleRunProperties1.Append(fontSize1);
                style1.Append(styleRunProperties1);
            }

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Thursday, July 23, 2015 6:16 AM