您好,欢迎访问一九零五行业门户网

C#如何绘制PDF嵌套表格?绘制PDF嵌套表格的步骤

如何绘制pdf嵌套表格?本篇文章就给大家详细介绍绘制pdf嵌套表格的步骤。有一定的参考价值,有需要的朋友可以参考一下,希望对你们有所助。
嵌套表格,即在一张表格中的特定单元格中再插入一个或者多个表格,使用嵌套表格的优点在于能够让内容的布局更加合理,同时也方便程序套用。下面的示例中,将介绍如何通过c#编程来演示如何插入嵌套表格到pdf文档。
要点概括:
1. 插入嵌套表格
2. 插入文字到嵌套表格
3. 插入图片到嵌套表格
使用工具
spire.pdf 4.9.7
注:
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);        }    }}
以上是本次c#在pdf中绘制嵌套表格的全部内容。
更多相关教程,请访问:
c#视频教程
c#开发图文教程
bootstrap视频教程
(本文完)
以上就是c#如何绘制pdf嵌套表格?绘制pdf嵌套表格的步骤的详细内容。
其它类似信息

推荐信息