Dear Aspose Support Team,
I hope this message finds you well. I am currently evaluating the Aspose PDF library for a project and I would like to clarify a few aspects before proceeding with the paid version.
Project Requirement:
- I need to generate a PDF where the header and footer remain fixed in height and width on every page.
- In between the header and footer, dynamic content will be displayed (products with name, price, quantity) and, at the end, the Terms & Conditions (T&C) section.
- If the content exceeds the available space between the header and footer on a page, it should automatically flow onto the next page, preserving the fixed header and footer on subsequent pages.
I have been trying to achieve this behavior, but I am facing issues with the following:
- The header and footer are not consistently appearing on all pages when the content overflows.
- The content flow is not behaving as expected when it exceeds the page, resulting in inconsistent formatting.
Before I proceed with the paid version, I would like to confirm whether Aspose PDF can handle this type of content overflow and ensure the fixed header and footer appear correctly on every page.
Could you please confirm if Aspose PDF supports this behavior and guide me on how to implement it? Any sample code or further details would be much appreciated.
Looking forward to your response.
Sample code:-
using Aspose.Pdf;
using Aspose.Pdf.Text;
using System;
using System.IO;
public class AsposePdfGenerator
{
public static void GenerateAsposePdf(string outputFilePath, string header, string footer)
{
// Create a new PDF document
Document pdfDocument = new Document();
// Define header and footer heights
float headerHeight = 50;
float footerHeight = 50;
// Add first page
Page page = pdfDocument.Pages.Add();
AddHeaderAndFooter(page, header, footer, headerHeight, footerHeight);
// Add product list and T&C
//instead of hardcoding we have to do dymamicaly based on page size
AddProductsAndTandC(page, headerHeight, footerHeight, pdfDocument);
// Save the document
pdfDocument.Save(outputFilePath);
Console.WriteLine(Path.GetFullPath(outputFilePath));
}
// Method to add header and footer to the page
static void AddHeaderAndFooter(Page page, string header, string footer, float headerHeight, float footerHeight)
{
// Header text
TextFragment headerText = new TextFragment(header);
headerText.TextState.FontSize = 14;
headerText.Position = new Position(200, page.PageInfo.Height - headerHeight + 10); // Position header
page.Paragraphs.Add(headerText);
// Footer text
TextFragment footerText = new TextFragment(footer);
footerText.TextState.FontSize = 10;
footerText.Position = new Position(200, footerHeight - 20); // Position footer
page.Paragraphs.Add(footerText);
}
// Method to add products and Terms and Conditions
static void AddProductsAndTandC(Page page, float headerHeight, float footerHeight, Document pdfDocument)
{
// Create a table for product data
Table productTable = new Table();
productTable.ColumnWidths = "200 100 100"; // Column widths for Product, Price, Quantity
productTable.DefaultCellTextState.FontSize = 12;
// Add table headers
Row headerRow = productTable.Rows.Add();
headerRow.Cells.Add("Product Name");
headerRow.Cells.Add("Price");
headerRow.Cells.Add("Quantity");
//instead of hardcoding we have to do dymamicaly based on page size
for (int i = 0; i < 15; i++)
{
Row row = productTable.Rows.Add();
row.Cells.Add("Product " + (i + 1).ToString());
row.Cells.Add("$" + ((i + 1) * 10));
row.Cells.Add((i + 1).ToString());
}
// Add the product table to the page
page.Paragraphs.Add(productTable);
// Now check if there's enough space left on the page for T&C
float availableHeight = (float)(page.PageInfo.Height - headerHeight - footerHeight - productTable.GetHeight());
// Check if T&C text can fit on the current page
if (availableHeight < 50) // Assuming T&C requires at least 50 units of height
{
// Create a new page if there is not enough space
page = pdfDocument.Pages.Add();
page.PageInfo.Width = 595; // A4 width in points
page.PageInfo.Height = 842; // A4 height in points
// Re-add header and footer for the new page
AddHeaderAndFooter(page, "Header Text for New Page", "Footer Text for New Page", headerHeight, footerHeight);
}
// Add Terms and Conditions (T&C) text
string termsText = "Terms and Conditions: These are the terms and conditions... \n" +
"You agree to follow the rules. Product warranty is limited to 30 days, etc.";
TextFragment termsAndConditions = new TextFragment(termsText);
termsAndConditions.TextState.FontSize = 10;
termsAndConditions.Position = new Position(50, page.PageInfo.Height - footerHeight - 80); // Position it just above the footer
page.Paragraphs.Add(termsAndConditions);
}
}