2012-08-02 12 views
5

Quiero aplicar una alineación de texto en la celda de la tabla en una tabla con OpenXML.Justificación en el texto de tableCell con OpenXML SDK 2.0

No entiendo por qué no se aplica.

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 para usted help!

+0

Se ve bien, ¿Probaste cambiando el tipo de JustificationValues? a JustificationValues ​​ – Kiru

+0

Lo hice pero nada cambia – Aelios

Respuesta

14

¿Funciona si tuviera que aplicar el paragraphProperties a la celda padre en lugar del párrafo?

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 antes:

<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 después:

<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> 

yo soy bastante nuevo en OpenXml. ¿El resultado se guarda en un documento de Word y se visualiza en Word?

+0

¡Bien, está bien ahora! Gracias – Aelios

+0

Cuando se abre con la herramienta de productividad esto se representa como un OpenXmlUnknownElement. Por lo tanto, no se puede convertir a PDF, etc. ¿Alguna forma de resolver esto? –

+0

Nuevo problema, publique un hilo por separado. – Nicodemeus

Cuestiones relacionadas