How to add rows and pages in the PDF

Hi

I see in Aspose.Pdf docs, is there some code that allows rows and pages to be created from a loop? I see that the all examples are hardcoded.

Hi Junmil,

Thanks for contacting support.

Aspose.Pdf offers the features to create dynamic PDF files and you can add rows, pages and other PDF objects at run time. You can also bind a datasource and generate PDF files based on contents being retrieved from database.

[C#]

// Instantiate the Document object by calling its empty constructor
Document doc = new Document();
Page page = doc.Pages.Add();

// Instantiate a table object
Aspose.Pdf.Table tab1 = new Aspose.Pdf.Table();

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

// Set column widths of the table
tab1.ColumnWidths = "50 50 50";

// Set default cell border using BorderInfo object
tab1.DefaultCellBorder = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, 0.1F);

// Set table border using another customized BorderInfo object
tab1.Border = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, 1F);

// Create MarginInfo object and set its left, bottom, right, and top margins
Aspose.Pdf.MarginInfo margin = new Aspose.Pdf.MarginInfo
{
    Top = 5f,
    Left = 5f,
    Right = 5f,
    Bottom = 5f
};

// Set the default cell padding to the MarginInfo object
tab1.DefaultCellPadding = margin;

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

// Add a cell with a large text string
row1.Cells.Add();
TextFragment myText = new TextFragment("col3 with large text string");
row1.Cells[2].Paragraphs.Add(myText);
row1.Cells[2].IsWordWrapped = false;

// Add multiple rows dynamically
for (int counter = 0; counter <= 100; counter++)
{
    Aspose.Pdf.Row row2 = tab1.Rows.Add();
    row2.Cells.Add("item1");
    row2.Cells.Add("item2");
    row2.Cells.Add("item3");
}

// Save the PDF
doc.Save("c:/pdftest/DynamicPDFfile.pdf");

Hi Junmil,

Thanks for your inquriy. Please check following documentation link to create table rows/cells in a loop. You can create pages and table/rows/cells in loop using Aspose.Pdf. In case you find any issue then please share the details so we will guide you accordingly.

We are sorry for the inconvenience caused.

Best Regards,