Size table to Page Width?

hi there

Is it possible to size a DOM table to the width of the page? I’m adding a table to a header and adding it to certain pages of my existing PDF and would like the table to auto size to the width of the page. This is the code I’m using.


var headerTable = new Aspose.PDF.Table {DefaultCellBorder = new PDF.BorderInfo(PDF.BorderSide.Top, 0.1F)};
var margin = new PDF.MarginInfo { Top = 2f, Left = 0f, Right = 0f, Bottom = 5f };
headerTable.DefaultCellPadding = margin;

var row1 = headerTable.Rows.Add();
var cell1= row1.Cells.Add();
cell1.Alignment = PDF.HorizontalAlignment.Left;
var textCell= new TextFragment(“my column text that should stretch…”);
cell1.Paragraphs.Add(textCell);
header.Paragraphs.Add(headerTable);

Thanks

Hi,

Thanks for contacting support.

In order to accomplish your requirements, you may get page dimensions and set TableWidth information equal to page width information. Please take a look over following code snippet. In case you encounter any issue, please share the resource PDF file, so that we can test the scenario in our environment.

[C#]

// Open document
Document pdfDocument = new Document("c:/pdftest/S_B-AUTO-BROKERS-APPLICATION.pdf");

Aspose.Pdf.HeaderFooter header = new Aspose.Pdf.HeaderFooter();

var headerTable = new Aspose.Pdf.Table
{
    DefaultCellBorder = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.Top, 0.1F)
};

var margin = new Aspose.Pdf.MarginInfo
{
    Top = 2f,
    Left = 0f,
    Right = 0f,
    Bottom = 5f
};

headerTable.DefaultCellPadding = margin;

// Set table default column width equal to page width
headerTable.DefaultColumnWidth = pdfDocument.Pages[1].PageInfo.Width.ToString();

// Set table border color as Red
headerTable.Border = new BorderInfo(BorderSide.All, Aspose.Pdf.Color.Red);

var row1 = headerTable.Rows.Add();

var cell1 = row1.Cells.Add();
cell1.Alignment = Aspose.Pdf.HorizontalAlignment.Left;

var textCell = new Aspose.Pdf.Text.TextFragment("my column text that should stretch....");
cell1.Paragraphs.Add(textCell);

header.Paragraphs.Add(headerTable);

pdfDocument.Pages[1].Header = header;

pdfDocument.Save("c:/pdftest/TableInHeader.pdf");

Thank you, this is working. I did not realize you could just set DefaultColumnWidth prior to the columns being added.


One related question - is it possible to style the text in the Text Fragment differently? In your example below - can I style the text like so, where a portion of the text is bolded?

my column text that should stretch…

Thanks

Hi,

Please try using following code snippet to accomplish your requirements.

[C#]

// Open document
Document pdfDocument = new Document("c:/pdftest/S_B-AUTO-BROKERS-APPLICATION.pdf");

Aspose.Pdf.HeaderFooter header = new Aspose.Pdf.HeaderFooter();

var headerTable = new Aspose.Pdf.Table
{
    DefaultCellBorder = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.Top, 0.1F)
};

var margin = new Aspose.Pdf.MarginInfo
{
    Top = 2f,
    Left = 0f,
    Right = 0f,
    Bottom = 5f
};

headerTable.DefaultCellPadding = margin;

// Set table default column width equal to page width
headerTable.DefaultColumnWidth = pdfDocument.Pages[1].PageInfo.Width.ToString();

// Set table border color as Red
headerTable.Border = new BorderInfo(BorderSide.All, Aspose.Pdf.Color.Red);

var row1 = headerTable.Rows.Add();

var cell1 = row1.Cells.Add();
cell1.Alignment = Aspose.Pdf.HorizontalAlignment.Left;

var textCell = new Aspose.Pdf.Text.TextFragment();

// Create TextSegment instances
Aspose.Pdf.Text.TextSegment normalSegment = new Aspose.Pdf.Text.TextSegment("my column text that")
{
    TextState = { ForegroundColor = Aspose.Pdf.Color.Red }
};

textCell.Segments.Add(normalSegment);

Aspose.Pdf.Text.TextSegment boldSegment = new Aspose.Pdf.Text.TextSegment(" Should ")
{
    TextState =
    {
        ForegroundColor = Aspose.Pdf.Color.Red,
        FontStyle = Aspose.Pdf.Text.FontStyles.Bold
    }
};

textCell.Segments.Add(boldSegment);

Aspose.Pdf.Text.TextSegment thirdSegment = new Aspose.Pdf.Text.TextSegment("stretch....")
{
    TextState = { ForegroundColor = Aspose.Pdf.Color.Red }
};

