文章主要介绍使用ITextSharp导出PDF表格和图片的简单操作说明,以下为ITextSharp.dll下载链接

  分享链接:http://pan.baidu.com/s/1nuc6glj 密码:3gxw

  一、流程

                                                                                                                                    

二、简单实例:

1)创建表格实例

      程序:

        /// <summary>
/// 创建PDF文件
/// </summary>
private void CreatPDFTable()
{
//打印PDF表格
string pdfname = string.Empty;
SaveFileDialog dlg = new SaveFileDialog();
dlg.FileName = "PDF表格";
dlg.DefaultExt = ".pdf";
dlg.Filter = "Text documents (.pdf)|*.pdf";
if (dlg.ShowDialog() == DialogResult.OK)
{
pdfname = dlg.FileName;
FileStream fs = new FileStream(pdfname, FileMode.Create); //创建文件流
Document document = new Document(PageSize.A7.Rotate()); //创建文件 PageSize.A7.Rotate()表示A7纸横向输出
PdfWriter pdfWriter = PdfWriter.GetInstance(document, fs); //实例化
document.Open(); //打开文件
document.Add(new Paragraph(""));
document.Add(PDFTable1()); //添加表格 document.SetPageSize(PageSize.A6); //A6纸纵向输出
document.NewPage(); //新起一页
document.Add(new Paragraph(""));
document.Add(PDFTable2());
document.Add(new Paragraph(""));
document.Add(PDFTable3());
document.Close(); //关闭文件
fs.Close();
}
}
/// <summary>
/// 创建表格1
/// </summary>
/// <returns></returns>
private PdfPTable PDFTable1()
{
var table1 = new PdfPTable(); //创建表格实例4列
int[] a = { , , , }; //设置列宽比例
table1.SetWidths(a); for (int i = ; i < ; i++)
{
table1.AddCell((i + ).ToString()); //添加单元格
}
return table1;
} /// <summary>
/// 创建表格2
/// </summary>
/// <returns></returns>
private PdfPTable PDFTable2()
{
//字体定义
var bfchinese = BaseFont.CreateFont(@"c:\windows\fonts\simkai.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);//simkai.ttf
var ChFont_12 = new iTextSharp.text.Font(bfchinese, );
var ChFont_10 = new iTextSharp.text.Font(bfchinese, );
var ChFont_8 = new iTextSharp.text.Font(bfchinese, );
var ChFont_12_red = new iTextSharp.text.Font(bfchinese, , iTextSharp.text.Font.ITALIC, BaseColor.RED);
var table2 = new PdfPTable(); //创建表格实例
PdfPCell cell;
cell = new PdfPCell(new Phrase(Convert.ToString(), ChFont_10));
cell.HorizontalAlignment = ; //居中输入 默认 0:居左 1:居中
cell.Colspan = ; //横向合并单元格
table2.AddCell(cell); cell = new PdfPCell(new Phrase(Convert.ToString(), ChFont_8));
cell.HorizontalAlignment = ;
cell.Rowspan = ; //纵向合并单元格
table2.AddCell(cell); cell = new PdfPCell(new Phrase(Convert.ToString(), ChFont_10));
cell.BackgroundColor = BaseColor.GRAY; //设置背景颜色
table2.AddCell(cell); cell = new PdfPCell(new Phrase(Convert.ToString(), ChFont_12_red)); //设置字体颜色
table2.AddCell(cell); for (int i = ; i < ; i++)
{
table2.AddCell((i + ).ToString()); //添加单元格
}
return table2;
} /// <summary>
/// 单元格中填加表
/// </summary>
/// <returns></returns>
private PdfPTable PDFTable3()
{
var table3 = new PdfPTable(); int[] a = { , , , }; //设置列宽比例
table3.SetWidths(a);
for (int i = ; i < ; i++)
{
if (i == )
{
PdfPCell cell = new PdfPCell(PDFTable2()); //单元格中添加表2
cell.Padding = ; //表与单元格间距为0;
table3.AddCell(cell);
continue;
}
table3.AddCell(""); //添加单元格
}
return table3;
}

    样式:

    

    

2)创建图片

    程序:

 private void btnPDFImage_Click(object sender, EventArgs e)
{
string pdfname = string.Empty;
SaveFileDialog dlg = new SaveFileDialog();
dlg.FileName = "PDF图片";
dlg.DefaultExt = ".pdf";
dlg.Filter = "Text documents (.pdf)|*.pdf";
if (dlg.ShowDialog() == DialogResult.OK)
{
pdfname = dlg.FileName;
FileStream fs = new FileStream(pdfname, FileMode.Create); //创建文件流
Document document = new Document(PageSize.A5.Rotate()); //创建文件 PageSize.A5.Rotate()表示A5纸横向输出
PdfWriter pdfWriter = PdfWriter.GetInstance(document, fs); //实例化
document.Open(); //打开文件
document.Add(addImage(document)); //添加图片
document.Add(addImage2(document)); //添加图片
document.Close(); //关闭文件
fs.Close();
}
}
/// <summary>
/// PDF添加图片
/// </summary>
/// <returns></returns>
private iTextSharp.text.Image addImage(Document document)
{
iTextSharp.text.Image hgLogo = iTextSharp.text.Image.GetInstance("yijing.jpg");
hgLogo.ScalePercent(4f); //图片比例
hgLogo.SetAbsolutePosition(40f, document.PageSize.Height - 100f); //iamge 位置
return hgLogo;
}
/// <summary>
/// PDF添加图片2
/// </summary>
/// <param name="document"></param>
/// <returns></returns>
private iTextSharp.text.Image addImage2(Document document)
{
iTextSharp.text.Image hgLogo = iTextSharp.text.Image.GetInstance("yijing.jpg");
hgLogo.ScalePercent(12f); //图片比例
hgLogo.SetAbsolutePosition(200f, document.PageSize.Height - 400f); //iamge 位置
return hgLogo;
}

