Home  >  Article  >  Backend Development  >  How to draw PDF nested tables in C#? Steps to draw PDF nested tables

How to draw PDF nested tables in C#? Steps to draw PDF nested tables

青灯夜游
青灯夜游forward
2018-10-18 14:55:352163browse

How to draw PDF nested tables? This article will give you a detailed introduction to the steps of drawing PDF nested tables. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Nested tables, that is, inserting one or more tables into specific cells 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. In the following example, we will introduce how to use C# programming to demonstrate how to insert nested tables into a PDF document.

Summary of key points:

1. Insert into nested table

2. Insert text into nested table

3. Insert Image to Nested Table

Using Tools

  • Spire.PDF 4.9.7

Note:

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

2. After downloading and installing, when editing the code, please pay attention to adding a reference to Spire.Pdf.dll (the dll file can be obtained in the Bin folder under the installation path)

Sample code (for reference)

Step 1: Create document

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

Step 2: Add fonts, brushes, and write text to PDF document

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);

Step 3: Create the first table

//创建一个PDF表格,并添加两行
PdfGrid grid = new PdfGrid(); 
PdfGridRow row1 = grid.Rows.Add();
PdfGridRow row2 = grid.Rows.Add();

//设置表格的单元格内容和边框之间的上、下边距
grid.Style.CellPadding.Top = 5f;
grid.Style.CellPadding.Bottom = 5f;

//添加三列,并设置列宽grid.Columns.Add(3);
grid.Columns[0].Width = 120f;
grid.Columns[1].Width = 150f;
grid.Columns[2].Width = 120f;

Step 4: Create a nested table

//创建一个一行两列的嵌套表格
PdfGrid embedGrid1 = new PdfGrid();
PdfGridRow newRow = embedGrid1.Rows.Add();
embedGrid1.Columns.Add(2);

//设置嵌套表格的列宽
embedGrid1.Columns[0].Width = 50f;
embedGrid1.Columns[1].Width = 60f;

Step 5: Add text and pictures to the nested table

//初始化SizeF类,设置图片大小
SizeF imageSize = new SizeF(45, 35);

//实例化PdfGridCellContentList、PdfGridCellContent类,加载需要添加到嵌套表格的图片
PdfGridCellContentList contentList = new PdfGridCellContentList();
PdfGridCellContent content = new PdfGridCellContent();
content.Image = PdfImage.FromFile("1.png");
content.ImageSize = imageSize;
contentList.List.Add(content);
//实例化PdfStringFormat、PdfTrueTypeFont类,设置单元格文字对齐方式
PdfStringFormat stringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);

//添加文本内容及图片到嵌套表格
newRow.Cells[0].Value = "Norway";
newRow.Cells[0].StringFormat = stringFormat;
newRow.Cells[1].Value = contentList; //将图片添加到嵌套表格的第二个单元格
newRow.Cells[1].StringFormat = stringFormat;

Step 6: Add data to 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; //将嵌套表格添加到第一个表格的第二行第二个单元格
row2.Cells[1].StringFormat = stringFormat;

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

Step 7: Draw the table to the specified position on the page

grid.Draw(page, new PointF(30f, 90f));

Step 8: Save the document

pdf.SaveToFile("result.pdf");

After completing the code, debug the program and generate documentation. The drawn table is as follows:

All codes:

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)
        {
            //实例化PdfDocument类,并添加页面到新建的文档
            PdfDocument pdf = new PdfDocument();
            PdfPageBase page = pdf.Pages.Add();

            //添加字体、画笔,写入文本到PDF文档
            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);

            //创建一个PDF表格,并添加两行
            PdfGrid grid = new PdfGrid(); 
            PdfGridRow row1 = grid.Rows.Add();
            PdfGridRow row2 = grid.Rows.Add();

            //设置表格的单元格内容和边框之间的上、下边距
            grid.Style.CellPadding.Top = 5f;
            grid.Style.CellPadding.Bottom = 5f;

            //添加三列,并设置列宽
            grid.Columns.Add(3);
            grid.Columns[0].Width = 120f;
            grid.Columns[1].Width = 150f;
            grid.Columns[2].Width = 120f; 

            //创建一个一行两列的嵌套表格
            PdfGrid embedGrid1 = new PdfGrid();
            PdfGridRow newRow = embedGrid1.Rows.Add();
            embedGrid1.Columns.Add(2);

            //设置嵌套表格的列宽
            embedGrid1.Columns[0].Width = 50f;
            embedGrid1.Columns[1].Width = 60f;

            //初始化SizeF类,设置图片大小
            SizeF imageSize = new SizeF(45, 35);

            //实例化PdfGridCellContentList、PdfGridCellContent类,加载需要添加到嵌套表格的图片
            PdfGridCellContentList contentList = new PdfGridCellContentList();
            PdfGridCellContent content = new PdfGridCellContent();
            content.Image = PdfImage.FromFile("1.png");
            content.ImageSize = imageSize;
            contentList.List.Add(content);
            //实例化PdfStringFormat、PdfTrueTypeFont类,设置单元格文字对齐方式
            PdfStringFormat stringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);         

            //添加文本内容及图片到嵌套表格
            newRow.Cells[0].Value = "Norway";
            newRow.Cells[0].StringFormat = stringFormat;
            newRow.Cells[1].Value = contentList; //将图片添加到嵌套表格的第二个单元格
            newRow.Cells[1].StringFormat = stringFormat;           

            //设置第一个表格的单元格的值和格式
            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; //将嵌套表格添加到第一个表格的第二行第二个单元格
            row2.Cells[1].StringFormat = stringFormat;

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

            //将表格绘制到页面指定位置
            grid.Draw(page, new PointF(30f, 90f));

            //保存文档并打开
            pdf.SaveToFile("result.pdf");
            System.Diagnostics.Process.Start("result.pdf");
        }
    }
}

The above is the C# in PDF Draws the entire contents of the nested table.

For more related tutorials, please visit:

(End of this article)

The above is the detailed content of How to draw PDF nested tables in C#? Steps to draw PDF nested tables. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete