How to modify the font size of a document from code RRS feed

  • Question

  • How can the parts of the document be discovered and the fontsize changed?

    Here is a sample table used,

    <w:sdt docpart="2CFD5BC34C8546929C222BE9FE7FE85A" id="-566108754" sdttag="content control 1" showingplchdr="t" title="content control 1">Main Title here.</w:sdt>   <w:sdt docpart="B3E0DD81C49046B5BB844C80F7E0D939" id="1165130247">Date of Report</w:sdt> 

    Salesman name:  <w:sdt docpart="B3E0DD81C49046B5BB844C80F7E0D939" id="193582825" showingplchdr="t">John Smith</w:sdt>       Another textbox:  <w:sdt docpart="B3E0DD81C49046B5BB844C80F7E0D939" id="-1475904701" showingplchdr="t">Jane Smith.</w:sdt>

    RunProperties runprop = new RunProperties(); Run run = new Run(); Run run2 = new Run(); FontSize fonz = new FontSize(); RunProperties runprop2 = new RunProperties(); RunFonts runf = new RunFonts(); foreach (DocumentFormat.OpenXml.Wordprocessing.TableRow tableRow in table.Descendants<DocumentFormat.OpenXml.Wordprocessing.TableRow>()) { i++; foreach (DocumentFormat.OpenXml.Wordprocessing.TableCell tableCell in tableRow.Descendants<DocumentFormat.OpenXml.Wordprocessing.TableCell>()) { j++; DocumentFormat.OpenXml.Wordprocessing.Paragraph para = tableCell.GetFirstChild<DocumentFormat.OpenXml.Wordprocessing.Paragraph>(); run = para.GetFirstChild<Run>(); if (run != null) {

    // run or para, both have the same result fonz = run.Descendants<DocumentFormat.OpenXml.Wordprocessing.FontSize>().FirstOrDefault(); fonz.Val = "6"; } } }

    Here if this code runs, the text outside of the content controls is changed to '6' fontsize.  but the top part is only content control and the bottom is label + content control.   The label here is "Salesman name:" and "Another textbox"

    Using the above reference the fontsize of this can be changed, but the text inside the sdt's is not touched

    How are these parts 'addressed' to be able to change the properties?

    • Edited by rogersbra1 Friday, December 23, 2016 9:30 PM
    Friday, December 23, 2016 8:49 PM

All replies

    Saturday, December 24, 2016 12:07 PM
  • Hi rogersbra1,

    try to use the code mentioned below may help you to solve your issue.

    The following example shows how to search for all content controls in a document, regardless of whether those content controls are in the main document part, in the headers/footers, or in endnotes/footnotes.  This example uses LINQ to XML.  If you are using the strongly-typed OM of the Open XML SDK, the code would be identical, except for the code to actually process the content controls.

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Xml;
    using System.Xml.Linq;
    using DocumentFormat.OpenXml.Packaging;
    public static class Extensions
        public static XDocument GetXDocument(this OpenXmlPart part)
            XDocument partXDocument = part.Annotation<XDocument>();
            if (partXDocument != null)
                return partXDocument;
            using (Stream partStream = part.GetStream())
            using (XmlReader partXmlReader = XmlReader.Create(partStream))
                partXDocument = XDocument.Load(partXmlReader);
            return partXDocument;
    class Program
        private static void IterateContentControlsForPart(OpenXmlPart part)
            XNamespace w = “”;
            XDocument doc = part.GetXDocument();
            foreach (var sdt in doc.Descendants(w + “sdt”))
                Console.WriteLine(“Found content control”);
        public static void IterateContentControls(WordprocessingDocument doc)
            foreach (var part in doc.MainDocumentPart.HeaderParts)
            foreach (var part in doc.MainDocumentPart.FooterParts)
            if (doc.MainDocumentPart.EndnotesPart != null)
            if (doc.MainDocumentPart.FootnotesPart != null)
        static void Main(string[] args)
            using (WordprocessingDocument doc = WordprocessingDocument.Open(“Test.docx”, false))


    Processing all Content Parts in an Open XML WordprocessingML Document



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact

    Monday, December 26, 2016 1:24 AM