Table with more than 8 columns are not being display on pdf page

Hi,
I am creating pdf document by importing table having 10 to 11 columns but On a page, only first 6 columns are being displayed and rest of columns are not getting displayed. I am trying to search its solution but not succeeded.

Please provide the help.

Thanks,
Sudhanshu

@SudhanshuMani

Thanks for contacting support.

In order to keep table on the relevant page, you need to set ColumnAdjustment property of the table to AutoFitToWindow OR you can also specify the ColumnWidths property of the table according to total width of the page. Please check following code snippet where I have added a table with 11 columns inside a page by setting ColumnAdjustment to AutoFitToWindow.

Document doc = new Document();
Page page = doc.Pages.Add();
page.PageInfo.Width = PageSize.PageLetter.Width;
page.PageInfo.Height = PageSize.PageLetter.Height;
var Table = new Table()
{
  ColumnAdjustment = ColumnAdjustment.AutoFitToWindow,
  DefaultCellBorder = new BorderInfo(BorderSide.All)
};
var Row = Table.Rows.Add();
var Cell1 = Row.Cells.Add("Cell");
var Cell2 = Row.Cells.Add("Cell");
var Cell3 = Row.Cells.Add("Cell");
var Cell4 = Row.Cells.Add("Cell");
var Cell5 = Row.Cells.Add("Cell");
var Cell6 = Row.Cells.Add("Cell");
var Cell7 = Row.Cells.Add("Cell");
var Cell8 = Row.Cells.Add("Cell");
var Cell9 = Row.Cells.Add("Cell");
var Cell10 = Row.Cells.Add("Cell");
var Cell11 = Row.Cells.Add("Cell");
page.Paragraphs.Add(Table);
doc.Save(dataDir + "tableAlignment.pdf");

tableAlignment.pdf (2.1 KB)

For your reference, an output obtained from above code is also attached. In case you face any other issue, please share your sample document along with the code snippet, so that we can test the scenario in our environment and address it accordingly.

Hi Asad,
Thanks for quick reply.
I have set the ColumnAdjustment to AutoFitToWindow , now each column is displaying properly but table
content and header is not aligned properly.
I am attaching here sampe file of PDF document , we have to set table content same as in pdf file but I am unable to set the width of columns and alignment as same.

Below are the code that i have done as POC

