C#中创建PDF网格并插入图片的方法

C#中创建PDF网格并插入图片的方法,第1张

概述这篇文章我将向大家演示如何以编程的方式在PDF文档中创建一个网格,并将图片插入特定的网格中。

这篇文章我将向大家演示如何以编程的方式在pdf文档中创建一个网格,并将图片插入特定的网格中。

网上有一些类似的解决方法,在这里我选择了一个免费版的pdf组件。安装控件后,创建新项目,添加安装目录下的dll文件作为项目的引用以及命名空间,如下:

using Spire.pdf;using Spire.pdf.Graphics;using Spire.pdf.GrID;

接下来是详细步骤及代码片段:

步骤1: 首先创建一个pdf文档,并添加一个新页面。

pdfdocument doc = new pdfdocument();pdfpageBase page = doc.Pages.Add();

步骤2:创建一个一行两列的网格。

pdfGrID grID = new pdfGrID();pdfGrIDRow row = grID.Rows.Add();grID.Columns.Add(2);

步骤3:设置单元格边框与填充内容的间距。

grID.Style.Cellpadding = new pdfpaddings(1,1,1);

步骤4:设置列宽。

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:加载图片。

pdfGrIDCellTextAndStyleList lst = new pdfGrIDCellTextAndStyleList();pdfGrIDCellTextAndStyle textAndStyle = new pdfGrIDCellTextAndStyle();textAndStyle.Image=pdfImage.Fromfile(@"C:\Users\administrator\Pictures8a5ba8f8851709a1f53e.jpg");

步骤6:设置图片的大小,将其插入第一个单元格。

textAndStyle.ImageSize = new Sizef(50,50);lst.List.Add(textAndStyle);row.Cells[0].Value = lst;

步骤7:在页面特定位置绘制pdf网格。

pdfLayoutResult result = grID.Draw(page,new PointF(10,30));

步骤8:保存并运行pdf文件。

doc.Savetofile(outputfile,fileFormat.pdf);System.Diagnostics.Process.Start(outputfile);

效果图:

这个Spire. pdf组件基于.NET的办公软件库,还有其他丰富的功能。所以对于有办公开发需求的朋友,感兴趣的话可以在官网参考在线教程。

全部代码:

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);//添加行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\Pictures8a5ba8f8851709a1f53e.jpg");//设置图片大小textAndStyle.ImageSize = new Sizef(50,50);lst.List.Add(textAndStyle);//在第一个单元格添加图片row.Cells[0].Value = lst;//在页面特定位置绘制pdf网格pdfLayoutResult result = grID.Draw(page,30));//保存并运行pdf文件doc.Savetofile(outputfile,fileFormat.pdf);System.Diagnostics.Process.Start(outputfile);}}}

以上所述是小编给大家介绍的C#中创建pdf网格并插入图片的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程小技巧网站的支持!

总结

以上是内存溢出为你收集整理的C#中创建PDF网格并插入图片的方法全部内容,希望文章能够帮你解决C#中创建PDF网格并插入图片的方法所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1257073.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存