资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

怎么在C#里绘制PDF嵌套表格-创新互联

今天就跟大家聊聊有关怎么在C#里绘制PDF嵌套表格,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

创新互联建站是一家集网站建设,象山企业网站建设,象山品牌网站建设,网站定制,象山网站建设报价,网络营销,网络优化,象山网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。

要点概括:

1. 插入嵌套表格

2. 插入文字到嵌套表格

3. 插入图片到嵌套表格

使用工具

  • Spire.PDF 4.9.7

注:

1.这里使用的版本为4.9.7,经测试,对于代码中涉及的PdfGridCellContentList类和PdfGridCellContent类仅在使用该版本或者以上版本可用。使用时,请注意版本信息。

2.下载安装后,在编辑代码时,请注意添加引用Spire.Pdf.dll(dll文件可在安装路径下的Bin文件夹下获取)

怎么在C#里绘制PDF嵌套表格

示例代码(供参考)

步骤 1 :创建文档

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

步骤 2 :添加字体、画笔,写入文本到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);

步骤 3 :创建第一个表格

//创建一个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;

步骤 4 :创建一个嵌套表格

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

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

步骤 5 :添加文本、图片到嵌套表格

//初始化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;

步骤 6 :添加数据到第一个表格

//设置第一个表格的单元格的值和格式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;

步骤 7:将表格绘制到页面指定位置

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

步骤 8 :保存文档

pdf.SaveToFile("result.pdf");

完成代码后,调试程序,生成文档。绘制的表格如下:

怎么在C#里绘制PDF嵌套表格

全部代码:

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

以上是本次C#在PDF中绘制嵌套表格的全部内容。

看完上述内容,你们对怎么在C#里绘制PDF嵌套表格有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注创新互联行业资讯频道,感谢大家的支持。


分享题目:怎么在C#里绘制PDF嵌套表格-创新互联
本文网址:http://cdkjz.cn/article/dcjoed.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220