This project has moved. For the latest updates, please go here.

Text orientation in table

Dec 25, 2014 at 9:05 PM
Edited Dec 26, 2014 at 5:36 AM
How to set vertical orientation for text in table in code with DocX?
Image
Dec 26, 2014 at 12:00 PM
I made myself.
Add in _Enumerations.cs:
  public enum TextDirection
  {
      btLr,
      right
  };
Add in Table.cs:
        public TextDirection TextDirection
        {
            get
            {
                XElement tcPr = Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName));

                // If tcPr is null, this cell contains no width information.
                if (tcPr == null)
                    return TextDirection.right;
                XElement textDirection = tcPr.Element(XName.Get("textDirection", DocX.w.NamespaceName));
                if (textDirection == null)
                    return TextDirection.right;
                XAttribute val = textDirection.Attribute(XName.Get("val", DocX.w.NamespaceName));
                if (val == null)
                    return TextDirection.right;

                // If val is not a VerticalAlign enum, something is wrong with this attributes value, so remove it and return VerticalAlignment.Center;
                try
                {
                    return (TextDirection)Enum.Parse(typeof(TextDirection), val.Value, true);
                }

                catch
                {
                    val.Remove();
                    return TextDirection.right;
                }
            }
            set 
            {
                /*
                    * Get the tcPr (table cell properties) element for this Cell,
                    * null will be return if no such element exists.
                    */
                XElement tcPr = Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName));
                if (tcPr == null)
                {
                    Xml.SetElementValue(XName.Get("tcPr", DocX.w.NamespaceName), string.Empty);
                    tcPr = Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName));
                }

                /*
                 * Get the vAlign (table cell vertical alignment) element for this Cell,
                 * null will be return if no such element exists.
                 */
                XElement textDirection = tcPr.Element(XName.Get("textDirection", DocX.w.NamespaceName));
                if (textDirection == null)
                {
                    tcPr.SetElementValue(XName.Get("textDirection", DocX.w.NamespaceName), string.Empty);
                    textDirection = tcPr.Element(XName.Get("textDirection", DocX.w.NamespaceName));
                }

                // Set the VerticalAlignment in 'val'
                textDirection.SetAttributeValue(XName.Get("val", DocX.w.NamespaceName), value.ToString());
            
            }
        }
Rebuild and use ;)

Example:
// Create a document.
using (DocX document = DocX.Create(@"Test.docx"))
{
// Add a Table to this document.
Table t = document.AddTable(2, 3);
// Specify some properties for this Table.
t.Alignment = Alignment.center;
t.Design = TableDesign.MediumGrid1Accent2;
// Add content to this Table.
t.Rows[0].Cells[0].Paragraphs.First().Append("A");
t.Rows[0].Cells[0].TextDirection = TextDirection.btLr;
t.Rows[0].Cells[1].Paragraphs.First().Append("B");
t.Rows[0].Cells[1].TextDirection = TextDirection.btLr;
t.Rows[0].Cells[2].Paragraphs.First().Append("C");
t.Rows[0].Cells[2].TextDirection = TextDirection.btLr;
t.Rows[1].Cells[0].Paragraphs.First().Append("D");
t.Rows[1].Cells[1].Paragraphs.First().Append("E");
t.Rows[1].Cells[2].Paragraphs.First().Append("F");
// Insert the Table into the document.
document.InsertTable(t);
document.Save();
}// Release this document from memory.
Marked as answer by mbdijs on 12/26/2014 at 5:00 AM
Developer
Jan 5, 2015 at 9:09 AM
Added to sources. Thanks. Next time please add it as a patch so I can easily include it ;-)