c# iText 生成PDF 有文字,图片,表格,文字样式,对齐方式,页眉页脚,等等等,

时间:2021-02-12 09:04:08

#region 下载说明书PDF
protected void lbtnDownPDF_Click(object sender, EventArgs e)
{
int pid = ConvertHelper.GetInteger(Request["PID"]);
pds = p.GetModel(pid);
#region 第一种方法
DataTable datatable = new DataTable(dt.ToString());
try
{
string Url1 = "pdfBook\\" + GetNewName() + ".pdf";
//创建一个文档对象,并设置他的初始化大小
Rectangle pSize = new Rectangle(144, 90);
//文档的背景色
pSize.BackgroundColor = Color.WHITE;
//常用页面大小 PageSize.A4;
Document document = new Document(PageSize.A4);
PdfWriter.GetInstance(document, new FileStream(Server.MapPath(Url1), FileMode.Create));
BaseFont bfChinese = BaseFont.CreateFont("C:\\WINDOWS\\Fonts\\simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
Font fontChinese = new Font(bfChinese, 12, Font.NORMAL, new Color(0, 0, 0));
Font headerChinese = new Font(bfChinese, 15, Font.BOLD, new Color(0, 0, 0));
Font beizhufontChinese = new Font(bfChinese, 10, Font.ITALIC, new Color(255, 0, 0));
#region 页眉和页脚
/**
* HeaderFooter的第2个参数为非false时代表打印页码
* 页眉页脚中也可以加入图片,并非只能是文字
*/
HeaderFooter header = new HeaderFooter(new Phrase("陕西龙凤祥珠宝版权所有", beizhufontChinese), false);

//设置是否有边框等
// header.setBorder(Rectangle.NO_BORDER);
header.Border = Rectangle.BOTTOM_BORDER;
header.Alignment = 1;
header.BorderColor = Color.RED;
document.Header = header;

HeaderFooter footer = new HeaderFooter(new Phrase("-", beizhufontChinese), new Phrase("-", beizhufontChinese));
/**
* 0是靠左
* 1是居中
* 2是居右
*/
footer.Alignment = 1;
footer.BorderColor = Color.RED;
footer.Border = Rectangle.BOTTOM_BORDER;
document.Footer = footer;

/**
* 页眉页脚的设置一定要在open前设置好
*/
#endregion
document.Open();

//按设置的字体输出文本
string contents = replaceHtmlCode(pds.ProductName);
string details = replaceHtmlCode(pds.ProductDetail);
Paragraph heading = new Paragraph(contents, headerChinese);
heading.SpacingAfter = 18f;
heading.Alignment = 1;
//heading.IndentationLeft = 220f;
document.Add(heading);

MultiColumnText columns = new MultiColumnText();

columns.AddSimpleColumn(36f, 336f);

columns.AddSimpleColumn(360f, document.PageSize.Width - 36f);

Paragraph detail = new Paragraph(details, fontChinese);
detail.SpacingAfter = 9f;
detail.Alignment = Element.ALIGN_JUSTIFIED;
detail.IndentationLeft = 12f;//全文都缩进了
//document.Add(detail);
//输出图片到PDF文件
string Url = "../uploadpic\\" + pds.Picture;//<img src="../uploadpic/20150213/wm201521317692.jpg" />
iTextSharp.text.Image jpeg01 = iTextSharp.text.Image.GetInstance(Server.MapPath(Url));
jpeg01.IndentationLeft = 150f;
//document.Add(jpeg01);
columns.AddElement(detail);
columns.AddElement(jpeg01);
columns.AddElement(detail);
columns.AddElement(detail);
columns.AddElement(detail);
//columns.AddElement(end);
document.Add(columns);
//Paragraph end = new Paragraph("陕西龙凤祥珠宝版权所有", beizhufontChinese);
//end.IndentationLeft = 200f;
//document.Add(end);
document.Close();

#region 自动下载
// 保存文件的物理路径
string FullPath = HttpContext.Current.Server.MapPath(Url1);
// 初始化FileInfo类的实例,作为文件路径的包装
FileInfo FI = new FileInfo(FullPath);
// 判断文件是否存在
if (FI.Exists)
{
// 将文件保存到本机
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment;filename=" + Server.UrlEncode(FI.Name));
Response.AddHeader("Content-Length", FI.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.Filter.Close();
Response.WriteFile(FI.FullName);
Response.End();
}
#endregion
}
catch (DocumentException de)
{
Response.Write(de.ToString());
}
#endregion

}
#endregion