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.

Tue Apr 14, 2015 6:14 pm

Hi
Maybe somebody has some idea how to deal with the following situation...
Inside my solution I do use the PDF component to create some pdfs on the fly insiode my code. There are some text and grid elements over more than one page.
The problem I have now is the footer I should create (on every page of the doc with mutliple information inside). I create the footer at the end of the doc generation for every page like that

Code: Select all
PdfUnitConvertor unitCvtr = new PdfUnitConvertor();
            foreach (PdfPageBase p in this.doc.Sections[0])
            {
                PdfStringFormat formatRight = new PdfStringFormat(PdfTextAlignment.Right) { MeasureTrailingSpaces = true };
                PdfStringFormat formatCenter = new PdfStringFormat(PdfTextAlignment.Center) { MeasureTrailingSpaces = true };
                PdfStringFormat formatLeft = new PdfStringFormat(PdfTextAlignment.Left) { MeasureTrailingSpaces = true };
                float x = unitCvtr.ConvertUnits(this.margin.Left, PdfGraphicsUnit.Point, PdfGraphicsUnit.Centimeter);
                float width = p.Canvas.ClientSize.Width
                              - unitCvtr.ConvertUnits(
                                  this.margin.Left,
                                  PdfGraphicsUnit.Point,
                                  PdfGraphicsUnit.Centimeter)
                              - unitCvtr.ConvertUnits(
                                  this.margin.Right,
                                  PdfGraphicsUnit.Point,
                                  PdfGraphicsUnit.Centimeter);
                float y = p.Canvas.ClientSize.Height
                          - unitCvtr.ConvertUnits(this.margin.Bottom, PdfGraphicsUnit.Point, PdfGraphicsUnit.Centimeter);
                p.Canvas.DrawLine(this.blackPen, x, y - 9, x + width, y - 9);
                y = y + 1;
                string numberLabel = string.Format("Seite {0} von {1}", startNumber++, pageCount);
                p.Canvas.DrawString(
                    string.Format("Name: {0} {1}", b.Name, b.Vorname),
                    this.fontArial8,
                    this.brSchwarz,
                    0,
                    y - 9,
                    formatLeft);
                p.Canvas.DrawString(
                    string.Format("Personal-Nr: {0}", b.IdMitarbeiter),
                    this.fontArial8,
                    this.brSchwarz,
                    width/2,
                    y - 9,
                    formatCenter);
                p.Canvas.DrawString(numberLabel, this.fontArial8, this.brSchwarz, x + width, y - 9, formatRight);
            }

my main problem now is that some grids with any kind of content are big enough to reach the end of the page an go over my footer element. Tried to use the PaginateBounds of the PdfGridLayoutFormat like that

Code: Select all
    SizeF sizeBounds = this.doc.Sections[0].Pages[0].GetClientSize().ToSize();
                    this.tableLayout = new PdfGridLayoutFormat
                                           {
                                               Layout = PdfLayoutType.Paginate,
                                               PaginateBounds =
                                                   new RectangleF(
                                                   0,
                                                   0,
                                                   sizeBounds.Width,
                                                   sizeBounds.Height - 20)
                                           };

but they seem to be ignored ....or I do something wrong.

Does anybody have an Idea how to do that so that the PaginateBounds work as expected?

Thnx in advance
Matthias

gmaa
 
Posts: 4
Joined: Wed Jan 04, 2012 11:51 am

Wed Apr 15, 2015 6:41 am

Hello,

Thanks for your inquiry.
Please refer to the code below:

Code: Select all
 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)
        {
           
         
            PdfStringFormat formatRight = new PdfStringFormat(PdfTextAlignment.Right) { MeasureTrailingSpaces = true };
            PdfStringFormat formatCenter = new PdfStringFormat(PdfTextAlignment.Center) { MeasureTrailingSpaces = true };
            PdfStringFormat formatLeft = new PdfStringFormat(PdfTextAlignment.Left) { MeasureTrailingSpaces = true };
           
            float y = 0;
           

            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", 10f, FontStyle.Italic));
            PdfPen pen = new PdfPen(Color.Black, 0.75f);
            y = font.Height + 1;
            bottomSpace.Graphics.SetTransparency(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 = formatRight;
            pageNumberLabel.Text = "Seite {0} von {1}";
            pageNumberLabel.Draw(bottomSpace.Graphics, pageSize.Width - 150, y);

            PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", 10f));
            String label;
            label=string.Format("Name: {0} {1}", "Hu", "Harry");

            float x = 0;
            bottomSpace.Graphics.DrawString(label, font1, PdfBrushes.Black, x, y, formatLeft);
            label = string.Format("Personal-Nr: {0}", "123456");
            bottomSpace.Graphics.DrawString(label, font1, PdfBrushes.Black, pageSize.Width/2, y, formatCenter);

        }
        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

Wed Apr 15, 2015 7:51 am

Hi,

Your solution adds the footer only to the first of all pages. All the following are without any footer.
As I've written there are also other elements an not the grid only so I've to add pages manually like that:
Code: Select all
        private PdfPageBase CreateNewPage()
        {
            if (this.page == null)
            {
                return this.doc.Pages.Add(PdfPageSize.A4, this.margin);
            }

            return this.page.Document.Sections[0].Pages.Add();
        }


could that be the Problem?
Greets,
Matthias

gmaa
 
Posts: 4
Joined: Wed Jan 04, 2012 11:51 am

Wed Apr 15, 2015 9:27 am

Hello,

Thanks for your feedback.
On my side all pages of the generated pdf document have the footer. I have uploaded the generated pdf document and the database file. Please see attachment.
If you still have issue, please provide us your code and test data. It would be helpful to replicate the issue and work out the solution for you ASAP.
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

Wed Apr 15, 2015 11:19 am

I could send you my class ...any mail adress? (there're some things inside which shouldn't be visible for all)
Greets,
Matthias

gmaa
 
Posts: 4
Joined: Wed Jan 04, 2012 11:51 am

Thu Apr 16, 2015 1:52 am

Hello,

Please send your code to sweety.lu@e-iceblue.com or support@e-iceblue.com.

Sincerely,
Sweety

E-iceblue support team
User avatar

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

Return to Spire.PDF