Free Support Forum - aspose.com

Tables spanning multiple pages horizontally do not align

Each page I generate contains a page header and footer. The first page has a title followed by a text paragraph and then a table which can span multiple pages vertically. When this happens, the content of table on subsequent pages does not align with the first page (because the title space is not reserved on the subsequent pages).

Can anyone help?

@MaccShaun

Thanks for contacting support.

Would you kindly share complete code snippet along with generated output PDF file. We will test the scenario in our environment and address it accordingly.

using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Aspose.Pdf;
using Aspose.Pdf.Text;

namespace PDFGen
{
class Program
{
static void Main(string[] args)
{
Aspose.Pdf.Document doc = new Aspose.Pdf.Document();

        doc.Info.Author = "Company Name";

        var page = doc.Pages.Add();

        page.PageInfo.Margin = new MarginInfo(15, 50, 15, 50);

        page.Header = new HeaderFooter();
        page.Header.Margin = new MarginInfo(15, 0, 15, 0);

        Aspose.Pdf.Table hdrTable = new Aspose.Pdf.Table();

        int imageSize = 35;
        int width2Use = (int)(!page.PageInfo.IsLandscape ? page.PageInfo.Width : page.PageInfo.Height);
        int twentyPercent = (int)(width2Use / 5);
        int thirtyPercent = (int)((width2Use / 10) * 3);
        int fortyPercent = (int)((width2Use / 10) * 4);
        int sixtyPercent = (int)(width2Use - (2 * twentyPercent));

        hdrTable.ColumnWidths = string.Format("{0} {1} {2} {3}", imageSize, thirtyPercent - imageSize, fortyPercent, thirtyPercent);
        hdrTable.DefaultCellPadding = new MarginInfo(0, 2, 0, 2);
        hdrTable.Border = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.Bottom, .5f, Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Black));

        Aspose.Pdf.Row hdrRow = hdrTable.Rows.Add();

        hdrRow.DefaultCellTextState.Font = FontRepository.FindFont("TimesNewRoman");
        hdrRow.DefaultCellTextState.FontSize = 11;
        hdrRow.DefaultCellTextState.FontStyle = FontStyles.Bold;

        Aspose.Pdf.Image image = new Aspose.Pdf.Image();
        image.File = "PrintLogo.bmp";
        image.FixHeight = imageSize;
        image.FixWidth = imageSize;

