在页面底部添加表格Aspose

问题描述

我使用 Aspose.PDF 在我的页面添加表格。但是我需要在页面底部和水平中心添加一个表格。 我使用 table.ColumnAdjustment = ColumnAdjustment.AutoFitToWindow; 将表格添加到水平中心, 但我无法获得桌子的高度,也无法公开 table.Margin。我如何找到桌子高度?或者如何将我的表格添加底部

解决方法

请尝试使用以下代码在 PDF 页面底部添加表格。您可以使用 HeaderFooter Class 和 Page.Footer Property 来满足您的要求。

// Instantiate Document instance by calling an empty constructor
Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document();
// Create a page in the pdf document
Aspose.Pdf.Page page = pdfDocument.Pages.Add();

// Create a Header Section of the PDF file
Aspose.Pdf.HeaderFooter footer = new Aspose.Pdf.HeaderFooter();
// Set the Odd Header for the PDF file
page.Footer = footer;
// Set the top margin for the header section
footer.Margin.Top = 20;

// Instantiate a table object
Aspose.Pdf.Table tab1 = new Aspose.Pdf.Table();
// Add the table in paragraphs collection of the desired section
footer.Paragraphs.Add(tab1);
// Set default cell border using BorderInfo object
tab1.DefaultCellBorder = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All,0.1F);
tab1.HorizontalAlignment = HorizontalAlignment.Center;
// Set with column widths of the table
tab1.ColumnWidths = "100%";

// Create rows in the table and then cells in the rows
Aspose.Pdf.Row row1 = tab1.Rows.Add();

row1.Cells.Add("Table in Footer Section");
row1.BackgroundColor = Color.Gray;
// Set the row span value for first row as 2
tab1.Rows[0].Cells[0].Alignment = HorizontalAlignment.Center;
tab1.Rows[0].Cells[0].DefaultCellTextState.ForegroundColor = Color.Cyan;
tab1.Rows[0].Cells[0].DefaultCellTextState.Font = FontRepository.FindFont("Helvetica");
           
// Save the Pdf file
pdfDocument.Save(dataDir + "TableInFooterSection_out.pdf");

为了获取表格的高度,可以使用GetHeight()类的Table方法。例如,在上面的代码示例中 - 您可以使用 double height = tab1.GetHeight();。如果您在满足要求的过程中遇到任何问题,请在 Aspose.PDF official support forum 中创建一个主题,以便相应地解决您的疑虑。我是 Asad Ali,我在 Aspose 担任开发布道师。