Justification in text from tableCell with OpenXML SDK 2.0
Asked Answered
I

2

6

I want to apply a text alignment in table cell in a table with OpenXML.

I don't understand why it is not applied.

Table table = new Table();
TableRow tableHeader = new TableRow();
table.AppendChild<TableRow>(tableHeader);
TableCell tableCell = new TableCell();
tableHeader.AppendChild<TableCell>(tableCell);
Paragraph paragraph = new Paragraph(new Run(new Text("test")));
ParagraphProperties paragraphProperties = new ParagraphProperties();
JustificationValues? justification = GetJustificationFromString("centre");
if (justification != null)
{
     paragraphProperties.AppendChild<Justification>(new Justification() { Val = justification });
}
paragraph.AppendChild<ParagraphProperties>(paragraphProperties);
tableCell.AppendChild<Paragraph>(paragraph);


public static JustificationValues? GetJustificationFromString(string alignment)
{
    switch(alignment)
    {
        case "centre" : return JustificationValues.Center;
        case "droite" : return JustificationValues.Right;
        case "gauche" : return JustificationValues.Left;
        default: return null;
    }
}

Thx for you help !

Interleave answered 2/8, 2012 at 14:27 Comment(1)
Looks fine, Did you tried by changing the type from JustificationValues? to JustificationValuesSharpedged
D
17

Does it work if you were to apply the paragraphProperties to the parent cell rather than the paragraph?

Table table = new Table();
TableRow tableHeader = new TableRow();
table.AppendChild<TableRow>(tableHeader);
TableCell tableCell = new TableCell();
tableHeader.AppendChild<TableCell>(tableCell);
ParagraphProperties paragraphProperties = new ParagraphProperties();
Paragraph paragraph = new Paragraph(new Run(new Text("test")));
JustificationValues? justification = GetJustificationFromString("centre");

// Use System.Nullable<T>.HasValue instead of the null check.
if (justification.HasValue)
{
    // Using System.Nullable<T>.Value to obtain the value and resolve a warning 
    // that occurs when using 'justification' by itself.
    paragraphProperties.AppendChild<Justification>(new Justification() { Val = justification.Value });
}

// append the paragraphProperties to the tableCell rather than the paragraph element
tableCell.AppendChild<ParagraphProperties>(paragraphProperties);
tableCell.AppendChild<Paragraph>(paragraph);
Console.WriteLine(table.OuterXml);

table.OuterXml before:

<w:tbl xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
  <w:tr>
    <w:tc>
      <w:p>
        <w:r>
          <w:t>test</w:t>
        </w:r>
        <w:pPr>
          <w:jc w:val="center" />
        </w:pPr>
      </w:p>
    </w:tc>
  </w:tr>
</w:tbl>   

table.OuterXml after:

<w:tbl xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
    <w:tr>
    <w:tc>
        <w:pPr>
        <w:jc w:val="center" />
        </w:pPr>
        <w:p>
        <w:r>
            <w:t>test</w:t>
        </w:r>
        </w:p>
    </w:tc>
    </w:tr>
</w:tbl>

I'm fairly new to OpenXml. Is the result saved to a word document and viewed in word?

Divulsion answered 30/8, 2012 at 1:2 Comment(1)
When opened with the Productivity Tool this is rendered as an OpenXmlUnknownElement. Thus cannot be converted to pdf, etc. Any way to solve this?Jewel
I
0

The reason that the ParagraphProperties node is having no effect is that the ORDER of the nodes matters here.

You are (quite reasonably!) adding the paragraph properties to the paragraph node AFTER the Run. You would have thought that the software would consider that node first whichever order it was specified in. However, this is Microsoft Word and the order does matter.

You need to append the ParagraphProperties node BEFORE the run.

I had exactly the same issue myself. Appending the ParagraphProperties first fixed the issue.

Iwo answered 29/11, 2018 at 23:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.