How to set font size in empty cell of a Word table using the Open XML SDK?
Asked Answered
T

2

2

I am creating a table in OpenXml with C# in a Word file. I used some code mentioned in this question to set the fontsize of the text in the cells. It works fine for the cells that contain text, but the empty cells seem to be given the normal style, and with that a bigger font size, which makes the row height bigger.

Here is my sample code with a single row with a single cell, where the font size should be 9:

TableRow tr = new TableRow();
TableCell tc = new TableCell();
Paragraph par = new Paragraph();
Run run = new Run();
Text txt = new Text("txt");

RunProperties runProps = new RunProperties();
FontSize fontSize = new Fontsize() { Val = "18" }; // font size 9

runProps.Append(fontSize);

run.Append(runProps);
run.Append(txt);

para.Append(run);
tc.Append(para);
tr.Append(tc);

Here is an example of the resulting table. As you can see the middle row is taller than the others. In the cells that say "txt" the font size is 9, but in the blank cell the font size is 11. The code above is used for all the cells, where the empty cell simply has the text "". When I looked at the file with the Open XML Tool, I can see that the RunProperties with value 18 is there for all the cells including the empty one.

Resulting table

How do I set the font size of a cell without displaying any text?

Tamarin answered 26/7, 2018 at 10:5 Comment(0)
J
1

I confirm what you report. One way around it would be to substitute a space " " for the "empty" string, adding "space preserve" to the text run, of course.

Another possibility would be to create a character style and apply it to the table cells. This turned out to be trickier than it sounds as the style needs to be applied twice to cells that contain text: once to the ParagraphMarkRunProperties and once to the RunProperties. For empty cells the style need be applied only to the ParagraphMarkRunProperties.

Actually, on reflection, you can use the same approach for the font size...

I've included both approaches in the code below. The one for just the font size is commented out (four lines).

The sample code assumes that the third cell of the one-row, four column table, has no content. Run and Text information is added only when there is content.

private void btnCreateTable_Click(object sender, EventArgs e)
{   
    string filePath = @"C:\X\TestCreateTAble.docx";
    using (WordprocessingDocument pkg = WordprocessingDocument.Open(filePath, true))
    {
        MainDocumentPart partDoc = pkg.MainDocumentPart;
        Document doc = partDoc.Document;

        StyleDefinitionsPart stylDefPart = partDoc.StyleDefinitionsPart;
        Styles styls = stylDefPart.Styles;
        Style styl = CreateTableCharacterStyle();
        stylDefPart.Styles.AppendChild(styl);

        Table t = new Table();
        TableRow tr = new TableRow();

        for (int i = 1; i <= 4; i++)
        {
            TableCell tc = new TableCell(new TableCellProperties(new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "500" }));
            Paragraph para = new Paragraph();
            ParagraphProperties paraProps = new ParagraphProperties();
            ParagraphMarkRunProperties paraRunProps = new ParagraphMarkRunProperties();
            RunStyle runStyl = new RunStyle() { Val = "Table9Point" };
            paraRunProps.Append(runStyl);
            //    FontSize runFont = new FontSize() {Val = "18" };
            //    paraRunProps.Append(runFont);
            paraProps.Append(paraRunProps);
            para.Append(paraProps);

            Run run = new Run();

            Text txt = null;
            if (i == 3)
            {
            }
            else
            {
                txt = new Text("txt");
                txt.Space = SpaceProcessingModeValues.Preserve;
                RunProperties runProps = new RunProperties();
                RunStyle inRunStyl = (RunStyle) runStyl.Clone();
                runProps.Append(inRunStyl);
                //    FontSize inRunFont = (FontSize) runFont.Clone();
                //    runProps.Append(inRunFont);
                run.Append(runProps);
                run.Append(txt);
                para.Append(run);
           }
            tc.Append(para);
            tr.Append(tc);
        }
        t.Append(tr);
        //Insert at end of document
        SectionProperties sectProps = doc.Body.Elements<SectionProperties>().LastOrDefault();
        doc.Body.InsertBefore(t, sectProps);
    }
}

private Style CreateTableCharacterStyle()
{
    Style styl = new Style()
    {
        CustomStyle = true,
        StyleId = "Table9Point",
        Type = StyleValues.Character,
    };
    StyleName stylName = new StyleName() { Val = "Table9Point" };
    styl.AppendChild(stylName);
    StyleRunProperties stylRunProps = new StyleRunProperties();
    stylRunProps.FontSize = new FontSize() { Val = "18" };
    styl.AppendChild(stylRunProps);
    BasedOn basedOn1 = new BasedOn() { Val = "DefaultParagraphFont" };
    styl.AppendChild(basedOn1);
    return styl;
}
Jerlenejermain answered 26/7, 2018 at 18:53 Comment(1)
Thanks for the answer! I ended up adding the font size I wanted to the paragraph run propertiesTamarin
S
0

Just need to set the FontSize and FontSizeComplexScript for the ParagraphProperties and RunProperties, like this:

var cell = new TableCell(
    new Paragraph(
        new ParagraphProperties(
            new SpacingBetweenLines { LineRule = LineSpacingRuleValues.Auto, Before = "0", After = "0" },
            new RunProperties(
                new FontSize { Val = "18" },
                new FontSizeComplexScript { Val = "18" })),
        new Run(
            new RunProperties(
                new FontSize { Val = "18" },
                new FontSizeComplexScript { Val = "18" }),
            new Text { Text = String.Empty, Space = SpaceProcessingModeValues.Preserve })));
Saith answered 5/5, 2022 at 17:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.