c# itextsharp根据模板导出pdf报表
2021-04-01 20:26
标签:contains one bsp .text text flatten source har mda PDF文件在目前来说是比较流行的电子文档格式,在.Net framework 中身并不包含可以和pdf打交道的方法,也没有很好操作PDF的类库,所以我们需要对pdf进行编辑,加密,模板打印等操作不得不去找可用的第三方组件,这里就可以选择使用ITextSharp来实现,这个程序是JAVA工具IText的.Net版本。 准备: 1、pdf编辑工具 中文帮助文档 2、 itextsharp.dll 点击下载 简单的操作直接查看帮助文件,此处先省略 后续考虑补充 本次讲的是根据pdf模板导出数据,首先创建模板,我这里使用的工具是Adobe Acrobat 视图——工具——准备表单,可以在需要赋值的地方放上一个文本框,把名字改成要用的名字。 这是编辑好后的效果 添加引用 新建了一个导出数据测试窗体 load事件以及测试数据, 封装了一个 导出Pdf帮助类 导出按钮事件 先获取pdf模板变量,Dictionary 赋值对应dic 保存成新pdf 这样一个小案例就完成了。 c# itextsharp根据模板导出pdf报表 标签:contains one bsp .text text flatten source har mda 原文地址:https://www.cnblogs.com/sbgh/p/9229774.html一、前言
二、itextsharp 使用
2.1 pdf模板
2.2 新建项目
///
using iTextSharp.text.pdf;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace itextsharpDemo
{
///
private void button1_Click(object sender, EventArgs e)
{
//dic 获取学生成绩表 Pdf导出模板
string templetPath = System.Environment.CurrentDirectory + @"\学生成绩表.pdf";
Dictionarystring, string> dic = PdfLeadingHelper.ReadForm(templetPath);
#region 赋值 dic
dic["className"] = textBox1.Text;
for (int i = 1; i )
{
if (dic.ContainsKey("number_" + i))
{
dic["number_" + i] = dt.Rows[i-1]["number"] +"";
dic["name_" + i] = dt.Rows[i-1]["name"] + "";
dic["yw_" + i] = dt.Rows[i-1]["yw"] + "";
dic["sx_" + i] = dt.Rows[i-1]["sx"] + "";
dic["yy_" + i] = dt.Rows[i-1]["yy"] + "";
dic["pd_" + i] = dt.Rows[i-1]["pd"] + "";
dic["pe_" + i] = dt.Rows[i-1]["pe"] + "";
dic["remark_" + i] = dt.Rows[i-1]["remark"] + "";
}
}
#endregion
//保存
SaveFileDialog dlg = new SaveFileDialog();
dlg.FileName = "成绩单";
dlg.DefaultExt = ".pdf";
dlg.Filter = "Text documents (.pdf)|*.pdf";
if (dlg.ShowDialog() == DialogResult.OK)
{
bool rsBool = PdfLeadingHelper.FillForm(templetPath, dlg.FileName, dic);
if (rsBool)
{
MessageBox.Show("导出成功!!");
}
}
}
文章标题:c# itextsharp根据模板导出pdf报表
文章链接:http://soscw.com/index.php/essay/71083.html