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 Apr 13, 2015 3:12 pm

Hi I have a following problem with table pagination:

On every page I have header and footer so I need to set paginate bounds so that table doesn't go over header and footer:

tableLayout.PaginateBounds = new RectangleF(0, headerHeight, page.Canvas.ClientSize.Width, page.Canvas.ClientSize.Height - headerHeight - footerHeight);

and then I draw table with:

PdfLayoutResult result = table.Draw(page, new PointF(0, y), tableLayout);

Problem appears when I need some text before table, so table doesn't start immediately after header but it starts on some y > headerHeight. In that case table is drawn immediately after header, over text so y from "table.Draw(page, new PointF(0, y), tableLayout)" is ignored.

Can this even be achieved with Spire PDF and how? If not you should seriously consider adding it because it is pretty common that table doesn't always start at the beginning of the page.

Thanks.

jbojcic
 
Posts: 23
Joined: Mon Apr 13, 2015 3:03 pm

Tue Apr 14, 2015 9:34 am

Hello,

Thanks for your inquiry.
Please try the following solution to see if it is what you want.
Code: Select all
//Create a pdf document.
            PdfDocument doc = new PdfDocument();

            //margin
            PdfUnitConvertor unitCvtr = new PdfUnitConvertor();
            PdfMargins margin = new PdfMargins();
            margin.Top = unitCvtr.ConvertUnits(2f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
            margin.Bottom = margin.Top;
            margin.Left = unitCvtr.ConvertUnits(0.5f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
            margin.Right = margin.Left;

            SetDocumentTemplate(doc, PdfPageSize.A4, margin);

            // Create one page
            PdfPageBase page = doc.Pages.Add();

            DrawPage(page);

            doc.SaveToFile("output.pdf");
            System.Diagnostics.Process.Start("output.pdf");
        }
        static void SetDocumentTemplate(PdfDocument doc, SizeF pageSize, PdfMargins margin)
        {
            PdfPageTemplateElement leftSpace = new PdfPageTemplateElement(margin.Left, pageSize.Height);
            doc.Template.Left = leftSpace;

            PdfPageTemplateElement topSpace = new PdfPageTemplateElement(pageSize.Width, margin.Top);
            topSpace.Foreground = true;
            doc.Template.Top = topSpace;

            //draw header label
            PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", 10f));
            PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Left);
            String label = "Welcome to use Spire.PDF component";
            float y = 0;
            float x = 0;
            topSpace.Graphics.DrawString(label, font1, PdfBrushes.PaleVioletRed, x, y, format);

            label = "E-mail :";
            SizeF size1 = font1.MeasureString(label, format);
            y = y + size1.Height + 3;
            PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("Arial", 10f));
            SizeF size2 = font2.MeasureString("E-mail : ", format);
            PdfTrueTypeFont font3 = new PdfTrueTypeFont(new Font("Arial", 10f));
            SizeF size3 = font3.MeasureString("Support@e-iceblue.com", format);
            float y1 = y + size3.Height - size2.Height;
            topSpace.Graphics.DrawString("E-mail : ", font2, PdfBrushes.PaleVioletRed, x, y1, format);

            x = x + size2.Width;
            topSpace.Graphics.DrawString("Support@e-iceblue.com", font3, PdfBrushes.Black, x, y, format);


            PdfPageTemplateElement rightSpace = new PdfPageTemplateElement(margin.Right, pageSize.Height);
            doc.Template.Right = rightSpace;

            PdfPageTemplateElement bottomSpace = new PdfPageTemplateElement(pageSize.Width, margin.Bottom);
            bottomSpace.Foreground = true;
            doc.Template.Bottom = bottomSpace;

            PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 9f, FontStyle.Italic));
            PdfPen pen = new PdfPen(Color.Black, 0.75f);
            y = font.Height + 1;
            bottomSpace.Graphics.SetTransparency(0.5f);
            bottomSpace.Graphics.DrawLine(pen, margin.Left, y, pageSize.Width - margin.Right, y);
            y = y + 1;
            PdfPageNumberField pageNumber = new PdfPageNumberField();
            PdfPageCountField pageCount = new PdfPageCountField();
            PdfCompositeField pageNumberLabel = new PdfCompositeField();
            pageNumberLabel.AutomaticFields = new PdfAutomaticField[] { pageNumber, pageCount };
            pageNumberLabel.Brush = PdfBrushes.Black;
            pageNumberLabel.Font = font;
            pageNumberLabel.StringFormat = format;
            pageNumberLabel.Text = "page {0} of {1}";
            pageNumberLabel.Draw(bottomSpace.Graphics, pageSize.Width -150, y);
        }
        static void DrawPage(PdfPageBase page)
        {
            PdfTable table = new PdfTable();

            string connectionStr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\test.accdb;";
            OleDbConnection conn = new OleDbConnection(connectionStr);
            OleDbCommand command = new OleDbCommand();
            command.CommandText = "select * from Table1";
            command.Connection = conn;

            using (OleDbDataAdapter dataAdapter = new OleDbDataAdapter(command))
            {
                DataTable dataTable = new DataTable();
                dataAdapter.Fill(dataTable);
                table.DataSourceType = PdfTableDataSourceType.TableDirect;
                table.DataSource = dataTable;
                PdfLayoutResult result = table.Draw(page, new PointF(0,0));
            }

If there are any questions, welcome to get it back to us.

Sincerely,
Sweety

E-iceblue support team
User avatar

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

Tue Apr 14, 2015 12:18 pm

Yes it works. I think problem with my code was that I didn't use top and bottom space for header and footer. If that's true then I was misguided by Your tutorials for header and footer in which header and footer are also drawn directly on page canvas.

Anyway thanks for Your help.

jbojcic
 
Posts: 23
Joined: Mon Apr 13, 2015 3:03 pm

Wed Apr 15, 2015 1:37 am

Hello,

Thanks for your feedback. We will correct our tutorials.
Please feel free to contact us, if you have any questions or needs. We are here for help.

Sincerely,
Sweety

E-iceblue support team
User avatar

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

Return to Spire.PDF