textCell.Segments.Add(thirdSegment);

cell1.Paragraphs.Add(textCell);

header.Paragraphs.Add(headerTable);

pdfDocument.Pages[1].Header = header;

pdfDocument.Save("c:/pdftest/TableInHeader.pdf");
Thank you, the text fragment formatting is working now.

However, back to the original table width issue, now when I add a new column to my table, it is stretching past the page with the following code, instead of maintaining the table size even with the new column. Additionally, the horizontal alignment for the cell (in this case, for the page number) does not align to the right.

headerTable.DefaultColumnWidth = pdfDocument.Pages[1].PageInfo.Width.ToString();
// set table border color as Red
headerTable.Border = new BorderInfo(BorderSide.All, Aspose.Pdf.Color.Red);
var row1 = headerTable.Rows.Add();
var cell1 = row1.Cells.Add();
cell1.Alignment = Aspose.Pdf.HorizontalAlignment.Left;
var textCell = new Aspose.Pdf.Text.TextFragment("my column text that should stretch....");
cell1.Paragraphs.Add(textCell);

var cell2 = row1.Cells.Add();
cell2.Alignment = Aspose.Pdf.HorizontalAlignment.Right;
var pageNumberCell = new Aspose.Pdf.Text.TextFragment("Page $p of $P");
cell2.Paragrahs.Add(pageNumberCell);

header.Paragraphs.Add(headerTable);
pdfDocument.Pages[1].Header = header;
pdfDocument.Save("c:/pdftest/TableInHeader.pdf");

Hi,

Thanks for sharing the details.

The reason second column do not render in PDF file is because you have set table width equal to page width and in order to generate correct output, you need to set smaller value to DefaultColumnWidth property. For your reference, I have also attached the output generated over my end.

[C#]
// set table default column width equal to page widht
headerTable.DefaultColumnWidth = “200”;
//pdfDocument.Pages[1].PageInfo.Width.ToString();

Hi there

I’m still having issues with having the table header taking up the width of the page. Here’s the complete code and the output generated. The red header table should start where the horizontal black line starts and ends where the horizontal black line ends.


Document pdfDocument = new Document(“c:/temp/template.pdf”);
Aspose.Pdf.HeaderFooter header = new Aspose.Pdf.HeaderFooter();
var headerTable = new Aspose.Pdf.Table { DefaultCellBorder = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.Top, 0.1F) };
var margin = new Aspose.Pdf.MarginInfo { Top = 2f, Left = 0f, Right = 0f, Bottom = 5f };
headerTable.DefaultCellPadding = margin;

headerTable.DefaultColumnWidth = “200”;
headerTable.Alignment = HorizontalAlignment.Center;
// set table border color as Red
headerTable.Border = new BorderInfo(BorderSide.All, Aspose.Pdf.Color.Red);

var pageWidth = pdfDocument.Pages[1].PageInfo.Width;
var firstColWidth = Math.Round(pageWidth * 0.75);
var secondColWidth = Math.Round(pageWidth * 0.25);

headerTable.ColumnWidths = firstColWidth.ToString() + " " + secondColWidth.ToString();
var row1 = headerTable.Rows.Add();
var cell1 = row1.Cells.Add();
cell1.Alignment = Aspose.Pdf.HorizontalAlignment.Left;

var textCell = new Aspose.Pdf.Text.TextFragment();
// create TextSegment instance
Aspose.Pdf.Text.TextSegment normalSegment = new Aspose.Pdf.Text.TextSegment(“my column text that”);
// set foreground color as Red
normalSegment.TextState.ForegroundColor = Aspose.Pdf.Color.Red;
// add segment to segments collection of TextFragment
textCell.Segments.Add(normalSegment);
Aspose.Pdf.Text.TextSegment boldSegment = new Aspose.Pdf.Text.TextSegment(" Should ");
boldSegment.TextState.ForegroundColor = Aspose.Pdf.Color.Red;
boldSegment.TextState.FontStyle = Aspose.Pdf.Text.FontStyles.Bold;
textCell.Segments.Add(boldSegment);
Aspose.Pdf.Text.TextSegment thirdSegment = new Aspose.Pdf.Text.TextSegment(“stretch…”);
thirdSegment.TextState.ForegroundColor = Aspose.Pdf.Color.Red;
textCell.Segments.Add(thirdSegment);
cell1.Paragraphs.Add(textCell);

var cell2 = row1.Cells.Add();
cell2.Alignment = Aspose.Pdf.HorizontalAlignment.Right;
var pageNumberCell = new Aspose.Pdf.Text.TextFragment(“Page $p of $P”);
cell2.Paragraphs.Add(pageNumberCell);

