EvotecIT / OfficeIMO

Fast and easy to use cross-platform .NET library that creates or modifies Microsoft Word (DocX) and later also Excel (XLSX) files without installing any software. Library is based on Open XML SDK
MIT License
280 stars 50 forks source link

Table text alignment #81

Closed hxhgxy closed 1 year ago

hxhgxy commented 1 year ago

How to set the text alignment in table cell? left, center or right.

I tried to set text, run, paragraph, tablecell and tablerow properties, it didnt work.

thanks a lot!

hxhgxy commented 1 year ago

if i do it manually in MS word, i cannot make it in table style - dont know why. i can create a paragraph style and apply it to table text.

PrzemyslawKlys commented 1 year ago

There is ParagraphAlignment which seems to work?

image

        internal static void Example_BasicTables1(string folderPath, bool openWord) {
            Console.WriteLine("[*] Creating standard document with tables");
            string filePath = System.IO.Path.Combine(folderPath, "Document with Tables1.docx");
            using (WordDocument document = WordDocument.Create(filePath)) {
                var paragraph = document.AddParagraph("Basic paragraph - Page 4");
                paragraph.ParagraphAlignment = JustificationValues.Center;

                document.AddParagraph();

                WordTable wordTable = document.AddTable(3, 4, WordTableStyle.PlainTable1);
                wordTable.Rows[0].Cells[0].Paragraphs[0].Text = "Test 1";
                wordTable.Rows[1].Cells[0].Paragraphs[0].Text = "Test 2";
                wordTable.Rows[2].Cells[0].Paragraphs[0].Text = "Test 3";
                // align to center
                wordTable.Rows[2].Cells[3].Paragraphs[0].Text = "Center";
                wordTable.Rows[2].Cells[3].Paragraphs[0].ParagraphAlignment = JustificationValues.Center;

                // align to right
                wordTable.Rows[1].Cells[3].Paragraphs[0].Text = "Right";
                wordTable.Rows[1].Cells[3].Paragraphs[0].ParagraphAlignment = JustificationValues.Right;

                // align it on paragraph outside of table
                var paragraph1 = wordTable.Rows[0].Cells[0].Paragraphs[0].AddParagraph();
                paragraph1 = paragraph1.AddParagraph();
                paragraph1.AddText("Ok");
                paragraph1.ParagraphAlignment = JustificationValues.Center;

                var paragraph2 = wordTable.Rows[1].Cells[0].Paragraphs[0].AddParagraphAfterSelf();
                paragraph2 = paragraph2.AddParagraphAfterSelf();
                paragraph2.AddText("Ok2");

                var paragraphBefore = wordTable.Rows[1].Cells[0].Paragraphs[0].AddParagraphBeforeSelf();
                paragraphBefore = paragraphBefore.AddParagraphBeforeSelf();
                paragraphBefore.AddText("Ok but Before");

                wordTable.Rows[2].Cells[0].Paragraphs[0].AddParagraphAfterSelf().AddParagraphAfterSelf().AddParagraphAfterSelf().Text = "Works differently";

                Console.WriteLine(wordTable.Style);

                // lets overwrite style
                wordTable.Style = WordTableStyle.GridTable6ColorfulAccent1;

                document.Save(openWord);
            }
        }
hxhgxy commented 1 year ago

It works well, thank you very much!

I tried to append justification to paragraphproperties, but did something wrong.