DataTable dt = new DataTable(“Employee”);
dt.Columns.Add(“ID”, typeof(Int32));
dt.Columns.Add(“Employee Name”, typeof(string));
dt.Columns.Add(“Gender”, typeof(string));
dt.Columns.Add(“Address”, typeof(string));
dt.Columns.Add(“City”, typeof(string));
dt.Columns.Add(“Country”, typeof(string));
dt.Columns.Add(“Pin”, typeof(string));
dt.Columns.Add(“District”, typeof(string));
dt.Columns.Add(“Area”, typeof(string));

        //Add 2 rows into the DataTable object programmatically          

        for (int i = 0; i < 150; i++)
        {
            DataRow dr = dt.NewRow();
            dr[0] = (i + 1);
            dr[1] = "Mary Miller " + (i + 1);
            dr[2] = "Female";
            dr[3] = "Sopra steria Pvt ltd";
            dr[4] = "Noida";
            dr[5] = "France India";
            dr[6] = "201012";
            dr[7] = "Ghaziabad";
            dr[8] = "Noida" + (i + 1);
            dt.Rows.Add(dr);
        }

        // The path to the documents directory.
        string dataDir = Path.GetFullPath(GetDataDir_Data() + @"Content\TempFiles\");

        Document document = new Document();

       
        Page page = document.Pages.Add();
        PageInfo pageInfo = document.PageInfo;
        pageInfo.Width = PageSize.PageLetter.Width;
        pageInfo.Height = PageSize.PageLetter.Height;
       

        Aspose.Pdf.MarginInfo marginInfo = pageInfo.Margin;
        marginInfo.Top = 10;
        page.PageInfo.Margin = marginInfo;

        //Create a HeaderFooter object for the section
        HeaderFooter hfFoot = new HeaderFooter();
        //Set the HeaderFooter object to odd & even footer
        page.Header = hfFoot;            
        hfFoot.Margin = new MarginInfo { Top = 2f};


        //Add a text paragraph containing current page number of total number of pages
        TextFragment t1 = new TextFragment("$p");      

        TextFragment t2 = new TextFragment("SOPRA SPARE PARTS SERVICES PRICE CATALOG");
        t2.TextState.Font = FontRepository.FindFont("Arial");
        t2.TextState.FontSize = 8;

        TextFragment t3 = new TextFragment("Last update: " + DateTime.Now.ToString("dd/MM/yyyy"));
        t3.TextState.Font = FontRepository.FindFont("Arial");
        t3.TextState.FontSize = 7;
        t3.TextState.ForegroundColor = Aspose.Pdf.Color.Blue;


        //Instantiate a table object
        var tab2 = new Table()
        {
            ColumnAdjustment = ColumnAdjustment.AutoFitToWindow,
            DefaultCellBorder = new BorderInfo(BorderSide.None)
        };

        //Add the table in paragraphs collection of the desired section
        hfFoot.Paragraphs.Add(tab2);            

        //Set with column widths of the table
        tab2.ColumnWidths = "10% 70% 30%";
        //tab2.IsKeptWithNext = false;

        //Create rows in the table and then cells in the rows
        Row row3 = tab2.Rows.Add();

        row3.Cells.Add();
        row3.Cells.Add();
        row3.Cells.Add();

        //Set the vertical allignment of the text as center alligned
        row3.Cells[0].Alignment = Aspose.Pdf.HorizontalAlignment.Left;            
        row3.Cells[1].Alignment = Aspose.Pdf.HorizontalAlignment.Center;          
        row3.Cells[2].Alignment = Aspose.Pdf.HorizontalAlignment.Right;            

        row3.Cells[0].Paragraphs.Add(t1);
        row3.Cells[1].Paragraphs.Add(t2);
        row3.Cells[2].Paragraphs.Add(t3);

        // Create Table instance
        Table table = new Table();
        table.ColumnWidths = "5 100 300 400 200 250 200 250 100";
        table.ColumnAdjustment = ColumnAdjustment.AutoFitToWindow;
        // table.DefaultCellBorder = new BorderInfo(BorderSide.All);
        // Set default cell border using BorderInfo object
        table.DefaultCellBorder = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.None, 0.0F);
        // Set table border using another customized BorderInfo object
        table.Border = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.None, 0.0F);
        table.Alignment = HorizontalAlignment.Left;
        table.HorizontalAlignment = HorizontalAlignment.Center;
        table.DefaultCellTextState.FontSize= 7;                       
       
        table.Margin.Top = 20;           

        // Set the default cell padding to the MarginInfo object
        table.DefaultCellPadding = new MarginInfo {  Top = 1f,Left = 2f,Right = 1f,Bottom = 1f};

        //Add the table in paragraphs collection of the desired section
        page.Paragraphs.Add(table);

        //Import data into the Table object from the DataTable created above
        table.ImportDataTable(dt, true, 0, 0);
       
        for (int i = 0; i < 9; i++)
        {
            table.Rows[0].Cells[i].DefaultCellTextState = new TextState("Arial", true, false);
            table.Rows[0].Cells[i].Alignment = HorizontalAlignment.Center;                         
        }

        document.Save(dataDir + "Catalog.pdf");

Thanks for your kind support.
edited_spare_catalog.pdf (107.3 KB)

@SudhanshuMani

Thanks for writing back.

Please check following code snippet, where I have generated a PDF with aligned table after modifying some lines of your code snippet. For your reference, I have attached and output as well, which was generated by below code snippet.

DataTable dt = new DataTable("Employee");
dt.Columns.Add("ID", typeof(Int32));
dt.Columns.Add("Employee Name", typeof(string));
dt.Columns.Add("Gender", typeof(string));
dt.Columns.Add("Address", typeof(string));
dt.Columns.Add("City", typeof(string));
dt.Columns.Add("Country", typeof(string));
dt.Columns.Add("Pin", typeof(string));
dt.Columns.Add("District", typeof(string));
dt.Columns.Add("Area", typeof(string));
//Add 2 rows into the DataTable object programmatically          
for (int i = 0; i < 150; i++)
{
 DataRow dr = dt.NewRow();
 dr[0] = (i + 1);
 dr[1] = "Mary Miller " + (i + 1);
 dr[2] = "Female";
 dr[3] = "Sopra steria Pvt ltd";
 dr[4] = "Noida";
 dr[5] = "France India";
 dr[6] = "201012";
 dr[7] = "Ghaziabad";
 dr[8] = "Noida" + (i + 1);
 dt.Rows.Add(dr);
}

