We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Rendering list of grids in PDF

Hi,

I'm trying to render a PDF file with the following structure

CATEGORY TEXT

SUB-CATEGORY TEXT

GRID

GRID

.....

GRID

GRID

SUB-CATEGORY TEXT

GRID

GRID

.....

GRID

GRID

CATEGORY TEXT

SUB-CATEGORY TEXT

GRID

GRID

.....

GRID

GRID

SUB-CATEGORY TEXT

GRID

GRID

.....

GRID

GRID

........


I have the following code, but it renders grid data on top of each other on the second page. 

PdfGraphics graphics = page.Graphics;

//header on the first page only
float currentYPosition = 55;

//Set the properties to paginate the text.
PdfLayoutFormat layoutFormat = new PdfLayoutFormat();
layoutFormat.Break = PdfLayoutBreakType.FitPage;
layoutFormat.Layout = PdfLayoutType.Paginate;

foreach (var category in categories)
{
    RectangleF categoryBounds = new RectangleF(0, currentYPosition, page.Graphics.ClientSize.Width, page.Graphics.ClientSize.Height);
    //Draw the text element with the properties and formats set.
    graphics.DrawString(category.Name, categoryFont, categoryBrush, categoryBounds, formatLeft);

    currentYPosition += 30;

    foreach (var subCategory in subCategories)
    {
        RectangleF subCategoryBounds = new RectangleF(0, currentYPosition, page.Graphics.ClientSize.Width, page.Graphics.ClientSize.Height);
        graphics.DrawString(subCategory.Name, subCategoryFont, subCategoryBrush, subCategoryBounds, formatLeft);

        currentYPosition += 40;

        foreach (var question in questions)
        {
            //Create PdfGrid
            PdfGrid grid = new PdfGrid();

            //GRID RENDERING LOGIC HERE

            //Set properties to paginate the grid.
            PdfGridLayoutFormat gridLayoutFormat = new PdfGridLayoutFormat();
            gridLayoutFormat.Break = PdfLayoutBreakType.FitElement;
            gridLayoutFormat.Layout = PdfLayoutType.Paginate;

            // Draw PdfGrid
            var gridDrawResult = grid.Draw(page, new PointF(0, currentYPosition), gridLayoutFormat);

            currentYPosition = currentYPosition + gridDrawResult.Bounds.Height + 20;
        }
    }
}  

1 Reply 1 reply marked as answer

IJ Irfana Jaffer Sadhik Syncfusion Team February 14, 2023 12:25 PM UTC

The Essential PDF supports maintaining the position of a PDF grid drawn on a PDF page using PdfGridLayoutResult. It provides the rendered bounds of the previously added grid, which can be used to place successive elements without overlapping. Add multiple PDF grids using the bottom position of the previously rendered PDF grid.


We suspect that the reported issue may occur when the grid is drawn on the incorrect page.


Please add the below code snippet to draw a grid with the layout result of the previous grid:

//Draw the grid on the PDF document page and store the grid position in PdfGridLayoutResult.

PdfGridLayoutResult pdfGridLayoutResult = pdfGrid.Draw(page, new PointF(10, 10));


pdfGridLayoutResult = pdfGrid.Draw(pdfGridLayoutResult.Page, new PointF(10, pdfGridLayoutResult.Bounds.Bottom + 20));


We have attached a complete sample to achieve your requirement, try this on your end and let us know the result.

Sample: https://www.syncfusion.com/downloads/support/directtrac/general/ze/NetCoreSample1337915530


Follow the below links for more information,

https://help.syncfusion.com/file-formats/pdf/pdfgrid#adding-multiple-tables

https://help.syncfusion.com/cr/file-formats/Syncfusion.Pdf.Graphics.PdfLayoutResult.html#Syncfusion_Pdf_Graphics_PdfLayoutResult_Page


Please try this on your end and let us know the result.


If still you are facing an issue, we request you share the simple sample to replicate the issue. So that we can assist with you further in this


Marked as answer
Loader.
Live Chat Icon For mobile
Up arrow icon