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.

Mon Dec 27, 2021 2:45 am

I have a table that when it is too long to fit on a single page, it automatically creates an additional page.

The issue is that the margins of the first page don't carry over to the next page. The result is that the table continues on the second page at the very top without any margin.

I'm using version FreeSpire.PDF version 7.8.9

Any help would be very much appreciated.
Thanks!

yyp
 
Posts: 14
Joined: Thu Jun 15, 2017 10:54 pm

Mon Dec 27, 2021 8:10 am

Hello,

Thank you for your inquiry.
I used the following code to do an initial test and found that the margin of each page of the result PDF file is consistent with the first page. Please try the following code. If it cannot meet your needs, please provide your complete test code and input PDF file (if any). You could attach them here or send them to us via email (support@e-iceblue.com). Thanks in advance.
Code: Select all
 //Create a pdf document
 PdfDocument doc = new PdfDocument();
 //Set the margin
 PdfMargins margin = new PdfMargins(40,40);
 //Add page
 PdfPageBase page = doc.Pages.Add(PdfPageSize.A4,margin);
 
 //Create a pdf table
 PdfTable table = new PdfTable();

 //Set data source of the pdf table
 table. DataSource = dataSource;

 //Set the color of table border
 PdfTableStyle style = new PdfTableStyle();
 style. CellPadding = 2;
 style. BorderPen = new PdfPen(Color. Gray, 1f);
 table. Style = style;

 //Draw the pdf table into pdf document
 table. Draw(page, new PointF(0, 0));

 //Save pdf document
 doc.SaveToFile(resultFile);

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1651
Joined: Wed Apr 07, 2021 2:50 am

Mon Dec 27, 2021 4:35 pm

I'm using an existing PDF
Code: Select all
doc.LoadFromFile


Is there a way to load the PDF from file and specify margins.

yyp
 
Posts: 14
Joined: Thu Jun 15, 2017 10:54 pm

Tue Dec 28, 2021 1:58 am

Hello,

Thank you for your feedback.
Our Spire.PDF supports resetting the page size of PDF files. The sample code is as follows.
Code: Select all
//Create a pdf document
PdfDocument doc = new PdfDocument();
doc.LoadFromFile(inputFile);
PdfPageBase page1 = doc.Pages[0];                   
//Create a pdf table
PdfTable table = new PdfTable();

//Set data source of the pdf table
table.DataSource = dataSource;

//Set the color of table border
PdfTableStyle style = new PdfTableStyle();
style.CellPadding = 2;
style.BorderPen = new PdfPen(Color.Gray, 1f);
table.Style = style;
//Draw the pdf table into pdf document
table.Draw(page1, new PointF(70, 320));

//Reset the page size
using (PdfDocument newDoc = new PdfDocument())
{
    float scale = 0.9f;
    for (int i = 0; i < doc.Pages.Count; i++)
    {
        PdfPageBase page = doc.Pages[i];
       
        float width = page.Size.Width  ;
        float height = page.Size.Height ;
        PdfMargins margin;
        PdfPageBase newPage;
        if (i == 0)
        {
            margin = new PdfMargins(40,20,40,0);
            //Add new page with expected width and height
            newPage = newDoc.Pages.Add(new SizeF(width, height), margin);
        }
        else
        {
            margin = new PdfMargins(40, 40);
            //Add new page with expected width and height
            newPage = newDoc.Pages.Add(new SizeF(width, height), margin);
        }
        newPage.Canvas.ScaleTransform(scale, scale);

        //Copy content of original page into new page
        newPage.Canvas.DrawTemplate(page.CreateTemplate(), PointF.Empty);
    }
    //save the document
    newDoc.SaveToFile(output);
}

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1651
Joined: Wed Apr 07, 2021 2:50 am

Tue Dec 28, 2021 5:59 pm

What you wrote doesn't work for my use case because there are other things on the page that can't be scaled down. It's not just the table.

Here is my use case:

I have an existing PDF with a letterhead.
My goal is to to open the existing file, add a letter with several paragraphs, add a table of data.
Save it as another file.

Issue with adding the table in this way is that on the second page no margins are used.
Issue with doing it as you suggested, is that the letterhead and letter also get scaled down, not just the table.

I tried a third approach. Create a new doc and page with margins and use DrawTemplate to copy the letterhead from the pageTemplate to the page I'm using.
And then write my letter and table.
Code: Select all
page.Canvas.DrawTemplate(pageTemplate.CreateTemplate, PointF.Empty)

This worked for the letter and table, but the margins are affecting the letterhead.

Here is the code snippet I'm using for this.
Code: Select all
        Dim doc As New PdfDocument
        Dim page As PdfPageBase

        Dim docTemplate As New PdfDocument
        Dim pageTemplate As PdfPageBase

        Dim m As New PdfMargins
        m.Top = 36
        m.Bottom = 36
        m.Left = 36
        m.Right = 36

        page = doc.Pages.Add(New SizeF(PageWidth, PageHeight), m)

        'get template
        docTemplate.LoadFromFile(Template)
        pageTemplate = docTemplate.Pages(0)

        'copy letterhead to page
        page.Canvas.DrawTemplate(pageTemplate.CreateTemplate, PointF.Empty)

        'code proceeds to write the letter and insert the table...


