Articles in this section
Category / Section

How to Set Margins when Drawing a Multi-Page ASP.NET Core PDF Grid?

3 mins read

The Syncfusion Essential PDF is a feature-rich and high performance ASP.NET Core PDF library used to create, read, and edit PDF documents programmatically without Adobe dependencies. Using this library, you can set different margins for the pages of a PDF document when drawing a grid spanning multiple pages using C#.

Steps to apply different margins when drawing a multi-page PDF grid programmatically:

  1. Create a new console application project.

Screenshot_1337.png

  1. Install the Syncfusion.Pdf.Net.Core NuGet package as a reference to your console application from Nuget.org.

NuGet.png

  1. Include the following namespaces in the Program.cs file.

C#

using Syncfusion.Pdf;
using Syncfusion.Pdf.Grid;
  1. Use the following code sample in Program.cs to set different margins for the second page and onward of the PDF document.

C#

//Create a new PDF document.
PdfDocument document = new PdfDocument();
//Set page margins
document.PageSettings.Margins.Top = 50;
document.PageSettings.Margins.Bottom = 50;
//Add a new PDF page.
PdfPage page = document.Pages.Add();
//Create a PdfGrid.
PdfGrid pdfGrid = new PdfGrid();
//Add values to the list.
List<object> data = new List<object>();
for (int i = 0; i < 100; i++)
{
   Object row1 = new { ID = "1", Name = "Clay", Price = "$10" };
   Object row2 = new { ID = "2", Name = "Gray", Price = "$20" };
   Object row3 = new { ID = "3", Name = "Ash", Price = "$30" };
   data.Add(row1);
   data.Add(row2);
   data.Add(row3);
}
//Add list to IEnumerable.
IEnumerable<object> tableData = data;
//Assign data source.
pdfGrid.DataSource = tableData;
PdfGridLayoutFormat format = new PdfGridLayoutFormat();
//Set paginate bounds
format.PaginateBounds = new Syncfusion.Drawing.RectangleF(0, 15, page.GetClientSize().Width, page.GetClientSize().Height - 15);
//Draw the grid, letting Syncfusion handle pagination.
pdfGrid.Draw(page, new Syncfusion.Drawing.RectangleF(0, 0, page.GetClientSize().Width, page.GetClientSize().Height), format);
//Create file stream.
using (FileStream outputFileStream = new FileStream("Output.pdf", FileMode.Create, FileAccess.ReadWrite))
{
   //Save the PDF document to file stream.
   document.Save(outputFileStream);
}
//Close the document.
document.Close(true);

The page size cannot be changed when drawing a grid with pagination. This means dynamic page size changes are not possible while rendering data using a grid. However, this requirement can be fulfilled by adjusting the pagination bounds to add space at the top of the second and subsequent pages.

A complete working sample can be downloaded from GitHub.

Output.png

Conclusion
I hope you enjoyed learning about how to set margins when drawing a multi-page ASP.NET Core PDF grid.

You can refer to our ASP.NET Core PDF feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our ASP.NET Core PDF example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied