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.

Thu Aug 20, 2015 1:44 pm

Hi,

Currently I have a PdfGrid that is created that gets data from a 2D string array. The string array is filled using data from a data grid view which my users can enter data into. Currently if I enter lots of information into the data grid view, then the table goes over to the next page fine, but then the data that needs to get placed underneath it ends up on top of the grid on the top of my PDF. Below is what I currently have drawing the grid and getting its height for the next object:
Code: Select all
result = itemGrid.Draw(page, new PointF(0, y));
y = y + result.Bounds.Height + 10;

But the height is now the height of the one row that made its way onto the second page. What I need to know is how to tell my next few elements whether or not the result has ended up onto the next page, and then how to draw my next objects below it.

I also have another problem where if the grid is only just on one page, then my content is getting draw behind my footer. My header and footer is created using the PdfPageTemplateElement, but for some reason instead of my next content going onto the next page it is just getting draw behind the footer template.

Any help would be appreciated, thanks.

Luke Bennett
 
Posts: 7
Joined: Thu Aug 20, 2015 1:02 pm

Fri Aug 21, 2015 8:25 am

Hello,

Thanks for your inquiry.
1.After you draw the Grid, you could get pages. Then draw your next objects according to the page number.
Here is the code for your reference.
Code: Select all
 PdfLayoutResult result= grid.Draw(page, new PointF(10, y));
            y = y + result.Bounds.Height + 10;
            PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", 10f));
            int count = doc.Pages.Count;
            PdfPageBase page1 = doc.Pages[count- 1];
            page1.Canvas.DrawString("hello world",font1,PdfBrushes.Red,10,y);   

2. I recommend you to judge the position before you draw the content.
Here is the code for your reference.
Code: Select all
  namespace pdfTest
{
    public partial class Form1 : Form
    {   
       public static float TemplateTopHeight, TemplateBottomHeight;

         private void button3_Click(object sender, EventArgs e)
        {   //...
              SetDocumentTemplate(doc, PdfPageSize.A4, margin);   
               PdfPageBase page = doc.Pages.Add();
               PdfGrid grid = new PdfGrid();
            float y = 70;
            grid.Columns.Add(2);
            for (int i = 0; i < 11; i++)
            {
                PdfGridRow row = grid.Rows.Add();
                row.Height = 51f;
            }
            PdfLayoutResult result = grid.Draw(page, new PointF(10, y));
            //int count = doc.Pages.Count;
            //page = doc.Pages[count - 1];
            string label = "hello world";         
            PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", 10f));
            y = y + result.Bounds.Height + 10;
            float height = font1.MeasureString(label).Height;
            float bottom = doc.PageSettings.Size.Height - doc.PageSettings.Margins.Top - doc.PageSettings.Margins.Bottom - height - TemplateTopHeight - TemplateBottomHeight;
            if (y > bottom)
            {
                page = doc.Pages.Add();

                page.Canvas.DrawString(label, font1, PdfBrushes.Red, 10, y - bottom);
            }         
            else
            {
                page.Canvas.DrawString(label, font1, PdfBrushes.Red, 10, y);
            }
           
        }
          static void SetDocumentTemplate(PdfDocument doc, SizeF pageSize, PdfMargins margin)
        {
          //...
           TemplateTopHeight = topSpace.Bounds.Bottom;
           TemplateBottomHeight = bottomSpace.Bounds.Bottom;
         // ...
             

         }   
   


     }
}


Best Regards,
Sweety

E-iceblue support team
User avatar

sweety1
 
Posts: 539
Joined: Wed Mar 11, 2015 1:14 am

Mon Aug 24, 2015 8:11 am

Hi Sweety, thanks for the reply.

The first section is perfect, can't believe I didn't think of that before...

And for the second bit, what is this line:

Code: Select all
 float bottom = doc.PageSettings.Size.Height - doc.PageSettings.Margins.Top - doc.PageSettings.Margins.Bottom - height - TemplateTopHeight - TemplateBottomHeight;


