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)