Tables are drawn in the margin of my page when creating pdf

0 Answers 63 Views
PdfProcessing
Adrian
Top achievements
Rank 1
Iron
Adrian asked on 25 May 2023, 07:39 AM

Hi there,

 

I need to create a pdf with various different tabs. So, I work with RadPdfProcessing.

In order to create a next page when there is no more place on the current page, I set a method that measures the available size on the page (without margin) and the size of the object to draw, before drawing it. If there is enough space, I draw it on current page, if not, I create a new page and draw it on.
But when I run my code, some tables are drawn in the margin of my page, as you can see on attached pictures.
I don't understant why. And when debugging, measures are working fine...
Could someone explain me what I'm missing or doing wrong please?

Here is my code :

Code about measuring object and page creation

        public static readonly Size _pageSize = new(Unit.MmToDip(210), Unit.MmToDip(297));
        public static readonly Thickness _margins = new(Unit.MmToDip(15));

        public static DocumentContainer CreateNewPage(RadFixedDocument document)
        {
            var page = document.Pages.AddPage();
            var editor = new DocumentContainer(page, _pageSize, new Padding(_margins.Top, _margins.Right, _margins.Bottom, _margins.Left));
            //if not first page, position the editor between margins 
            if(document.Pages.Count != 1) 
                editor.Position.Translate(_margins.Left, _margins.Top);
            return editor;
        }

        public static DocumentContainer CheckRemainingSpaceAndCreateNewPageIfNeeded(IBlockElement elementToDraw, DocumentContainer editor, RadFixedDocument document)
        {
            var remainingHeight = editor.PageHeight - _margins.Bottom  - editor.Position.Matrix.OffsetY;
            var neededSize = elementToDraw.Measure(new Size(editor.PageInternWidth, editor.PageInternHeight));

            if(remainingHeight < neededSize.Height)
            {
                return CreateNewPage(document);
            }

            return editor;
        }

 

Code about table creation

        public static Table CreateTable(BorderStyle borderStyle = BorderStyle.None)
        {
            return new Table
            {
                Borders = new TableBorders(new Border(borderStyle)),
                LayoutType = TableLayoutType.FixedWidth,
            };
        }

And the code about table drawing


        private Table DrawOneStyleTable(...)
        {
            var oneCellPadding = new Thickness(Unit.MmToDip(5));
            var anotherCellPadding = new Thickness(Unit.MmToDip(5), Unit.MmToDip(2), Unit.MmToDip(5), Unit.MmToDip(2));

            //Add table
            Table table = Helper.CreateTable();
            table.DefaultCellProperties.Padding = new Thickness(5);

            table.Borders = new TableBorders(new Border(2, BorderStyle.Single, mycolor));

            //Add rows

            _editor = Helper.CheckRemainingSpaceAndCreateNewPageIfNeeded(table, _editor, _document);
            _editor.DrawTable(table, _editor.PageInternWidth);
            return table;
        }

        private Table DrawSecondStyleTable(...)
        {
            //Add table and row
            Table table = Helper.CreateTable();
            //Add content

            _editor = Helper.CheckRemainingSpaceAndCreateNewPageIfNeeded(table, _editor, _document);
            _editor.DrawTable(table, _editor.PageInternWidth);
            return table;
        }

 

Please do not hesitate to ask me more information if needed.

 

Thank you in advance for your help,
Regards,
Adrian

Yoan
Telerik team
commented on 30 May 2023, 06:46 AM

Hello, Adrian

I would need some additional context regarding this case in order for me to get a better understanding of it and to be able to assist you properly. More specifically the DocumentContainer class implementation is something that is preventing me from successfully reproducing this case.

That said, would you be able to provide us with a fully functional sample project that you are using to reproduce the scenario? Attaching the result documents would also prove very helpful. Any additional information is always welcome.

Thank you in advance.

Regards,

Yoan

Adrian
Top achievements
Rank 1
Iron
commented on 30 May 2023, 09:02 AM

Hi Yoan,

Thank you for your answer.
Is there a less publicly way to provide you a sample project than placing it here?

I will try to prepare a fully functional one, so that you could help me easily on this case.

Thank you,
Regards,
Adrian

Adrian
Top achievements
Rank 1
Iron
commented on 30 May 2023, 12:33 PM

Hi Yoan,

You will find a functional sample project here .
A result document (and other ones that you could try to create later) can be found in the "Samples" project, in the "PdfResults" folder.

Thank you in advance for your help,
Regards,
Adrian

Yoan
Telerik team
commented on 01 Jun 2023, 12:30 PM

Hello Adrian, 

Thank you for providing us with the needed resources. After examining the project it looks like the overall approach that is being taken is appropriate. I narrowed down the possible causes of this unusual behavior to the measurement of the remaining space.

While calculating the 'remainingHeight' in the project, the 'margins.Bottom' is not taken into consideration, unlike in the provided code snippets above. Subtracting it from the equation seems to fix the issue:

public static DocumentContainer CheckRemainingSpaceAndCreateNewPageIfNeeded(IBlockElement elementToDraw, DocumentContainer editor, RadFixedDocument document)
{
    var remainingHeight = editor.PageHeight - _margins.Bottom - editor.Position.Matrix.OffsetY;
    //var remainingHeight = editor.PageInternHeight - (editor.Position.Matrix.OffsetY - _margins.Top);
    var neededSize = elementToDraw.Measure(new Size(editor.PageInternWidth, editor.PageInternHeight));
    var neededHeight = (neededSize.Height + _margins.Bottom) + (2 * 2); // 2*2 = border thickness bottom + top

    //if(remainingHeight < neededSize.Height)
    if(remainingHeight < neededHeight)
    {
        return CreateNewPage(document);
    }

    return editor;
}

I am attaching the same project with this slight modification for you to review and test. It exports a result document that does not have any unusual content placement. The document is outputted in the same 'Samples/PdfResults' folder as the initial file, which you are welcome to compare and evaluate.

Hope this helps. In case this is not sufficient enough for you, or I am not understanding the requirements correctly, feel free to write back so we can continue with resolving this case.

Regards,

Yoan

No answers yet. Maybe you can help?

Tags
PdfProcessing
Asked by
Adrian
Top achievements
Rank 1
Iron
Share this question
or