Table alignment not applying when saving document

Questions and answers on how to use DOCXReadWrite
Post Reply
Gasper
Posts: 5
Joined: Wed Feb 07, 2024 7:53 am

Table alignment not applying when saving document

Post by Gasper »

I am facing an issue when saving our docx template that contains a table.
The table is aligned to the center of the document, however when I load and save the tamplate it gets aligned to the right.

I have made a very simple test project to test this.

Code: Select all

docx := TDOCXReadWriteVcl.Create(nil);
docx.LoadFromFile(template.docx'); //template.docx contains table aligned to center
docx.SaveToFile('test.docx'); //saving the template aligns the table to the left
Here's a few things I have tried, but none of them seem to do the trick.

Code: Select all

     //Loop through the entire document, check if paras = table, set PAPX.Alignment := axptaCenter
     for i := 0 to WordDoc.Editor.Paras.Count - 1 do
     begin
        if WordDoc.Editor.Paras.Items[i].Type_ = alptTable then
        begin
//          WordDoc.Editor.Paras.Items[i].TempPAPX.Alignment := axptaCenter;
//          WordDoc.Editor.Paras.ParaAtIndex(i).PAPX.Alignment := axptaCenter;
          WordDoc.Editor.Paras.Items[i].Paras.Last.PAPX.Alignment := axptaCenter;
        end;
     end;

//     WordDoc.Editor.Footers.AddDefault;
//     WordDoc.Editor.Footers.Default_.Paras[1].PAPX.Alignment := axptaCenter;
Ideas and any insight is greatly appreciated!
Gasper
Posts: 5
Joined: Wed Feb 07, 2024 7:53 am

Re: Table alignment not applying when saving document

Post by Gasper »

Would anyone happen to have a solution for this please? I'm at a loss of what else to try.

Code: Select all

if docx.Editor.Footers.Default_.Paras.Items[0].Type_ = alptTable then
  begin
    writeln(sLineBreak + 'Item ' + IntToStr(Counter) + ' of footer is a table');
    footerTable := docx.Editor.Footers.Default_.Paras.Tables[0];
    newTable := docx.Editor.Footers.Default_.Paras.AddTable;
    newTable.Assign(footerTable);
  end;
 
docx.Editor.Footers.Default_.Paras[1].PAPX.Alignment := axptaCenter;
docx.Editor.Footers.Default_.Paras[1].AppendPlainText('Text');
I have even tried copying the table, which works, but the new table is still aligned to the left. The appended plain text however gets aligned to the center.
If I try modifying the PAPX of Paras[0] (table?) I get an exception saying 'Para item is not para'.

A solution would be greatly appreciated!
Post Reply