        hdrRow.Cells.Add();
        hdrRow.Cells[0].Paragraphs.Add(image);
        hdrRow.Cells[0].RowSpan = 2;
        hdrRow.Cells[0].Alignment = HorizontalAlignment.Left;
        hdrRow.Cells.Add("Company Name");
        hdrRow.Cells[1].Alignment = HorizontalAlignment.Left;
        hdrRow.Cells[1].DefaultCellTextState.FontSize = 16;
        hdrRow.Cells[1].DefaultCellTextState.Font = FontRepository.FindFont("Arial");
        hdrRow.Cells[1].Border = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.Bottom, .5f, Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Black));
        hdrRow.Cells.Add("Report Title");
        hdrRow.Cells[2].Alignment = HorizontalAlignment.Center;
        hdrRow.Cells[2].RowSpan = 2;
        hdrRow.Cells[2].VerticalAlignment = VerticalAlignment.Center;
        hdrRow.Cells.Add("USERNAME");
        hdrRow.Cells[3].Alignment = HorizontalAlignment.Right;
        hdrRow.Cells[3].RowSpan = 2;
        hdrRow.Cells[3].VerticalAlignment = VerticalAlignment.Center;

        hdrRow = hdrTable.Rows.Add();

        hdrRow.DefaultCellTextState.Font = FontRepository.FindFont("TimesNewRoman");
        hdrRow.DefaultCellTextState.FontSize = 11;
        hdrRow.DefaultCellTextState.FontStyle = FontStyles.Bold;

        hdrRow.Cells.Add("Service Name");
        hdrRow.Cells[0].Alignment = HorizontalAlignment.Left;

        page.Header.Paragraphs.Add(hdrTable);

        page.Footer = new HeaderFooter();
        page.Footer.Margin = new MarginInfo(15, 0, 15, 0);

        Aspose.Pdf.Table ftrTable = new Aspose.Pdf.Table();

        ftrTable.ColumnWidths = string.Format("{0} {1} {0}", twentyPercent, sixtyPercent);
        ftrTable.DefaultCellPadding = new MarginInfo(0, 1, 0, 1);
        ftrTable.Border = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.Top, .5f, Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Black));

        Aspose.Pdf.Row ftrRow = ftrTable.Rows.Add();

        ftrRow.DefaultCellTextState.Font = FontRepository.FindFont("TimesNewRoman");
        ftrRow.DefaultCellTextState.FontSize = 10;
        ftrRow.DefaultCellTextState.FontStyle = FontStyles.Bold;

        ftrRow.Cells.Add("Version : 9.99.9.999");
        ftrRow.Cells[0].Alignment = HorizontalAlignment.Left;
        ftrRow.Cells.Add("This report is confidential and for the intended recipient only.");
        ftrRow.Cells[1].Alignment = HorizontalAlignment.Center;
        ftrRow.Cells[1].DefaultCellTextState.FontSize = 9;
        ftrRow.Cells.Add(System.DateTime.Now.ToString("dd/MM/yy HH:mm.ss"));
        ftrRow.Cells[2].Alignment = HorizontalAlignment.Right;

        ftrRow = ftrTable.Rows.Add();

        ftrRow.DefaultCellTextState.Font = FontRepository.FindFont("TimesNewRoman");
        ftrRow.DefaultCellTextState.FontSize = 10;
        ftrRow.DefaultCellTextState.FontStyle = FontStyles.Bold;

        ftrRow.Cells.Add(string.Empty);
        ftrRow.Cells[0].Alignment = HorizontalAlignment.Left;
        ftrRow.Cells.Add("If you are not the intended recipient please destroy this page immediately.");
        ftrRow.Cells[1].Alignment = HorizontalAlignment.Center;
        ftrRow.Cells[1].DefaultCellTextState.FontSize = 9;
        ftrRow.Cells.Add("Page $p-$P");
        ftrRow.Cells[2].Alignment = HorizontalAlignment.Right;

        page.Footer.Paragraphs.Add(ftrTable);

        Aspose.Pdf.Heading heading = new Aspose.Pdf.Heading(1);
        heading.IsInList = false;
        heading.Text = "Supplier Name";
        heading.TextState.FontSize = 20;
        heading.TextState.ForegroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Red);

        page.Paragraphs.Add(heading);
        page.Paragraphs[0].Margin = new MarginInfo(0, 10, 0, 0);

        TextFragment textFragment = new TextFragment("Services provided between dd/mm/yyyy  and dd/mm/yyyy.");

        textFragment.TextState.Font = FontRepository.FindFont("TimesNewRoman");
        textFragment.TextState.FontSize = 12;
        textFragment.TextState.FontStyle = FontStyles.Italic;
        textFragment.TextState.FormattingOptions = new TextFormattingOptions();

        textFragment.TextState.FormattingOptions.LineSpacing = TextFormattingOptions.LineSpacingMode.FullSize;

        page.Paragraphs.Add(textFragment);
        page.Paragraphs[1].Margin = new MarginInfo(0, 10, 0, 0);

        Aspose.Pdf.Table table = new Aspose.Pdf.Table();

        table.Broken = TableBroken.Vertical;
        table.CornerStyle = BorderCornerStyle.Round;

        table.Border = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, .5f, Aspose.Pdf.Color.FromRgb(System.Drawing.Color.LightGray));
        table.DefaultCellBorder = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, .5f, Aspose.Pdf.Color.FromRgb(System.Drawing.Color.LightGray));
        table.DefaultCellTextState.Font = FontRepository.FindFont("Arial");
        table.DefaultCellTextState.FontSize = 12;

        table.DefaultCellPadding = new MarginInfo(5, 5, 5, 5);
        table.RepeatingRowsCount = 1;
        table.ColumnWidths = string.Format("200 100 200 200 100");

        for (int row_count = 1; row_count <= 50; row_count++)
        {
            Aspose.Pdf.Row row = table.Rows.Add();

            if (row_count == 1)
            {
                row.DefaultCellTextState.FontSize = 14;
                row.DefaultCellTextState.FontStyle = FontStyles.Bold;

                row.Cells.Add("Name");
                row.Cells.Add("Age");
                row.Cells.Add("Position");
                row.Cells.Add("Car");
                row.Cells.Add("Reg. No.");

                row.Cells[1].Alignment = HorizontalAlignment.Center;
            }
            else
            {
                string name = string.Empty;
                int age = 0;
                string position = string.Empty;
                string car = string.Empty;
                string regNo = string.Empty;

                switch (row_count % 3)
                {
                    case 1:
                        name = "Director One";
                        age = 51;
                        position = "Director";
                        car = "Audi A6 S-Line Black Edition";
                        regNo = "XX99 XXX";
                        break;
                    case 2:
                        name = "Director Two";
                        age = 49;
                        position = "Director";
                        car = "Audi A4 Avant S-Line";
                        regNo = "XX99 XXX";
                        break;
                    default:
                        name = "Employee One";
                        age = 46;
                        position = "Employee";
                        car = "Audi A1 SE";
                        regNo = "XX99 XXX";
                        break;
                }

                row.Cells.Add(name);
                row.Cells.Add(age.ToString());
                row.Cells.Add(position);
                row.Cells.Add(car);
                row.Cells.Add(regNo);

                row.Cells[1].Alignment = HorizontalAlignment.Center;
            }
        }

        page.Paragraphs.Add(table);

        TextFragment endOfReport = new TextFragment("*** End of Report ***");

        endOfReport.TextState.Font = FontRepository.FindFont("TimesNewRoman");
        endOfReport.TextState.FontSize = 12;
        endOfReport.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Center;
        endOfReport.Margin = new MarginInfo(0, 0, 0, 10);

        page.Paragraphs.Add(endOfReport);

        doc.Save(".\\Output.pdf");
    }
}

}
Output.pdf (361.2 KB)

