C#中創(chuàng)建PDF網(wǎng)格并插入圖片的方法
這篇文章我將向大家演示如何以編程的方式在PDF文檔中創(chuàng)建一個(gè)網(wǎng)格,并將圖片插入特定的網(wǎng)格中。
網(wǎng)上有一些類似的解決方法,在這里我選擇了一個(gè)免費(fèi)版的PDF組件。安裝控件后,創(chuàng)建新項(xiàng)目,添加安裝目錄下的dll文件作為項(xiàng)目的引用以及命名空間,如下:
using Spire.Pdf; using Spire.Pdf.Graphics; using Spire.Pdf.Grid;
接下來(lái)是詳細(xì)步驟及代碼片段:
步驟1: 首先創(chuàng)建一個(gè)PDF文檔,并添加一個(gè)新頁(yè)面。
PdfDocument doc = new PdfDocument(); PdfPageBase page = doc.Pages.Add();
步驟2:創(chuàng)建一個(gè)一行兩列的網(wǎng)格。
PdfGrid grid = new PdfGrid(); PdfGridRow row = grid.Rows.Add(); grid.Columns.Add(2);
步驟3:設(shè)置單元格邊框與填充內(nèi)容的間距。
grid.Style.CellPadding = new PdfPaddings(1, 1, 1, 1);
步驟4:設(shè)置列寬。
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\Pictures\448a5ba8f8851709a1f53e.jpg");
步驟6:設(shè)置圖片的大小,將其插入第一個(gè)單元格。
textAndStyle.ImageSize = new SizeF(50, 50); lst.List.Add(textAndStyle); row.Cells[0].Value = lst;
步驟7:在頁(yè)面特定位置繪制PDF網(wǎng)格。
PdfLayoutResult result = grid.Draw(page, new PointF(10, 30));
步驟8:保存并運(yùn)行PDF文件。
doc.SaveToFile(outputFile, FileFormat.PDF); System.Diagnostics.Process.Start(outputFile);
效果圖:

這個(gè)Spire. PDF組件基于.NET的辦公軟件庫(kù),還有其他豐富的功能。所以對(duì)于有辦公開發(fā)需求的朋友,感興趣的話可以在官網(wǎng)參考在線教程。
全部代碼:
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";
//新建一個(gè)PDF文檔
PdfDocument doc = new PdfDocument();
//添加頁(yè)面
PdfPageBase page = doc.Pages.Add();
//創(chuàng)建PDF網(wǎng)格
PdfGrid grid = new PdfGrid();
//設(shè)置單元格邊框與填充內(nèi)容的間距
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);
//設(shè)置列寬
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");
//設(shè)置圖片大小
textAndStyle.ImageSize = new SizeF(50, 50);
lst.List.Add(textAndStyle);
//在第一個(gè)單元格添加圖片
row.Cells[0].Value = lst;
//在頁(yè)面特定位置繪制PDF網(wǎng)格
PdfLayoutResult result = grid.Draw(page, new PointF(10, 30));
//保存并運(yùn)行PDF文件
doc.SaveToFile(outputFile, FileFormat.PDF);
System.Diagnostics.Process.Start(outputFile);
}
}
}
以上所述是小編給大家介紹的C#中創(chuàng)建PDF網(wǎng)格并插入圖片的方法,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
C# GetField方法的應(yīng)用實(shí)例講解
C#中的GetField是一個(gè)反射方法,用于獲取指定類型的字段信息,它可以通過(guò)字段名稱來(lái)獲取字段對(duì)象,并且可以在運(yùn)行時(shí)動(dòng)態(tài)地訪問(wèn)和操作這些字段,本文給大家介紹了C# GetField方法的應(yīng)用,需要的朋友可以參考下2024-04-04
C#去掉字符串中所有匹配的字符String.Replace方法
在C#中,如果你想要去掉字符串中所有匹配的字符,你可以使用String.Replace方法,本文主要介紹了C#去掉字符串中所有匹配的字符String.Replace方法,具有一定的參考價(jià)值,感興趣的可以了解一下2024-04-04
VS2019配置OpenCV4.1.0詳細(xì)教程與測(cè)試代碼(推薦)
這篇文章主要介紹了VS2019配置OpenCV4.1.0詳細(xì)教程與測(cè)試代碼,本文通過(guò)截圖實(shí)例代碼相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03
C#實(shí)現(xiàn)調(diào)用本機(jī)攝像頭實(shí)例
這篇文章主要介紹了C#實(shí)現(xiàn)調(diào)用本機(jī)攝像頭的方法,可以實(shí)現(xiàn)調(diào)用本機(jī)攝像頭進(jìn)行拍照,具有不錯(cuò)的實(shí)用價(jià)值,需要的朋友可以參考下2014-08-08
C#如何根據(jù)Model字段名稱自動(dòng)匹配并替換值
這篇文章主要為大家詳細(xì)介紹了如何使用 C# 反射機(jī)制實(shí)現(xiàn)一個(gè)靈活的模板引擎,能夠根據(jù) Model 字段名稱自動(dòng)匹配并替換模板中的占位符,需要的可以了解下2025-04-04
C#中GraphicsPath的Flatten方法用法實(shí)例
這篇文章主要介紹了C#中GraphicsPath的Flatten方法,實(shí)例分析了Flatten方法的相關(guān)使用技巧,需要的朋友可以參考下2015-06-06

