Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Wed Mar 26, 2025 3:33 pm

I am creating a multi-page PDF. I have tried both of the following options below and neither seems to work correctly:

Original Page Creation:

Code: Select all
PdfDocument pdf = new PdfDocument();
PdfPageBase page = pdf.Pages.Add(PdfPageSize.A4, new PdfMargins(10, 10, 10, 10));


Last line saved to this page:

Code: Select all
grid2.Draw(page, new Point(12, 160));


To start the next page I tried:

Code: Select all
pdf.Pages.Add();


and

Code: Select all
page = pdf.Pages.Insert(1,PdfPageSize.A4, new PdfMargins(10, 10, 10, 10));


Neither worked. What is the correct way to add a page after the current page?

tim8w123
 
Posts: 18
Joined: Wed Feb 22, 2023 3:55 pm

Thu Mar 27, 2025 1:54 am

Hello,

Thanks for your inquiry.
I made a demo based on your description and used pdf.Pages.Add() to add a new PDF page, but I did not reproduce the issue you mentioned. I attached the complete test code and result file for your reference. Please run the test code directly and check whether the generated PDF contains two pages. Looking forward to your feedback. Also, if there is any misunderstanding, please write back and tell us more details about your problem, or provide us with a code that can reproduce your problem. Thanks in advance.
Code: Select all
//Create a PdfDocument object
PdfDocument doc = new PdfDocument();
//Add a page
PdfPageBase page = doc.Pages.Add(PdfPageSize.A4, new PdfMargins(10));
//Create a PdfGrid
PdfGrid grid = new PdfGrid();
//Set cell padding
grid.Style.CellPadding = new PdfPaddings(1, 1, 1, 1);
//Set font
grid.Style.Font = new PdfTrueTypeFont(new Font("Times New Roman", 13f, FontStyle.Regular), true);
//Add rows
PdfGridRow row1 = grid.Rows.Add();
PdfGridRow row2 = grid.Rows.Add();
PdfGridRow row3 = grid.Rows.Add();
PdfGridRow row4 = grid.Rows.Add();
grid.Columns.Add(4);
//Set column width
foreach (PdfGridColumn col in grid.Columns)
{
    col.Width = 110f;
}
//Write data into specific cells
row1.Cells[0].Value = "Order and Payment Status";
row2.Cells[0].Value = "Order number";
row2.Cells[1].Value = "Date";
row2.Cells[2].Value = "Customer";
row2.Cells[3].Value = "Paid or not";
row3.Cells[0].Value = "00223";
row3.Cells[1].Value = "2022/06/02";
row3.Cells[2].Value = "Brick Lane Realty";
row3.Cells[3].Value = "Yes";
row4.Cells[0].Value = "00224";
row4.Cells[1].Value = "2022/06/03";
row4.Cells[3].Value = "No";
//Span cell across columns
row1.Cells[0].ColumnSpan = 4;
//Span cell across rows
row3.Cells[2].RowSpan = 2;
//Set text alignment of specific cells
row1.Cells[0].StringFormat = new PdfStringFormat(PdfTextAlignment.Center);
row3.Cells[2].StringFormat = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle);
//Set background color of specific cells
row1.Cells[0].Style.BackgroundBrush = PdfBrushes.Orange;
row4.Cells[3].Style.BackgroundBrush = PdfBrushes.LightGray;
//Format cell border
PdfBorders borders = new PdfBorders();
borders.All = new PdfPen(Color.Orange, 0.8f);
foreach (PdfGridRow pgr in grid.Rows)
{
    foreach (PdfGridCell pgc in pgr.Cells)
    {
        pgc.Style.Borders = borders;
    }
}
//Draw table on the page
grid.Draw(page, new PointF(12, 160));
// Add a new Page
doc.Pages.Add();
//Save the document to a PDF file
doc.SaveToFile("PdfGrid.pdf");


Sincerely,
William
E-iceblue support team
User avatar

William.Zhang
 