@MaccShaun

Thanks for sharing the code snippet.

We were able to notice the behavior of table rendering in output PDF document. However, this is happening because title on the first page is not present on the subsequent pages, as you also have stated earlier. Would you please confirm if you want to repeat page title on every page of PDF document or you only want to render table with equivalent top margin on every page. We will check details accordingly at our side and share our feedback with you.

Thank you for spending time to verify the issue.

To answer your question, the title should only appear on page 1 but we need the table to render with the same top margin as if the title had been output. I hope that makes sense?

@MaccShaun

Thanks for writing back.

You were not specifying any top margin for main table in you shared code snippet. However, by default table will be rendered with default margin values. Please note that top margin of the table will be specified according to the content above it. In your case, document title is above the table on first page and on subsequent pages, only header of the page remains above it.

If you specify table margins as following:

Aspose.Pdf.Table table = new Aspose.Pdf.Table();
table.Margin = new MarginInfo(0, 0, 0, 50);

You will notice that table will be rendered with 50 margin value on each page, but its margin will be calculated with respect to the content which is above the table i.e. 50 points margin from title or header. This behavior of the API seems justified as far as top margin of the table is concerned.

In case it is mandatory for you to keep table on subsequent pages, in a way that it renders with same top margin as with the title, we can log an investigation ticket in our issue tracking system. We will then investigate the feasibility of this feature that your require and share our feedback with you.

Thank you for your reply.

I agree with your comments regarding margins but not the end result.

It appears sensible to me to expect vertically split pages to align so, when the pages are laid alongside each other, the rows align and the columns continue across the pages? This would maintain the context of the row.

I agree my sample is simplistic but, for the purpose of our application, this alignment is essential.

@MaccShaun

We have logged an investigation ticket as PDFNET-44991 in our issue tracking system. We will further investigate the feasibility of this feature to achieve what you require. As soon as we have some definite updates regarding investigation, we will update you within this forum thread. Please be patient and spare us little time.

We are sorry for the inconvenience.