iTextSharp生成pdf文档案例

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、A5A6可以使用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:windowsfontsSIMHEI.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:windowsfontsSIMHEI.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(); }
}

原文地址:https://www.cnblogs.com/guozefeng/p/4166729.html