Posts: 757
Joined: Mon Dec 27, 2021 2:23 am

Thu Mar 27, 2025 2:10 pm

Yes, I get the 2nd page. But when I try and write on the 2nd page, it writes it on top of the first page. Is the positioning not reset for each page? i.e. do I need to continually increase the draw position as I go from page to page?

Here's how I am going to the next page based on your example:
Code: Select all
pdf.Pages.Add();
page = pdf.Pages[iPages-1];


My last Draw position on Page1 was

Code: Select all
grid2.Draw(page, new Point(12, 160));


Do I need the first draw on Page 2 to be the Height of Page 1 + an amount? i.e

Code: Select all
grid2.Draw(page, new Point(12, page.Canvas.ClientSize.Height + 160));

tim8w123
 
Posts: 18
Joined: Wed Feb 22, 2023 3:55 pm

Fri Mar 28, 2025 2:07 am

Hello,

Thanks for your reply.
Please note that pdf.Pages.Add() will return a PdfPageBase object, and you can just draw the content in this PdfPageBase. I have attached my latest code for your reference, the key is in the last 3 lines.
Code: Select all
//Create a PdfDocument object
PdfDocument doc = new PdfDocument();
//Add a page
PdfPageBase page = doc.Pages.Add(PdfPageSize.A4, new PdfMargins(10));
//Create a PdfGrid
PdfGrid grid = new PdfGrid();
//Set cell padding
grid.Style.CellPadding = new PdfPaddings(1, 1, 1, 1);
//Set font
grid.Style.Font = new PdfTrueTypeFont(new Font("Times New Roman", 13f, FontStyle.Regular), true);
//Add rows
PdfGridRow row1 = grid.Rows.Add();
PdfGridRow row2 = grid.Rows.Add();
PdfGridRow row3 = grid.Rows.Add();
PdfGridRow row4 = grid.Rows.Add();
grid.Columns.Add(4);
//Set column width
foreach (PdfGridColumn col in grid.Columns)
{
    col.Width = 110f;
}
//Write data into specific cells
row1.Cells[0].Value = "Order and Payment Status";
row2.Cells[0].Value = "Order number";
row2.Cells[1].Value = "Date";
row2.Cells[2].Value = "Customer";
row2.Cells[3].Value = "Paid or not";
row3.Cells[0].Value = "00223";
row3.Cells[1].Value = "2022/06/02";
row3.Cells[2].Value = "Brick Lane Realty";
row3.Cells[3].Value = "Yes";
row4.Cells[0].Value = "00224";
row4.Cells[1].Value = "2022/06/03";
row4.Cells[3].Value = "No";
//Span cell across columns
row1.Cells[0].ColumnSpan = 4;
//Span cell across rows
row3.Cells[2].RowSpan = 2;
//Set text alignment of specific cells
row1.Cells[0].StringFormat = new PdfStringFormat(PdfTextAlignment.Center);
row3.Cells[2].StringFormat = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle);
//Set background color of specific cells
row1.Cells[0].Style.BackgroundBrush = PdfBrushes.Orange;
row4.Cells[3].Style.BackgroundBrush = PdfBrushes.LightGray;
//Format cell border
PdfBorders borders = new PdfBorders();
borders.All = new PdfPen(Color.Orange, 0.8f);
foreach (PdfGridRow pgr in grid.Rows)
{
    foreach (PdfGridCell pgc in pgr.Cells)
    {
        pgc.Style.Borders = borders;
    }
}
//Draw table on the page
grid.Draw(page, new PointF(12, 160));

// Add a new Page
PdfPageBase newPage = doc.Pages.Add();
grid.Draw(newPage, new PointF(12, 160));

//Save the document to a PDF file
doc.SaveToFile("AddNewPage.pdf");


Sincerely,
William
E-iceblue support team
User avatar

William.Zhang
 
Posts: 757
Joined: Mon Dec 27, 2021 2:23 am

Return to Spire.PDF