Hello,
// This function dynamically adds a table that spans 2 pages to an existing pdf document.
static private void AddTable(Aspose.Pdf.Document pdf, Aspose.Pdf.Text.TextFragment textFragment)
{
// local function to create cell stles for header row and data rows
Aspose.Pdf.Text.TextState GetStyle(bool bHeader, string sName, int nSize, Aspose.Pdf.HorizontalAlignment eAlign)
{
Aspose.Pdf.Text.TextState rc = new Aspose.Pdf.Text.TextState(sName, nSize);
rc.HorizontalAlignment = eAlign;
if (bHeader)
{
rc.FontStyle = Aspose.Pdf.Text.FontStyles.Bold;
}
return rc;
}
try
{
// Create the styles
Aspose.Pdf.Text.TextState tsLH = GetStyle(true, “Tahoma”, 8, Aspose.Pdf.HorizontalAlignment.Left);
Aspose.Pdf.Text.TextState tsCH = GetStyle(true, “Tahoma”, 8, Aspose.Pdf.HorizontalAlignment.Center);
Aspose.Pdf.Text.TextState tsRH = GetStyle(true, “Tahoma”, 8, Aspose.Pdf.HorizontalAlignment.Right);
Aspose.Pdf.Text.TextState tsL = GetStyle(false, "Tahoma", 9, Aspose.Pdf.HorizontalAlignment.Left);
Aspose.Pdf.Text.TextState tsC = GetStyle(false, "Tahoma", 9, Aspose.Pdf.HorizontalAlignment.Center);
Aspose.Pdf.Text.TextState tsR = GetStyle(false, "Tahoma", 9, Aspose.Pdf.HorizontalAlignment.Right);
// Create new table
Aspose.Pdf.Table table = new Aspose.Pdf.Table();
// Set column widths of the table
table.ColumnWidths = "40 220 40 40 40 60 60 45";
table.RepeatingRowsCount = 1;
table.DefaultCellPadding = new Aspose.Pdf.MarginInfo(2, 2, 2, 2);
// Set the border for table cells
table.DefaultCellBorder = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.Bottom, 1.0f, Aspose.Pdf.Color.FromRgb(System.Drawing.Color.DarkGray));
// Add the header row
Aspose.Pdf.Row oRow = table.Rows.Add();
oRow.DefaultCellBorder = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.Bottom | Aspose.Pdf.BorderSide.Top, 1.0f, Aspose.Pdf.Color.FromRgb(System.Drawing.Color.DarkGray));
oRow.BackgroundColor = Aspose.Pdf.Color.FromArgb(231, 231, 231);
// Stuff the header cells
oRow.Cells.Add("SIC", tsCH);
oRow.Cells.Add("Business Description", tsLH);
oRow.Cells.Add("Sales", tsRH);
oRow.Cells.Add("DE", tsRH);
oRow.Cells.Add("Price", tsRH);
oRow.Cells.Add("Price/Sales", tsRH);
oRow.Cells.Add("Price/DE", tsRH);
oRow.Cells.Add("State(s)", tsRH);
// Add enough rows to the table to need 2 pages
int iRow;
string strRow = "";
for (iRow = 0; iRow <= 50; iRow++)
{
strRow = iRow.ToString();
oRow = table.Rows.Add();
oRow.Cells.Add("SIC " + strRow, tsC); // H_SIC
oRow.Cells.Add("Text description " + strRow, tsL); // H_Business_Description
oRow.Cells.Add("$" + strRow, tsR); // I1_Gross_Sales
oRow.Cells.Add("$" + strRow, tsR); // I1_EBT
oRow.Cells.Add("$" + strRow, tsR); // T_Selling_Price
oRow.Cells.Add(strRow + ".23", tsR); // PriceToSales
oRow.Cells.Add(strRow + ".54", tsR); // PriceToEBT
oRow.Cells.Add("CO", tsR); // H_StateCode
}
// Remove the table anchor text
textFragment.Text = "";
// Add the table
textFragment.Page.Paragraphs.Add(table);
// When the resulting file is opened in Acrobat
// 1. Last page is portrait instead of landscape
// 2. Table is not at the top of the last page
// 3. Footer on last page is missing
// 4. Acrobat says last page has an error
}
catch (Exception ex)
{
}
}
// This function just
// 1. Opens the source file (pdfSource.pdf)
// 2. Finds where to add the table
// 3. Calls AddTable to add the table
// 4. Save the modified doc into the result file (pdfResult.pdf)
private void btnTest_Click(object sender, EventArgs e)
{
try
{
string sSource = Path.Combine(Application.StartupPath, “pdfSource.pdf”);
string sResult = Path.Combine(Application.StartupPath, “pdfResult.pdf”);
Aspose.Pdf.Document pdf = new Aspose.Pdf.Document(sSource);
Aspose.Pdf.Text.TextFragmentAbsorber textFragmentAbsorber = new Aspose.Pdf.Text.TextFragmentAbsorber("SAMPLE_COMPS_TABLE");
Aspose.Pdf.Text.TextSearchOptions textSearchOptions = new Aspose.Pdf.Text.TextSearchOptions(true);
textFragmentAbsorber.TextSearchOptions = textSearchOptions;
pdf.Pages.Accept(textFragmentAbsorber);
Aspose.Pdf.Text.TextFragmentCollection textFragmentCollection = textFragmentAbsorber.TextFragments;
// Finds just 1 fragments
foreach (Aspose.Pdf.Text.TextFragment textFragment in textFragmentCollection)
{
AddTable(pdf, textFragment);
}
pdf.Save(sResult);
pdf = null;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}