Document document = new Document();
Page page = document.Pages.Add();
PageInfo pageInfo = document.PageInfo;
page.SetPageSize(959.76, 1357.92);

Aspose.Pdf.MarginInfo marginInfo = pageInfo.Margin;
marginInfo.Top = 60;
marginInfo.Left = 35;
marginInfo.Right = 35;
page.PageInfo.Margin = marginInfo;

//Create a HeaderFooter object for the section
HeaderFooter hfFoot = new HeaderFooter();
//Set the HeaderFooter object to odd & even footer
page.Header = hfFoot;
hfFoot.Margin = new MarginInfo { Top = 50, Left = 35, Right = 35 };


//Add a text paragraph containing current page number of total number of pages
TextFragment t1 = new TextFragment("$p");

TextFragment t2 = new TextFragment("SOPRA SPARE PARTS SERVICES PRICE CATALOG");
t2.TextState.Font = FontRepository.FindFont("Arial");
t2.TextState.FontSize = 8;

TextFragment t3 = new TextFragment("Last update: " + DateTime.Now.ToString("dd/MM/yyyy"));
t3.TextState.Font = FontRepository.FindFont("Arial");
t3.TextState.FontSize = 7;
t3.TextState.ForegroundColor = Aspose.Pdf.Color.Blue;
t3.TextState.HorizontalAlignment = HorizontalAlignment.Right;

//Instantiate a table object
var tab2 = new Table()
{
 ColumnAdjustment = ColumnAdjustment.AutoFitToWindow,
 DefaultCellBorder = new BorderInfo(BorderSide.None)
};

//Add the table in paragraphs collection of the desired section
hfFoot.Paragraphs.Add(tab2);

//Set with column widths of the table
tab2.ColumnWidths = "10% 70% 20%";
//tab2.IsKeptWithNext = false;

//Create rows in the table and then cells in the rows
Row row3 = tab2.Rows.Add();

row3.Cells.Add();
row3.Cells.Add();
row3.Cells.Add();

//Set the vertical allignment of the text as center alligned
row3.Cells[0].Alignment = Aspose.Pdf.HorizontalAlignment.Left;
row3.Cells[1].Alignment = Aspose.Pdf.HorizontalAlignment.Center;
row3.Cells[2].Alignment = Aspose.Pdf.HorizontalAlignment.Right;

row3.Cells[0].Paragraphs.Add(t1);
row3.Cells[1].Paragraphs.Add(t2);
row3.Cells[2].Paragraphs.Add(t3);

// Create Table instance
Table table = new Table();
table.ColumnWidths = "20 200 50 200 50 100 50 100 150";

// Set default cell border using BorderInfo object
table.DefaultCellBorder = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.None, 0.0F);
// Set table border using another customized BorderInfo object
table.Border = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.None, 0.0F);
table.Alignment = HorizontalAlignment.Left;
table.HorizontalAlignment = HorizontalAlignment.Center;
table.DefaultCellTextState.FontSize = 7;

table.Margin.Top = 20;

// Set the default cell padding to the MarginInfo object
table.DefaultCellPadding = new MarginInfo { Top = 1f, Left = 2f, Right = 1f, Bottom = 1f };

//Add the table in paragraphs collection of the desired section
page.Paragraphs.Add(table);

//Import data into the Table object from the DataTable created above
table.ImportDataTable(dt, true, 0, 0, dt.Rows.Count + 1, dt.Columns.Count);

for (int i = 0; i < 9; i++)
{
 table.Rows[0].Cells[i].DefaultCellTextState = new TextState("Arial", true, false);
}
document.Save(dataDir + "Catalog.pdf"); 

Catalog.pdf (63.0 KB)

In case of any further assistance, please feel free to contact us.

Hi Asad,

Thanks for your support. It is working fine and table layout is being displayed as expected.

Thanks again for your help!.

Regards,
Sudhanshu

@SudhanshuMani

Thanks for your feedback.

Please keep using our API and in case of any further assistance, please feel free to ask.