Tables do not respect paragraph alignment

Hi,

I’ve inserted a table into a paragraph and am trying to align it to the right. However, it looks like the paragraph horizontal alignment and the cell horizontal alignment both don’t work. The table doesn’t take up all the space because it’s set to autofit to the content, but it is still on the left side of the second cell.

Document doc = new Document();
Page page = doc.Pages.Add();
page.PageInfo.Width = PageSize.PageLetter.Width;
page.PageInfo.Height = PageSize.PageLetter.Height;
page.PageInfo.Margin = new MarginInfo(27, 27, 27, 27);
var outerTable = new Table()
{
ColumnAdjustment = ColumnAdjustment.AutoFitToWindow,
Border = new BorderInfo(BorderSide.All),
DefaultCellBorder = new BorderInfo(BorderSide.All),
};
page.Paragraphs.Add(outerTable);
var outerRow = outerTable.Rows.Add();
var outerCell1 = outerRow.Cells.Add(“OuterCell1”);
var outerCell2 = outerRow.Cells.Add();
var innerTable = new Table()
{
ColumnAdjustment = ColumnAdjustment.AutoFitToContent,
Border = new BorderInfo(BorderSide.All),
DefaultCellBorder = new BorderInfo(BorderSide.All),
};
outerCell2.Paragraphs.Add(innerTable);
var innerRow = innerTable.Rows.Add();
var innerCell1 = innerRow.Cells.Add(“InnerCell1”);
var innerCell2 = innerRow.Cells.Add(“InnerCell2”);
//outerCell1.Alignment = HorizontalAlignment.Center;
//outerCell2.Alignment = HorizontalAlignment.Right;
outerCell1.Paragraphs[0].HorizontalAlignment = HorizontalAlignment.Center;
outerCell2.Paragraphs[0].HorizontalAlignment = HorizontalAlignment.Right;
doc.Save(“tableAlignment.pdf”);

Hello Daniel,

Thanks for contacting support.

In order to set horizontal alignment of content inside a table cell, you need to set it for its DefaultTextState property. Please check following code snippet where I have set the horizontal alignment of content inside table cells.

Document doc = new Document();

Page page = doc.Pages.Add();

page.PageInfo.Width = PageSize.PageLetter.Width;

page.PageInfo.Height = PageSize.PageLetter.Height;

page.PageInfo.Margin = new MarginInfo(27, 27, 27, 27);

var outerTable = new Table()

{

    ColumnAdjustment = ColumnAdjustment.AutoFitToWindow,

    Border = new BorderInfo(BorderSide.All),

    DefaultCellBorder = new BorderInfo(BorderSide.All),

};

page.Paragraphs.Add(outerTable);

var outerRow = outerTable.Rows.Add();

var outerCell1 = outerRow.Cells.Add("OuterCell1");

var outerCell2 = outerRow.Cells.Add();

var innerTable = new Table()

{

    ColumnAdjustment = ColumnAdjustment.AutoFitToWindow,

    Border = new BorderInfo(BorderSide.None),

    DefaultCellBorder = new BorderInfo(BorderSide.None),

};

outerCell2.Paragraphs.Add(innerTable);

var innerRow = innerTable.Rows.Add();

var innerCell1 = innerRow.Cells.Add("InnerCell1");

var innerCell2 = innerRow.Cells.Add("InnerCell2");

outerCell1.DefaultCellTextState.HorizontalAlignment = HorizontalAlignment.Center;

outerCell2.DefaultCellTextState.HorizontalAlignment = HorizontalAlignment.Right;

innerCell1.DefaultCellTextState.HorizontalAlignment = HorizontalAlignment.Right;

innerCell2.DefaultCellTextState.HorizontalAlignment = HorizontalAlignment.Right;

doc.Save(dataDir + "tableAlignment.pdf");

For your reference, I have also attached generated output by above code. In case of any further assistance, please feel free to contact us.

Best Regards,

Hi Asad,


That sample code still does not not do what we want. If you notice, in the original reproducer I have the second table auto fits to the content which is intentional. I don’t want the inner table to take up the entire cell. That way I can set a margin size and have a consistent amount of whitespace between the inner table’s cells which may contain text/images. In this case I need to have the entire inner table aligned to the right of the outerCell2 to properly create my layout. In your attached code if you change the innerTable’s ColumnAdjustment back to AutoFitToContent you’ll notice the same behavior where all the text will be on the left side of outerCell2.

Hi Daniel,


Thanks for adding more details.

I have tested the scenario in our environment as per your requirement and observed that HorizontalAlignment was not being honored inside table cell when content was a nested table. So for the purpose of detailed investigation, I have logged an issue as PDFNET-42908 in our issue tracking system. We will further look into the details of the issue and keep you posted with the status of its rectification. Please be patient and spare us little time.

We are sorry for the inconvenience.


Best Regards,

May I get an update on this?

@dfactset

Regretfully, the ticket has not been yet resolved due to other pending issues in the queue. We have recorded your concerns and will surely inform you once the issue is fixed. We apologize for your inconvenience.