Delete Rows and Columns from a PDF Grid in C#

2018-06-28 01:46:34 Written by  support iceblue
Rate this item
(0 votes)

Spire.PDF supports to delete rows or columns from a PDF grid before drawing it onto a PDF page. This article demonstrates the detail steps of how to delete a row and a column from a PDF grid using Spire.PDF.

Detail steps:

Step 1: Create a PDF document and add a page to it.

PdfDocument doc = new PdfDocument();
PdfPageBase page = doc.Pages.Add();

Step 2: Create a PDF grid.

PdfGrid grid = new PdfGrid();
//Set cell padding
grid.Style.CellPadding = new PdfPaddings(3, 3, 1, 1);

Step 3: Add 3 rows and 4 columns to the grid.

PdfGridRow row1 = grid.Rows.Add();
PdfGridRow row2 = grid.Rows.Add();
PdfGridRow row3 = grid.Rows.Add();

grid.Columns.Add(4);

Step 4: Set columns' width.

foreach (PdfGridColumn column in grid.Columns)
{
    column.Width = 60f;
}

Step 5: Add values to grid cells.

for (int i = 0; i < grid.Columns.Count; i++)
{
    row1.Cells[i].Value = String.Format("column{0}", i + 1);
    row2.Cells[i].Value = "a";
    row3.Cells[i].Value = "b";
}

Step 6: Delete the second row and the second column from the grid.

grid.Rows.RemoveAt(1);

grid.Columns.RemoveAt(1);

Step 7: Draw the grid onto the page and save the file.

grid.Draw(page, new PointF(0, 20));

doc.SaveToFile("Output.pdf");

Output:

Delete Rows and Columns from a PDF Grid in C#

Full code:

using System;
using System.Drawing;
using Spire.Pdf;
using Spire.Pdf.Grid;

namespace Delete_Row_and_Column_from_PDFGrid
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PDF document
            PdfDocument doc = new PdfDocument();

            //Add a page
            PdfPageBase page = doc.Pages.Add();

            //Create a PDF grid
            PdfGrid grid = new PdfGrid();

            //Set cell padding
            grid.Style.CellPadding = new PdfPaddings(3, 3, 1, 1);

            //Add 3 rows and 4 columns to the grid
            PdfGridRow row1 = grid.Rows.Add();
            PdfGridRow row2 = grid.Rows.Add();
            PdfGridRow row3 = grid.Rows.Add();
            grid.Columns.Add(4);

            //Set columns’ width
            foreach (PdfGridColumn column in grid.Columns)
            {
                column.Width = 60f;
            }

            //Add values to grid cells
            for (int i = 0; i < grid.Columns.Count; i++)
            {
                row1.Cells[i].Value = String.Format("column{0}", i + 1);
                row2.Cells[i].Value = "a";
                row3.Cells[i].Value = "b";
            }

            //Delete the second row
            grid.Rows.RemoveAt(1);

            //Delete the second column
            grid.Columns.RemoveAt(1);

            //Draw the grid to the page
            grid.Draw(page, new PointF(0, 20));

            //Save the file
            doc.SaveToFile("Output.pdf");
        }
    }
}

Additional Info

  • tutorial_title:
Last modified on Sunday, 26 September 2021 02:04