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.

Fri Mar 20, 2020 11:07 am

if the content continues to the next page, then the margins are not set. how to set ?

am placing all my contents to the grid cell and it is continuing to the next page. but it starts at the 0th position from top. How to set margins to all the pages.

ushanandhini_1297
 
Posts: 16
Joined: Mon Apr 20, 2015 11:34 am

Mon Mar 23, 2020 7:34 am

Hi

Thanks for your post.
Please refer to the following code to set margins.
Code: Select all
    //Set page margins
            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(3f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
            margin.Right = margin.Left;
 PdfPageBase page = pdf.Pages.Add(PdfPageSize.A4,margin);

Full code:
Code: Select all
 PdfDocument pdf = new PdfDocument();
            //Margins
            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(3f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
            margin.Right = margin.Left;
          PdfPageBase page = pdf.Pages.Add(PdfPageSize.A4,margin);
          //The following is test data
            String text = "Spire.PDF for .NET is a professional PDF API applied to creating, writing, editing, handling and reading PDF files without any external dependencies within .NET ( C#, VB.NET, ASP.NET, .NET Core) application. Using this .NET PDF library, you can implement rich capabilities to create PDF files from scratch or process existing PDF documents entirely through C#/VB.NET without installing Adobe Acrobat.";
            String allText = text + text;
            //Create a grid
            PdfGrid grid = new PdfGrid();
            grid.Columns.Add(1);
            //Set the cell padding
            grid.Style.CellPadding = new PdfPaddings(1, 1, 1, 1);
            PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 12f));
            PdfStringFormat format = new PdfStringFormat();
            format.Alignment = PdfTextAlignment.Left;
            PdfGridRow row;
            for (int i = 0; i < 10; i++)
            {
                row = grid.Rows.Add();
                row.Cells[0].StringFormat = format;
                row.Cells[0].Value = allText;
                row.Cells[0].Style.Font = font;

            }
            PdfLayoutResult result = grid.Draw(page, new PointF(0, 0));
            String output = "result1.pdf";
            pdf.SaveToFile(output);




Sincerely,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 3044
Joined: Wed Jun 27, 2012 8:50 am

Return to Spire.PDF

cron