going to calculate? Won't the margin top & margin bottom also be apart of the template top & bottom heights?

Luke Bennett
 
Posts: 7
Joined: Thu Aug 20, 2015 1:02 pm

Mon Aug 24, 2015 9:35 am

Hello,

Thanks for your reply.
Yes, this code going to calculate the position. The margin top & margin bottom also be apart of the template top & bottom heights.

Best Regards,
Sweety

E-iceblue support team
User avatar

sweety1
 
Posts: 539
Joined: Wed Mar 11, 2015 1:14 am

Mon Aug 24, 2015 9:52 am

Okay thanks.

I've implemented getting the page using the count, but whenever I draw anything on to the second page, I get the error "An error exists on this page. Acrobat may not display the page correctly. Please contact the person who created the PDF document to correct the problem." and the page no longer has any margins or a header/footer.

If I remove my DrawStrings and just let the grid go over to page two then the header and footer and all the margins are shown correctly, it only breaks when I begin to add more elements to the page.

Any ideas why this would happen?

Thanks,
Luke

Luke Bennett
 
Posts: 7
Joined: Thu Aug 20, 2015 1:02 pm

Tue Aug 25, 2015 7:17 am

Hello,

Thanks for your reply.
Could you please share your sample code with us?
It would be helpful to replicate the issue and work out the solution for you ASAP.

Best Regards,
Sweety

E-iceblue support team
User avatar

sweety1
 
Posts: 539
Joined: Wed Mar 11, 2015 1:14 am

Tue Aug 25, 2015 8:28 am

Hi,

Would you like it emailed to your support email or just attach the file here?

Thanks,
Luke

Luke Bennett
 
Posts: 7
Joined: Thu Aug 20, 2015 1:02 pm

Tue Aug 25, 2015 9:28 am

Hello,

Thanks for your reply.
You can attach the file here.
Or if the information is confidential, you can send it to us ( Support@e-iceblue.com ) via email.

Best Regards,
Sweety

E-iceblue support team
User avatar

sweety1
 
Posts: 539
Joined: Wed Mar 11, 2015 1:14 am

Tue Aug 25, 2015 9:36 am

Hi,

I have sent an email to the support address.

Thanks,
Luke

Luke Bennett
 
Posts: 7
Joined: Thu Aug 20, 2015 1:02 pm

Thu Aug 27, 2015 10:16 am

Hi,

Attached is my shelled out solution with 3 PDFs. The solution needs the free Spire.PDF 3.2 version re-adding via Nuget as with it the size was above the maximum upload.

The first one is my PDF that doesn’t spread over 2 pages, which is working fine.
The second one creates the PDF to the point of the grid which shows that my template is working fine on the second page before I add any other content.
The third PDF has an error when I open the second page, and loses the header and footer. This only happens once that extra bottom section of the PDF is generated.

Hopefully this will help you diagnose the problem.
Last edited by Luke Bennett on Fri Aug 28, 2015 7:59 am, edited 1 time in total.

Luke Bennett
 
Posts: 7
Joined: Thu Aug 20, 2015 1:02 pm

Fri Aug 28, 2015 6:05 am

Hello,

Thanks for your information.
I have tested your issue with the latest version of spire.pdf ( Spire.PDF Pack Version:3.5 ). It worked well in our side. Please download it and have a try.

Best Regards,
Sweety

E-iceblue support team
User avatar

sweety1
 
Posts: 539
Joined: Wed Mar 11, 2015 1:14 am

Fri Aug 28, 2015 8:03 am

Hi,

Okay perfect, thanks for your help! :)

Luke

Luke Bennett
 
Posts: 7
Joined: Thu Aug 20, 2015 1:02 pm

Fri Aug 28, 2015 8:56 am

Hello,

Please feel free to contact us, if you have any questions or needs. We are here for help.

Best Regards,
Sweety
E-iceblue support team
User avatar

sweety1
 
Posts: 539
Joined: Wed Mar 11, 2015 1:14 am

Return to Spire.PDF