当前位置: 移动技术网 > IT编程>开发语言>.net > C# 绘制PDF嵌套表格

C# 绘制PDF嵌套表格

2018年10月18日  | 移动技术网IT编程  | 我要评论

张楚恬,龙龙卡盟,栗路亚

嵌套表格,即在一张表格中的特定单元格中再插入一个或者多个表格,使用嵌套表格的优点在于能够让内容的布局更加合理,同时也方便程序套用。下面的示例中,将介绍如何通过c#编程来演示如何插入嵌套表格到pdf文档。

要点概括:

1. 插入嵌套表格

2. 插入文字到嵌套表格

3. 插入图片到嵌套表格

 

使用工具

注:

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

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

示例代码(供参考)

步骤 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");

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

全部代码:

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

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

更多关于在pdf中绘制的表格的方法,请参阅以下示例:

(本文完)

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网