So the question now is: is there a way to use DrawTemplate to copy the entire contents from one page to another and not get affected by the margins?
If there isn't a way, what is the correct way of accomplishing what I'm trying to achieve.

Thanks again for the help.

yyp
 
Posts: 14
Joined: Thu Jun 15, 2017 10:54 pm

Wed Dec 29, 2021 6:07 am

Hello,

Thank you for your feedback.
Using the method of resetting the PDF file size cannot achieve your needs. Our Spire.PDF provides the Grid method to draw the table, the sample code is as follows, this code can meet your needs.
Code: Select all
Dim doc As PdfDocument = New PdfDocument
doc.LoadFromFile(inputFile)
Dim page As PdfPageBase = doc.Pages(0)
Dim grid As PdfGrid = New PdfGrid
grid.Style.CellPadding = New PdfPaddings(1, 1, 1, 1)
Dim header() As String = data(0).Split(";"c)
grid.Columns.Add(header.Length)
Dim width As Single = (page.Canvas.ClientSize.Width  _
            - (grid.Columns. Count + 1))
grid.Columns(0).Width = width * 0.15f
grid.Columns(1).Width = width * 0.15f
grid.Columns(2).Width = width * 0.20f
grid.Columns(3).Width = width * 0.10f
grid.Columns(4).Width = width * 0.15f
Dim headerRow As PdfGridRow = grid.Headers.Add(1)(0)
'Set the row height
headerRow.Height = 15f
Dim i As Integer = 0
Do While (i < header.Length)
    headerRow.Cells(i).Value = header(i)
    headerRow.Cells(i).StringFormat = New PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle)
    i = (i + 1)
Loop

Dim margins As Single = doc.PageSettings.Margins.Bottom
Dim firstPageRowCount As Integer = CInt((page.Size.Height - margins - 320) / 15)
'Draw firstPageRowCount lines on the first page
Dim i As Integer = 1
Do While (i < firstPageRowCount)
    Dim row As PdfGridRow = grid.Rows.Add
    'Set the row height.
    row. Height = 15f
    Dim rowData() As String = data(i).Split(Microsoft.VisualBasic.ChrW(59))
    Dim c As Integer = 0
    Do While (c < rowData.Length)
        row.Cells(c).Value = rowData(c)
        row.Cells(c).StringFormat = New PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle)
        c = (c + 1)
    Loop
     
    i = (i + 1)
Loop

grid. Draw(page, New PointF(40, 320))
Dim pageHeight As Single = page.Size.Height
Dim pageWidth As Single = page.Size.Width
'Clear the data of the grid
grid.Rows.Clear
grid.Headers.Clear
'Add new page and set margin
page = doc.Pages.Add(New SizeF(pageWidth, pageHeight), New PdfMargins(36))
'Draw the grid from row firstPageRowCount
Dim i As Integer = firstPageRowCount
Do While (i < data.Length)
    Dim row As PdfGridRow = grid.Rows.Add
    'Set the row height
    row. Height = 15f
    Dim rowData() As String = data(i).Split(";"c)
    Dim c As Integer = 0
    Do While (c < rowData.Length)
        row.Cells(c).Value = rowData(c)
        row.Cells(c).StringFormat = New PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle)
        c = (c + 1)
    Loop
     
    i = (i + 1)
Loop
grid. Draw(page, New PointF(0, 0))

doc.SaveToFile(output)
Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1651
Joined: Wed Apr 07, 2021 2:50 am

Thu Dec 30, 2021 3:59 am

Grid works. It's a lot of code to populate but if this is the only way, it does the job...
I'm also concerned about performance. Does the grid work as fast as the table or is the same thing behind the scenes just with more flexibility?

The sample code you shared obviously doesn't handle a situation where the grid will span a third (and 4th, ...) page.
Can you share a snippet that can add pages in a loop based on the number of rows and row height of the grid?

Thinking of another possibility, back to using a table.
Is there perhaps an event that is triggered when a new page is created in a document. If yes, would I be able to handle that event and add margin to the new page that is created before it's created?
Would this work with a Table.
I'm going back to Table because it's so much easier to work with that a grid.


Thanks!

yyp
 
Posts: 14
Joined: Thu Jun 15, 2017 10:54 pm

Thu Dec 30, 2021 9:04 am

Hello,

Thank you for your feedback.
1) The performance of Grid is better than that of Table, and Gird can get or set more attributes.
2) The code I provided last time supports the table to span to the third (and fourth) page, because the table will automatically spread from the second page and retain the margins. I have attached my complete test code and result file.
3) The event you mentioned (creating a new page trigger event) is not included in our Spire.Pdf.
In summary, for your needs, I suggest you use Grid. This makes it easier to achieve your needs.

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1651
Joined: Wed Apr 07, 2021 2:50 am

Return to Spire.PDF