您的位置:首页 > 编程语言 > C#

用itextsharp实现Pdf生成 (C#代码)

2013-09-23 15:26 316 查看
string path = Server.MapPath("PDFiles");

string filestr = DateTime.Now.ToString("yyyyMMddHHmmss") + ".pdf";

string filename = path + "/" +filestr; //尽量采用变量

//第一步:创建一个新PDF Document对象,设置纸张为A4,左右上下边距,单位为磅,72磅=1英寸=25.4毫米

Document document = new Document(PageSize.A4, 36, 36, 36, 36);

//第二步:创建PdfWriter

PdfWriter writer=PdfWriter.GetInstance(document, new FileStream(filename, FileMode.Create));

//第三步:打开文档()

writer.PageEvent = new PDFHeaderFooter(); //如果不要页面页脚请注释该行

document.Open();

//第四步:写文档(页眉页脚、文字、表格、图片、)

//1.中文字体支持

//黑体

BaseFont bfComic = BaseFont.CreateFont(@"C:\Windows\Fonts\SIMHEI.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

//宋体

BaseFont bfSun = BaseFont.CreateFont(@"C:\Windows\Fonts\SIMSUN.TTC,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

//字体大小

Font fontHead = new Font(bfSun, 24);

Font fontText = new Font(bfSun, 16);

//以下两行用于排版控制

document.Add(new Paragraph(" "));

document.Add(new Paragraph(" "));

//2.PDF表格

PdfPTable tab = new PdfPTable(3);

PdfPCell cell = new PdfPCell(new Phrase("业务数据展示", fontHead));

cell.Colspan = 3;

cell.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right

cell.BorderColor = new BaseColor(System.Drawing.Color.Red);

cell.Border = Rectangle.BOTTOM_BORDER; // | Rectangle.TOP_BORDER;

cell.BorderWidthBottom = 3f;

tab.AddCell(cell);

//第一行

for (int i = 0; i < dt.Rows.Count; i++)

{

tab.AddCell(dt.Rows[i][0].ToString());

PdfPCell cellB = new PdfPCell(new Phrase(dt.Rows[i][1].ToString(), fontText));

tab.AddCell(cellB);

tab.AddCell(dt.Rows[i][4].ToString());

}

//第二行

tab.AddCell(dt.Rows[0][0].ToString());

PdfPCell cellC = new PdfPCell(new Phrase(dt.Rows[0][1].ToString(), fontText));

tab.AddCell(cellC);

tab.AddCell(dt.Rows[0][2].ToString());

//第三行(跨单元格)

cell = new PdfPCell();

cell.Colspan = 3;

iTextSharp.text.List pdfList = new List(List.ALPHABETICAL); //支持数字、字母、无序号等列表显示

pdfList.Add(new iTextSharp.text.ListItem(dt.Rows[0][0].ToString()));

pdfList.Add(dt.Rows[0][1].ToString());

pdfList.Add(dt.Rows[0][2].ToString());

pdfList.Add(dt.Rows[0][3].ToString());

cell.AddElement(pdfList);

tab.AddCell(cell);

document.Add(tab);

//3.文字输出

document.Add(new Paragraph("Welcome to doptech,德璞欢迎您!",fontText));

//坐标式文字输出

PdfContentByte cb = writer.DirectContent;

cb.BeginText();

cb.SetFontAndSize(bfSun, 12);

cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, "This text is centered 看到了吗", 200, 500, 0);

cb.EndText();

//划线-含二维绘图方法

cb.SetLineWidth(8f);

cb.SetLineDash(40f);

cb.MoveTo(100, 550);

cb.LineTo(500, 550);

cb.Stroke();

// 4.显示图片

iTextSharp.text.Image img1 = iTextSharp.text.Image.GetInstance(Server.MapPath("images/Tulips.jpg"));

img1.ScaleToFit(50, 30);

Chunk ck=new Chunk(img1,50,50);

document.Add(img1);

iTextSharp.text.Image img2 = iTextSharp.text.Image.GetInstance(Server.MapPath("images/Tulips.jpg"));

img2.ScaleToFit(500, 800);

img2.Alignment = Element.ALIGN_CENTER | Element.ALIGN_MIDDLE;

document.Add(img2);

iTextSharp.text.Image img3 = iTextSharp.text.Image.GetInstance(Server.MapPath("images/bbtn.jpg"));

cb.MoveTo(300, 300);

cb.AddImage(img3, 300, 300, 100, 100, 100, 100);

//cb.AddImage(logo,100,100,100,100,100,100);

//第五步:关闭文档

document.Close();

--------------------------------------------

//页眉页脚类

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using iTextSharp.text.pdf;

using iTextSharp.text;

/// <summary>

///PDFHeaderFooter 的摘要说明

/// </summary>

public class PDFHeaderFooter : PdfPageEventHelper

{

Font fontText;

string header;

string footer;

public PDFHeaderFooter()

{

//

//TODO: 在此处添加构造函数逻辑

//

BaseFont bfSun = BaseFont.CreateFont(@"C:\Windows\Fonts\SIMSUN.TTC,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

fontText = new Font(bfSun, 18);

}

public PDFHeaderFooter(string header,string footer)

{

BaseFont bfSun = BaseFont.CreateFont(@"C:\Windows\Fonts\SIMSUN.TTC,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

fontText = new Font(bfSun, 18);

header = header;

footer = footer;

}

public override void OnOpenDocument(PdfWriter writer, Document document)

{

base.OnOpenDocument(writer, document);

}

// 页眉

public override void OnStartPage(PdfWriter writer, Document document)

{

base.OnStartPage(writer, document);

PdfPTable tabFot = new PdfPTable(new float[] { 1F });

tabFot.SpacingAfter = 10F;

PdfPCell cell;

tabFot.TotalWidth = 600F;

cell = new PdfPCell(new Phrase("这里是页眉",fontText));

tabFot.AddCell(cell);

tabFot.WriteSelectedRows(0, -1, 36, document.Top, writer.DirectContent);

}

// 页脚

public override void OnEndPage(PdfWriter writer, Document document)

{

base.OnEndPage(writer, document);

PdfPTable tabFot = new PdfPTable(new float[] { 1F });

PdfPCell cell;

tabFot.TotalWidth = 500F;

cell = new PdfPCell(new Phrase("这里是页脚",fontText));

tabFot.AddCell(cell);

tabFot.WriteSelectedRows(0, -1, 36, document.Bottom, writer.DirectContent);

}

//write on close of document

public override void OnCloseDocument(PdfWriter writer, Document document)

{

base.OnCloseDocument(writer, document);

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: