1.
using iTextSharp.text;
using iTextSharp.text.pdf;

2.设置页面大小
iTextSharp.text.Rectangle pageSize = new iTextSharp.text.Rectangle(610f, 760f);
设置背景颜色
pageSize.BackgroundColor = new BaseColor(100,100,100);
3.创建文档:
Document document = new Document(pageSize);
对于一些常用尺寸比如:A4、A5\A6可以使用PageSize类,如果是横向页面可以调用它的Rotate方法。eg:
PageSize.A4.Rotate().
设置margion:
Document doc = new Document(PageSize.A4.Rotate(), 5f, 5f, 5f, 5f);
4.创建writer

PdfWriter pw = PdfWriter.GetInstance(doc, new FileStream(@"...\my.pdf",FileMode.OpenOrCreate,FileAccess.ReadWrite));
5.设置相关信息:
BaseFont baseFT = BaseFont.CreateFont(@"c:\windows\fonts\SIMHEI.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
iTextSharp.text.Font font = new iTextSharp.text.Font(baseFT); //写入一个段落, Paragraph
#region 设置PDF的头信息,一些属性设置,在Document.Open 之前完成
doc.AddAuthor("尚层装饰");
doc.AddCreationDate();
doc.AddCreator("AddCreator");
doc.AddSubject("AddSubject");
doc.AddTitle("title");
doc.AddKeywords("111");
//自定义头
doc.AddHeader("Expires", "0");
#endregion
doc.Open();
doc.NewPage();

6.插入文字:
Paragraph ph = new Paragraph("PPPPpppzzzzpppPPPP" + "", font);
ph.Alignment = iTextSharp.text.Rectangle.ALIGN_CENTER;
doc.Add(ph);
7.插入图片
iTextSharp.text.Image image =
iTextSharp.text.Image.GetInstance(@"...\20140527030942838.png");
image.Alignment = iTextSharp.text.Image.MIDDLE_ALIGN;
//image.SetAbsolutePosition(0f, 40f);
image.ScaleToFit(610f, 760f);
doc.Add(image);
8.插入表格:
iTextSharp.text.pdf.PdfPTable table = new iTextSharp.text.pdf.PdfPTable(2); // 表格有 2 列
iTextSharp.text.pdf.PdfPCell cell = new iTextSharp.text.pdf.PdfPCell(); // 创建单元格
cell.AddElement(new Phrase("sfdsdf"));
cell.Colspan = 2;
table.AddCell(cell);
//cell.HorizontalAlignment=1; // 在单元格中添加数据
table.SetWidths(new int[]{200, 100});
table.AddCell("1"); // 将单元格加入到表格中
table.AddCell("2"); // 将单元格加入到表格中
table.AddCell("1"); // 将单元格加入到表格中
table.AddCell("2"); // 将单元格加入到表格中
table.AddCell("1"); // 将单元格加入到表格中
table.AddCell("2"); // 将单元格加入到表格中
table.HorizontalAlignment = 1;
doc.Add(table);
doc.Close();

============asp.net直接下载pdf文件案例:

//pdf页面大小
iTextSharp.text.Rectangle pageSize = new iTextSharp.text.Rectangle(610f, 360f);
Document doc = new Document(pageSize, 0f, 0f, 0f, 0f);
//设置边界
MemoryStream Memory = new MemoryStream();
try
{
PdfWriter.GetInstance(doc, Memory);
BaseFont baseFT = BaseFont.CreateFont(@"c:\windows\fonts\SIMHEI.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
iTextSharp.text.Font font = new iTextSharp.text.Font(baseFT); //写入一个段落, Paragraph

#region 设置PDF的头信息,一些属性设置,在Document.Open 之前完成

doc.AddAuthor("尚层装饰");
doc.AddCreationDate();
doc.AddCreator("" + model.FirstOrDefault().SliCreateTime + "");
doc.AddSubject("");
doc.AddTitle("" + model.FirstOrDefault().SolTitle + "");
doc.AddKeywords("");
//自定义头
doc.AddHeader("Expires", "0");
#endregion
doc.Open();
foreach (var item in model)
{
doc.NewPage();
doc.Add(new Paragraph("" + item.SliName ?? "1111" + "", font));
iTextSharp.text.Image image =
iTextSharp.text.Image.GetInstance(SFast.MyUrl.ToAb(item.SliImagePath));
image.Alignment = iTextSharp.text.Image.MIDDLE_ALIGN;
// image.SetAbsolutePosition(0f, 40f);
image.ScaleToFit(610f, 360f);
doc.Add(image);
}
doc.Close();
string Strfilename = "" + model.FirstOrDefault().SolTitle + "";
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.Write("<meta http-equiv=Content-Type content=text/html;charset=UTF-8>");
Strfilename = HttpUtility.UrlEncode(Strfilename + ".pdf", System.Text.Encoding.GetEncoding("UTF-8"));
HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", Strfilename));
HttpContext.Current.Response.ContentType = "application/octet-streem";
Response.OutputStream.Write(Memory.GetBuffer(), 0, Memory.GetBuffer().Length);
Response.OutputStream.Flush();
Response.OutputStream.Close();
Response.Flush();
Response.End();
}
catch (DocumentException de) { Console.WriteLine("图片路径未找到"); Console.ReadKey(); }
}

iTextSharp生成pdf文档案例的更多相关文章

  1. 利用ItextSharp 生成PDF文档改进版

    导入的ItextSharp.dll一定要是较高的版本 数据库表结构 生成的PDF样式 代码: namespace WebPDF { public partial class _Default : Sy ...

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

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

  3. 如何从Windows Phone 生成PDF文档

    我需要从我的Windows Phone应用程序生成PDF. 遗憾的是没有标准的免费的PDF生成库在Windows Phone上运行. 我不得不自己生成PDF,通过直接写入到文件格式. 这竟然是真的很容 ...

  4. Spring Boot集成JasperReports生成PDF文档

    由于工作需要,要实现后端根据模板动态填充数据生成PDF文档,通过技术选型,使用Ireport5.6来设计模板,结合JasperReports5.6工具库来调用渲染生成PDF文档.本人文采欠缺,写作能力 ...

  5. 手把手教你使用 Java 在线生成 pdf 文档

    一.介绍 在实际的业务开发的时候,研发人员往往会碰到很多这样的一些场景,需要提供相关的电子凭证信息给用户,例如网银/支付宝/微信购物支付的电子发票.订单的库存打印单.各种电子签署合同等等,以方便用户查 ...

  6. 利用Java动态生成 PDF 文档

    利用Java动态生成 PDF 文档,则需要开源的API.首先我们先想象需求,在企业应用中,客户会提出一些复杂的需求,比如会针对具体的业务,构建比较典型的具备文档性质的内容,一般会导出PDF进行存档.那 ...

  7. Aspose.Words操作word生成PDF文档

    Aspose.Words操作word生成PDF文档 using Aspose.Words; using System; using System.Collections.Generic; using ...

  8. 使用PHP生成PDF文档

    原文:使用PHP生成PDF文档 实际工作中,我们要使用PHP动态的创建PDF文档,目前有许多开源的PHP创建PDF的类库,今天我给大家来介绍一款优秀的PDF库,它就是TCPDF,TCPDF是一个用于快 ...

  9. DocFX生成PDF文档

    使用DocFX生成PDF文档,将在线文档转换为PDF离线文档. 关于DocFX的简单介绍使用DocFX生成文档 使用docfx 命令 1.下载 https://github.com/dotnet/do ...

随机推荐

  1. Eclipse 发布到网站的附加产品的形式 Update Site

    Eclipse 发布到网站的附加产品的形式 Update Site 通过Update Site Project项目将自己做的插件产品公布到公网上,给客户或其它測试人员下载和应用,这样自己的插件就以网站 ...

  2. Bind和Eval的区别详解

    原文:Bind和Eval的区别详解 1.简单描述Eval和Bind的区别 绑定表达式 <%# Eval("字段名") %> <%# Bind("字段名& ...

  3. Nyoj Fire Station

    描述A city is served by a number of fire stations. Some residents have complained that the distance fr ...

  4. Android - 警告:it is always overridden by the value specified in the Gradle build script

    警告:it is always overridden by the value specified in the Gradle build script 本文地址: http://blog.csdn. ...

  5. SimpleInjector与MVC4集成,与Web Api集成,以及通过属性注入演示

    SimpleInjector与MVC4集成,与Web Api集成,以及通过属性注入演示   1,与MVC集成 见http://simpleinjector.codeplex.com/wikipage? ...

  6. jQuery的三种bind/One/Live/On事件绑定使用方法

    本篇文章介绍了,关于jQuery新的事件绑定机制on()的使用技巧.需要的朋友参考下   今天浏览jQuery的deprecated列表,发现live()和die()在里面了,赶紧看了一下,发现从jQ ...

  7. 判断sqlserver对象是否存在

    --查看对象是否已经存在  --数据库是否存在     --if exists (select * from sys.databases where name = ’数据库名’)    --  dro ...

  8. oracle_恢复流程图

    网上看到一个很好的数据恢复检查图片,共享给大家

  9. android 应用程序框架

    携带Android软件开发时间,由开发商开发Android应用程序是通过应用程序框架和Android底层交互,因此,发展以达到最大的部分是应用程序框架. 应用集成框架 那里4一个重要组成部分,以下. ...

  10. java_maven_linux_windows下项目运行jar

    我才用的是eclipse自带的export功能,暂时没把maven的打包插件研究清楚 导出jar包后,需要用解压缩打开,把配置文件copy到压缩包的内的顶级目录 保存 写 bat 脚本 @author ...