样式:

ITextSharp导出PDF表格和图片(C#)的更多相关文章

  1. iTextSharp导出PDF模板(报告)

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.IO;us ...

  2. 利用ITextSharp导出PDF文件

    最近项目中需要到处PDF文件,最后上网搜索了一下,发现ITextSharp比较好用,所以做了一个例子: public string ExportPDF() { //ITextSharp Usage / ...

  3. 使用itextsharp创建PDF文档——图片集合

    文档管理系统中 ,扫描模块将文档或证件扫描后.为了便于保存多个图片,拟将多个图片生成一个PDF文档进行保存. 这里我们就需要PDF生成工具了.你可以在这里下载.PDFCreator 主要使用了开源工具 ...

  4. C# 利用ITextSharp导出PDF文件

    最近项目中需要导出PDF文件,最后上网搜索了一下,发现ITextSharp比较好用,所以做了一个例子: public string ExportPDF() { //ITextSharp Usage / ...

  5. c# iTextSharp导出PDF

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Th ...

  6. 使用iTextSharp导出PDF

    /// <summary> /// 导出至PDF /// </summary> /// <param name="dt">数据源</par ...

  7. 史上最全的springboot导出pdf文件

    最近项目有一个导出报表文件的需求,我脑中闪过第一念头就是导出pdf(产品经理没有硬性规定导出excel还是pdf文件),于是赶紧上网查看相关的资料,直到踩了无数的坑把功能做出来了才知道其实导出exce ...

  8. PHP导入导出excel表格图片(转)

    写excel的时候,我用过pear的库,也用过pack压包的头,同样那些利用smarty等作的简单替换xml的也用过,csv的就更不用谈了.呵呵.(COM方式不讲了,这种可读的太多了,我也写过利用wp ...

  9. iText导出pdf、word、图片

    一.前言 在企业的信息系统中,报表处理一直占比较重要的作用,本文将介绍一种生成PDF报表的Java组件--iText.通过在服务器端使用Jsp或JavaBean生成PDF报表,客户端采用超级连接显示或 ...

随机推荐

  1. .NET (三)委托第三讲:内置委托Action

    .NET 为我们提供了无返回值的内置委托 Action,代码如下: // 摘要: // 封装一个方法,该方法只有一个参数并且不返回值. // // 参数: // obj: // 此委托封装的方法的参数 ...

  2. vsftp 根据用户设置

    #vsftpd.conf ###############pam_service_name=vsftpduserlist_enable=YEStcp_wrappers=YESlocal_root=/da ...

  3. Gson运用

    输出对象或者对象的list时,我们一般都是重写toString,和遍历list,但是使用Gson输出对象或者对象的list会非常方便. Gson输出list或者对象.Gson数据没有格式化. impo ...

  4. Centos下安装Redis

    转自:http://nnzhp.cn/article/9/ 遇到问题,安装后并启动,redis-cli报错:Could not connect to Redis at 127.0.0.1:6379: ...

  5. [JSP]解决Maven创建项目失败

    来源:http://lovespss.blog.51cto.com/1907593/522225 新建Maven项目时遇到这个错误: Unable to create project from arc ...

  6. for+next()实现数组的遍历及while list each 的使用

    //要求使用for循环语句来完成该数组的遍历//输出每一项的键名和对应值:    $a = array(           'a' => 34,           5 => 51,  ...

  7. 《Benign and maligenant breast tumors classification based on region growing and CNN segmentation》翻译阅读与理解

    注明:本人英语水平有限,翻译不当之处,请以英文原版为准,不喜勿喷,另,本文翻译只限于学术交流,不涉及任何版权问题,若有不当侵权或其他任何除学术交流之外的问题,请留言本人,本人立刻删除,谢谢!! 另:欢 ...

  8. ThinkPHP升级指导

    升级指导 http://www.kancloud.cn/manual/thinkphp5/163239 从V5.0.1升级到V5.0.2 从V5.0.1升级到V5.0.2需要注意如下事项: 下列模型属 ...

  9. XidianOJ 1087 浪漫的V8

    题目描述 V8为了讨女朋友开心,给lx承包大活后面那个水塘.为了筹集资金,V8偷偷地溜进了一座古墓,发现在他面前有金光闪闪的若干小箱子,里面全都是金粉,作为横行于各种@#¥&场所的V8来说,辨 ...

  10. log4net日志在app.config中assembly不起作用

    log4net 1.2.15.0日志在app.config中assembly不起作用,必须 1.手动调用方法log4net.Config.XmlConfigurator.Configure()来初始化 ...