这篇文章我将向大家演示如何以编程的方式在PDF文档中创建一个网格,并将图片插入特定的网格中。对c# pdf 网格 插入图片的知识感兴趣的朋友一起看看吧
这篇文章我将向大家演示如何以编程的方式在PDF文档中创建一个网格,并将图片插入特定的网格中。对c# pdf 网格 插入图片的知识感兴趣的朋友一起看看吧
这篇文章我将向大家演示如何以编程的方式在PDF文档中创建一个网格,并将图片插入特定的网格中。对c# pdf 网格 插入图片的知识感兴趣的朋友一起看看吧
这篇文章我将向大家演示如何以编程的方式在pdf文档中创建一个网格,并将图片插入特定的网格中。
网上有一些类似的解决方法,在这里我选择了一个免费版的pdf组件。安装控件后,创建新项目,添加安装目录下的dll文件作为项目的引用以及命名空间,如下:
|
1
2
3
|
using spire.pdf;using spire.pdf.graphics;using spire.pdf.grid; |
接下来是详细步骤及代码片段:
步骤1: 首先创建一个pdf文档,并添加一个新页面。
|
1
2
|
pdfdocument doc = new pdfdocument();pdfpagebase page = doc.pages.add(); |
步骤2:创建一个一行两列的网格。
|
1
2
3
|
pdfgrid grid = new pdfgrid();pdfgridrow row = grid.rows.add();grid.columns.add(2); |
步骤3:设置单元格边框与填充内容的间距。
|
1
|
grid.style.cellpadding = new pdfpaddings(1, 1, 1, 1); |
步骤4:设置列宽。
|
1
2
3
|
float width = page.canvas.clientsize.width - (grid.columns.count + 1);grid.columns[0].width = width * 0.1f;grid.columns[1].width = width * 0.1f; |
步骤5:加载图片。
|
1
2
3
|
pdfgridcelltextandstylelist lst = new pdfgridcelltextandstylelist();pdfgridcelltextandstyle textandstyle = new pdfgridcelltextandstyle();textandstyle.image=pdfimage.fromfile(@"c:\users\administrator\pictures\448a5ba8f8851709a1f53e.jpg"); |
步骤6:设置图片的大小,将其插入第一个单元格。
|
1
2
3
|
textandstyle.imagesize = new sizef(50, 50);lst.list.add(textandstyle);row.cells[0].value = lst; |
步骤7:在页面特定位置绘制pdf网格。
|
1
|
pdflayoutresult result = grid.draw(page, new pointf(10, 30)); |
步骤8:保存并运行pdf文件。
|
1
2
|
doc.savetofile(outputfile, fileformat.pdf);system.diagnostics.process.start(outputfile); |
效果图:

这个spire. pdf组件基于.net的办公软件库,还有其他丰富的功能。所以对于有办公开发需求的朋友,感兴趣的话可以在官网参考在线教程。
全部代码:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
using system;using system.drawing;using system.windows.forms;using spire.pdf;using spire.pdf.graphics;using spire.pdf.grid;namespace insert_an_image_to_pdf_grid_cell{public partial class form1 : form{public form1(){initializecomponent();}private void button1_click(object sender, eventargs e){string outputfile ="output.pdf";//新建一个pdf文档pdfdocument doc = new pdfdocument();//添加页面pdfpagebase page = doc.pages.add();//创建pdf网格pdfgrid grid = new pdfgrid();//设置单元格边框与填充内容的间距grid.style.cellpadding = new pdfpaddings(1, 1, 1, 1);//添加行pdfgridrow row = grid.rows.add();//添加列grid.columns.add(2);float width = page.canvas.clientsize.width - (grid.columns.count + 1);//设置列宽grid.columns[0].width = width * 0.1f;grid.columns[1].width = width * 0.1f;//加载图片pdfgridcelltextandstylelist lst = new pdfgridcelltextandstylelist();pdfgridcelltextandstyle textandstyle = new pdfgridcelltextandstyle();textandstyle.image=pdfimage.fromfile (@"c:\users\administrator\pictures\448a5ba8f8851709a1f53e.jpg");//设置图片大小textandstyle.imagesize = new sizef(50, 50);lst.list.add(textandstyle);//在第一个单元格添加图片row.cells[0].value = lst;//在页面特定位置绘制pdf网格pdflayoutresult result = grid.draw(page, new pointf(10, 30));//保存并运行pdf文件doc.savetofile(outputfile, fileformat.pdf);system.diagnostics.process.start(outputfile);}}} |
以上所述是小编给大家介绍的c#中创建pdf网格并插入图片的方法,希望对大家有所帮助如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对米米素材网网站的支持!
原文链接:http://www.cnblogs.com/Yesi/p/6050438.html
发表评论