header.Paragraphs.Add(headerTable);
pdfDocument.Pages[1].Header = header;
pdfDocument.Save(“c:/temp/TableInHeader.pdf”);

Hi there

Here’s an update, I’m able to get the header to the page width, but now, my second column isn’t not aligning to the right. See attached output. Updated code:

Document pdfDocument = new Document(“c:/temp/template.pdf”);
Aspose.Pdf.HeaderFooter header = new Aspose.Pdf.HeaderFooter();
var headerTable = new Aspose.Pdf.Table { DefaultCellBorder = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.Top, 0.1F)};
var margin = new Aspose.Pdf.MarginInfo { Top = 2f, Left = 0f, Right = 0f, Bottom = 5f };
headerTable.DefaultCellPadding = margin;
headerTable.HorizontalAlignment = HorizontalAlignment.Center;
//var pageWidth = pdfDocument.Pages[1].PageInfo.Width + pdfDocument.Pages[1].PageInfo.Margin.Left + pdfDocument.Pages[1].PageInfo.Margin.Right;
var pageWidth = 595.44;
headerTable.DefaultColumnWidth = pageWidth.ToString();
headerTable.Alignment = HorizontalAlignment.Center;
// set table border color as Red
headerTable.Border = new BorderInfo(BorderSide.All, Aspose.Pdf.Color.Red);

var firstColWidth = pageWidth * 0.75;
var secondColWidth = pageWidth * 0.25;

headerTable.ColumnWidths = firstColWidth.ToString() + " " + secondColWidth.ToString();

var row1 = headerTable.Rows.Add();
var cell1 = row1.Cells.Add();
cell1.Alignment = Aspose.Pdf.HorizontalAlignment.Left;

var textCell = new Aspose.Pdf.Text.TextFragment();
// create TextSegment instance
Aspose.Pdf.Text.TextSegment normalSegment = new Aspose.Pdf.Text.TextSegment(“my column text that”);
// set foreground color as Red
normalSegment.TextState.ForegroundColor = Aspose.Pdf.Color.Red;
// add segment to segments collection of TextFragment
textCell.Segments.Add(normalSegment);
Aspose.Pdf.Text.TextSegment boldSegment = new Aspose.Pdf.Text.TextSegment(" Should ");
boldSegment.TextState.ForegroundColor = Aspose.Pdf.Color.Red;
boldSegment.TextState.FontStyle = Aspose.Pdf.Text.FontStyles.Bold;
textCell.Segments.Add(boldSegment);
Aspose.Pdf.Text.TextSegment thirdSegment = new Aspose.Pdf.Text.TextSegment(“stretch…”);
thirdSegment.TextState.ForegroundColor = Aspose.Pdf.Color.Red;
textCell.Segments.Add(thirdSegment);
cell1.Paragraphs.Add(textCell);

var cell2 = row1.Cells.Add();
var pageNumberCell = new Aspose.Pdf.Text.TextFragment(“Page $p of $P”);
cell2.Alignment = Aspose.Pdf.HorizontalAlignment.Right;
cell2.Paragraphs.Add(pageNumberCell);
header.Paragraphs.Add(headerTable);
pdfDocument.Pages[1].Header = header;
pdfDocument.Pages[1].Header.Margin.Top = 20;
pdfDocument.Pages[1].Header.Margin.Right = 27.36;
pdfDocument.Pages[1].Header.Margin.Left = 27.36;
pdfDocument.Save(“c:/temp/Table-Header-No-Alignment.pdf”);

Hi,


Thanks for sharing the details.

I have tested the scenario and have managed to reproduce same problem that Right Alignment information is not being honored for second cell contents (page number information). For the sake of correction, I have logged it as PDFNEWNET-40800 in our issue tracking system. We will further look into the details of this problem and will keep you posted on the status of correction. Please be patient and spare us little time. We are sorry for this inconvenience.

Hi there

Can you provide an ETA for this fix? It’s critical for a production release for us. Otherwise, I need an alternative option to include the table header as noted.

Hi,


Thanks for your patience.

As we recently have noticed above stated issue, so its pending for review and is not yet resolved. However the product team will surely consider investigating/fixing it as per development schedule and as soon as we have some definite updates regarding its resolution, we will let you know. Please be patient and spare us little time. We are sorry for this delay and inconvenience.

Hi there

I was wondering if there’s an update for a fix to this issue?

Thanks

Hi,


Thanks for your patience.

I am afraid the issue reported earlier is still pending for review and is not yet resolved. However the product team will start investigating the issue as per their development schedule and as soon as we have some further updates, we will let you know.