C# Draw PDF Nested Table

Keywords: ASP.NET Programming Windows

Nested tables, i.e. inserting one or more tables into a specific cell of a table, have the advantage of making the layout of the content more reasonable and also facilitating the application. In the following example, you will show how to insert nested tables into PDF documents through C # programming.
The main points are summarized as follows:

  1. Insert nested tables
  2. Insert text into nested tables
  3. Insert pictures into nested tables

Using tools

  • Spire.PDF 4.9.7
  • Visual Studio
    Note:
    1. The version used here is 4.9.7. Tests show that the PdfGridCellContentList class and the PdfGridCellContent class involved in the code are available only in this version or above. When using, please pay attention to version information.
    2. After downloading the installation, when editing the code, please note adding a reference to Spire.Pdf.dll(dll files can be obtained under the Bin folder in the installation path)

    Sample code (for reference)

using Spire.Pdf;
using Spire.Pdf.Graphics;
using Spire.Pdf.Grid;
using System.Drawing;
using System.Windows.Forms;
using System;

namespace NestedTable_PDF
{
    class Program
    {
        static void Main(string[] args)
        {
            //Instantiate the PdfDocument class and add pages to the new document
            PdfDocument pdf = new PdfDocument();
            PdfPageBase page = pdf.Pages.Add();

           //Add fonts, brushes, and write text to PDF documents
            PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("style between the running hand and the regular script", 11f), true);
            PdfPen pen = new PdfPen(Color.Gray);
            string text = "2018 Pyeongchang Olympic Winter Games Medal Ranking";
            page.Canvas.DrawString(text, font, pen, 100, 50);

            //Create a PDF table and add two rows
            PdfGrid grid = new PdfGrid(); 
            PdfGridRow row1 = grid.Rows.Add();
            PdfGridRow row2 = grid.Rows.Add();

            //Set the upper and lower margins between the cell content and the border of the table
            grid.Style.CellPadding.Top = 5f;
            grid.Style.CellPadding.Bottom = 5f;

            //Add three columns and set column width
            grid.Columns.Add(3);
            grid.Columns[0].Width = 120f;
            grid.Columns[1].Width = 150f;
            grid.Columns[2].Width = 120f; 

            //Create a nested table with one row and two columns
            PdfGrid embedGrid1 = new PdfGrid();
            PdfGridRow newRow = embedGrid1.Rows.Add();
            embedGrid1.Columns.Add(2);

            //Set the column width of nested tables
            embedGrid1.Columns[0].Width = 50f;
            embedGrid1.Columns[1].Width = 60f;

            //Initialize the SizeF class and set the image size
            SizeF imageSize = new SizeF(45, 35);
            //Instantiate PdfGridCellContentList, PdfGridCellContent classes, and load images added to nested tables
            PdfGridCellContentList contentList = new PdfGridCellContentList();
            PdfGridCellContent content = new PdfGridCellContent();
            content.Image = PdfImage.FromFile("1.png");
            content.ImageSize = imageSize;
            contentList.List.Add(content);
            //Instantiate PdfStringFormat, PdfTrueTypeFont classes, set cell text alignment, font, font size, etc.
            PdfStringFormat stringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);         

            //Set the values of cells in nested tables and apply formatting
            newRow.Cells[0].Value = "Norway";
            newRow.Cells[0].StringFormat = stringFormat;
            newRow.Cells[1].Value = contentList; //Add the image to the second cell of the nested table
            newRow.Cells[1].StringFormat = stringFormat;           

            //Set the value and format of the cell in the first table
            row1.Cells[0].Value = "Rank";
            row1.Cells[0].StringFormat = stringFormat;
            row1.Cells[0].Style.Font = font;
            row1.Cells[0].Style.BackgroundBrush = PdfBrushes.LightSalmon;
            row1.Cells[1].Value = "Country";
            row1.Cells[1].StringFormat = stringFormat;
            row1.Cells[1].Style.Font = font;
            row1.Cells[1].Style.BackgroundBrush = PdfBrushes.LightSalmon;
            row1.Cells[2].Value = "Total";
            row1.Cells[2].StringFormat = stringFormat;
            row1.Cells[2].Style.Font = font;
            row1.Cells[2].Style.BackgroundBrush = PdfBrushes.LightSalmon;

            row2.Cells[0].Value = "1";
            row2.Cells[0].StringFormat = stringFormat;
            row2.Cells[0].Style.Font = font;
            row2.Cells[1].Value = embedGrid1; //Add nested tables to the second row and second cell of the first table
            row2.Cells[1].StringFormat = stringFormat;

            row2.Cells[2].Value = "39";
            row2.Cells[2].StringFormat = stringFormat;
            row2.Cells[2].Style.Font = font;

            //Draw the table to the specified location on the page
            grid.Draw(page, new PointF(30f, 90f));

            //Save the document and open it
            pdf.SaveToFile("result.pdf");
            System.Diagnostics.Process.Start("result.pdf");
        }
    }
}

After the completion of the code, debug the program, generate documents, and draw nested tables as follows:

Above is the whole content of this C # drawing PDF nested tables.
Example extension:

(End of this article)

Posted by PHP_Idiot on Thu, 31 Jan 2019 23:57:16 -0800