加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C#中如何创建PDF网格并插入图片

发布时间:2020-12-16 01:08:41 所属栏目:百科 来源:网络整理
导读:这篇文章我将向大家演示如何以编程的方式在PDF文档中创建一个网格,并将图片插入特定的网格中。 网上有一些类似的解决方法,在这里我选择了一个免费版的PDF组件。安装控件后,创建新项目,添加安装目录下的dll文件作为项目的引用以及命名空间,如下: using

这篇文章我将向大家演示如何以编程的方式在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:UsersAdministratorPictures448a5ba8f8851709a1f53e.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:UsersAdministratorPictures448a5ba8f8851709a1f53e.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);
        }
    }
}

?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读