Forum


Replies: 4   Views: 80
Table in table cell without padding

Posted by aventyret  · 03-12-2024 - 10:11

I have bit of a conundrum. We're using PHPDocX to generate word files from some form data.

In order to do this we sometimes build a grid using tables. This works well on one level. So we might have a structuredTag for tex inpout, and then som text that runs atfer it. In that case we make a table and put the structured tag in one column and the text after.

But sometimes we need to have two columns. For that we also use a table and then put our content in each column.

The problem arrises when putting a table as content. I added a screenshot to show how it comes out. The third text and structuredTag needs to align to the left with the first two. The Label is put there just using addText() 

https://pasteboard.co/HKZlx9ffQn4e.png

 

The outer table is setup like this:

        $col1 = [
            'value' => $leftFragment,
        ];
        $col2 = [
            'value' => $rightFragment,
        ];

        // Create a table with two columns
        $table = [
            [
                $col1,
                $col2
            ]
        ];

        $tableOptions = [
            'tableLayout' => 'fixed',
            'tableStyle' => 'PlainTablePHPDOCX'
        ];

And in the $leftFragment I have this table:

 

 $options = [
            'placeholderText' => 'structuredTag,
            'border' => 'single',
            'borderSpacing' => 0,
            'borderTopSpacing' => 0,
            'borderBottomSpacing' => 0,         
            'spacingBottom' => 0,
            'spacingTop' => 0,
        ];

        $leftFragment->addStructuredDocumentTag('richText', $options);

       
            $rightFragment = new WordFragment($docx);
            $rightFragment->addText($'More text, [
                'spacingBottom' => 0,
                'spacingTop' => 0,
            ]);


            $table = [
                [
                    $leftFragment,
                    $rightFragment
                ]
            ];

            $tableOptions = [
                'tableLayout' => 'fixed',
                'tableStyle' => 'PlainTablePHPDOCX',
            ];

            $docx->addTable($table, $tableOptions);

Posted by admin  · 03-12-2024 - 11:37

Hello,

We recommend you remove the cell margin from the inner table, not from the outer table. Unless you apply a margin/spacing style to specific contents, all cell contents get the same cell margin.

As detailed on the addTable API documentation page (https://www.phpdocx.com/api-documentation/word-content/add-table-Word-document-with-PHP), you can use the cellMargin option to set the cell margins applied to all cells in the table:

$table = [
    [
        $col1,
        $col2,
    ]
];

$tableOptions = [
    'tableLayout' => 'fixed',
    'tableStyle' => 'PlainTablePHPDOCX',
    'cellMargin' => [
        'left' => 0,
        'top' => 0,
        'bottom' => 0,
        'right' => 0,
    ],
];

$docx->addTable($table, $tableOptions);

or to specific cells:

$table = [
    [
        [
            'value' => $col1,
            'cellMargin' => [
                'left' => 0,
                'top' => 0,
                'bottom' => 0,
                'right' => 0,
            ],
        ],
        [
            'value' => $col2,
            'cellMargin' => [
                'left' => 200,
                'top' => 0,
                'bottom' => 0,
                'right' => 200,
            ],
        ]
    ]
];

$tableOptions = [
    'tableLayout' => 'fixed',
    'tableStyle' => 'PlainTablePHPDOCX',
];

$docx->addTable($table, $tableOptions);

Also please note that using a fixed layout, we recommend you set the column widths:

$table = [
    [
        [
            'value' => $col1,
            'cellMargin' => [
                'left' => 0,
                'top' => 0,
                'bottom' => 0,
                'right' => 0,
            ],
        ],
        [
            'value' => $col2,
            'cellMargin' => [
                'left' => 200,
                'top' => 0,
                'bottom' => 0,
                'right' => 200,
            ],
        ]
    ]
];

$tableOptions = [
    'tableLayout' => 'fixed',
    'tableStyle' => 'PlainTablePHPDOCX',
    'columnWidths' => [1400,1400],
];

$docx->addTable($table, $tableOptions);

Otherwise, you can get strange cell outputs when applying custom margins.

Regards.

Posted by aventyret  · 03-12-2024 - 12:19

Thank you very much,

That makes it align properly. But I end up with getting the left corner of my structuredTag clipped.

Like this: https://pasteboard.co/xpElJuJZGpkw.png

Is there any way to make the bit that ends up left of the margin visible? kind of like having the table being transparent instead of clipping what's outside?

 

Posted by admin  · 03-12-2024 - 12:58

Hello,

We have done some tests with some MS Word versions and don't see any way to apply transparency to the outer table. Even after removing the borders, MS Word does an overlapping. Transparency (no color) and z-index can be applied to very specific contents in MS Word.

Maybe the easiest solution is to set a very low value that displays the left corner and move the alignment as little as possible:

'left' => 36,

Cell margins use twips (twentieth of a point) as size value.

Regards.

Posted by aventyret  · 03-12-2024 - 17:13

Thank you so much for your excellent support.

Right now we solved this problem by adding an indent to the structured tag 

'indentLeft' => 40

to Have it a bit further in. This way we can use margon 0 and still get everything lined up. It l works for us if it's aligned with the label above (even better). That introduced some new challenges though.. So I might have to look into your solution but for the other side of the table :-)