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

心已入冬 提交于 2019-12-11 11:59:12

问题


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.

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


回答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;
}


来源:https://stackoverflow.com/questions/51536259/how-to-set-font-size-in-empty-cell-of-a-word-table-using-the-open-xml-sdk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!