### C# draw PDF nested form

Nested tables, that is, insert one or more tables in a specific cell in a table. The advantage of using nested tables is that it can make the layout of the content more reasonable, and it is also convenient for program application. The following example will introduce how to use C# programming to demonstrate how to insert a nested table into a PDF document.

The main points are summarized:

1. Insert a nested table

2. Insert text into nested table

3. Insert the picture into the nested table

use tools

Note:

1. The version used here is 4.9.7. After testing, the PdfGridCellContentList and PdfGridCellContent classes involved in the code are only available in this version or above. When using, please pay attention to the version information.

2. After downloading and installing, when editing the code, please add a reference to Spire.Pdf.dll (the dll file can be obtained under the Bin folder under 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 the page to the newly created document
PdfDocument pdf = new PdfDocument();

//Add fonts, brushes, and write text to PDF documents
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("行楷", 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 form and add two rows
PdfGrid grid = new PdfGrid();

//Set the top and bottom margins between the cell content and the border of the table

//Add three columns and set the column width
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();

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

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

//Set the value of the cell of the nested table and apply the format
newRow.Cells[0].Value = "Norway";
newRow.Cells[0].StringFormat = stringFormat;
newRow.Cells[1].Value = contentList;//Add the picture to the second cell of the nested table
newRow.Cells[1].StringFormat = stringFormat;

//Set the value and format of the first table cell
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 the nested grid to the second cell of the second row of the first grid
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 position on the page
grid.Draw(page, new PointF(30f, 90f));

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


After the code is completed, debug the program, generate the document, and the effect of drawing the nested table is as follows:

The above is the entire content of this C# drawing PDF nested table.