记得上回有写到用C#操作Excel(.net 4.0)
/// <summary>
///产生PDf
/// </summary>
/// <param name="strFileName">文件名称</param>
/// <param name="dt">数据源</param>
public void PDF(string strFileName, DataTable dt, stringPrintName)
{
if (!Directory.Exists(AppConfigString.FilePath))
{
Directory.CreateDirectory(AppConfigString.FilePath);
}
string strPathTemp = AppConfigString.FilePath + DateTime.Now.ToString("yyyyMMddhhmmss") + ".pdf";
//零時的pdf地址,用于水印读取文件
string strPath = AppConfigString.FilePath + strFileName;//真正文件的位置
Document document = new Document(PageSize.A4, 25, 25, 25, 25);
FileStream stream = newFileStream(strPathTemp, FileMode.Create);
PdfWriter writer =PdfWriter.GetInstance(document, stream);
document.Footer = Foot();//页尾的函数,在open前定义
document.Open();
HeaderFooter head = new HeaderFooter(HeadTable(3, PrintName), true);
//true为在页眉的右边生成一个页数,会影响到页眉的高度,在open后定义
//headTable为页眉的函数
head.Border =Rectangle.NO_BORDER;
document.Header =head;
//页眉第一页不会出现,第2页才会出现,所以此地直接在第一列加了一个表头
//页尾在第一页就会出现
document.Add(HeadTable(3, PrintName));//第一頁表頭不自動添加
//主要数据的添加
int IRowCount =dt.Rows.Count;
int IColumnCount =dt.Columns.Count;
PdfPTable pdfTb = newPdfPTable(IColumnCount);
pdfTb.HeaderRows = 1;//自動加表頭
for (int i = 0; i < IRowCount; i++)
{
for (int j = 0; j < IColumnCount; j++)
{
string value =dt.Rows[i][j].ToString();
Phrase phs = new Phrase(value, PdfFont.Font(3));
//此时把数据转为Phrase只是为了使用字体,不然中文不能显示
pdfTb.AddCell(phs);
}
}
document.Add(pdfTb);
document.Close();
writer.Close();
PDFWatermark(strPathTemp, strPath, 10, 10, PrintName);//为生成好的文件添加水印
}
/// <summary>
///產生表頭的數據
/// </summary>
/// <param name="IType">1:ID 2:Name 3:detali</param>
/// <returns></returns>
public Phrase HeadTable(int IType, stringPrintName)
{
string strhead = "";
switch(IType)
{
case 1: strhead = "XXXXXXXXXXXXXXXXXXXX"; break;
case 2: strhead = "XXXXXXXXXXXXXXXXXXXX"; break;
case 3: strhead = "XXXXXXXXXXXXXXXXXXXX"; break;
}
Phrase phshead = new Phrase(strhead, PdfFont.Font(1));
Phrase phsjimi = new Phrase("XXXXXXXXXXXXXXXXXXXX ", PdfFont.Font(3));
Phrase phsPeople = new Phrase("XXXXXXXXXXXXXXXXXXXX" + PrintName + "XXXXXXXXXXXXXXXXXXXX" + PrintName, PdfFont.Font(3));
Phrase phsPage = new Phrase(" ", PdfFont.Font(3));
Phrase phsPrintDate = new Phrase("列印時間:" + DateTime.Now.ToString("yyyy/MM/dd hh:mm") + "
Print Date:" + DateTime.Now.ToString("yyyy/MM/dd hh:mm"), PdfFont.Font(3));
PdfPCell pchead = newPdfPCell(phshead);
pchead.VerticalAlignment = 1;
pchead.HorizontalAlignment = 1;
PdfPCell pcjimi = newPdfPCell(phsjimi);
PdfPCell pcPeople = newPdfPCell(phsPeople);
PdfPCell pcPage = newPdfPCell(phsPage);
PdfPCell pcPrintDate = newPdfPCell(phsPrintDate);
PdfPCell pcnull = newPdfPCell();
pchead.Border = 0;
pcjimi.Border = 0;
pcPeople.Border = 0;
pcPage.Border = 0;
pcPrintDate.Border = 0;
pcnull.Border = 0;
pcPeople.PaddingBottom = 10;
pcPrintDate.PaddingBottom = 10;
Phrase phead = newPhrase();
PdfPTable thead = new PdfPTable(2);
thead.AddCell(pchead);
thead.AddCell(pcjimi);
thead.AddCell(pcnull);
thead.AddCell(pcPage);
thead.AddCell(pcPeople);
thead.AddCell(pcPrintDate);
thead.SetWidths(new float[] { PageSize.A4.Width, 200});
phead.Add(thead);
returnphead;
}
/// <summary>
///页尾
/// </summary>
/// <returns></returns>
publicHeaderFooter Foot()
{
string strFoot = @"XXXXXXXXXXXXXXXXXX";
Phrase pfoot = new Phrase(strFoot, PdfFont.Font(2));
HeaderFooter foot = new HeaderFooter(pfoot, false);
foot.Border =Rectangle.NO_BORDER;
returnfoot;
}
/// <summary>
///文字类型定义
/// </summary>
/// <param name="IType">返回文字类别</param>
/// <returns></returns>
public static Font Font(intIType)
{
BaseFont.AddToResourceSearch("iTextAsian.dll");
BaseFont.AddToResourceSearch("iTextAsianCmaps.dll");
BaseFont bf = BaseFont.CreateFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);
//简体?繁体不能使用
//
Font font = new Font(bf);//普通文章
font.Size = 10;
Font fontFoot = new Font(bf);//頁脚文字
fontFoot.Size = 7;
fontFoot.Color =Color.RED;
Font fontNormal = new Font(bf);//正常文字
fontNormal.Size = 7;
switch(IType)
{
case 1: returnfont;
case 2: returnfontFoot;
case 3: returnfontNormal;
default: returnfontNormal;
}
}
4:主要内容的制作根据数据源,利用PdfPTable和Phase等制作,块等我还没用会,主要是样式和定位没搞会下面的博客有在Asp.Net中操作PDF – iTextSharp -利用块,短语,段落添加文本的方法http://www.cnblogs.com/CareySon/archive/2011/11/03/2234625.html5:水印的添加
/// <summary>
///為生成的pdf添加水印
/// </summary>
/// <param name="inputfilepath"></param>
/// <param name="outputfilepath"></param>
/// <param name="top"></param>
/// <param name="left"></param>
/// <returns></returns>
public bool PDFWatermark(string inputfilepath, string outputfilepath, float top, float left, stringstrName)
{
PdfReader pdfReader = null;
PdfStamper pdfStamper = null;
try{
pdfReader = newPdfReader(inputfilepath);
int numberOfPages =pdfReader.NumberOfPages;
iTextSharp.text.Rectangle psize = pdfReader.GetPageSize(1);
float width =psize.Width;
float height =psize.Height;
pdfStamper = new PdfStamper(pdfReader, newFileStream(outputfilepath, FileMode.Create));
PdfContentByte waterMarkContent;
WatermarkCreater wmc = newWatermarkCreater();
Draw.Image image =wmc.GetImageByte(strName, AppConfigString.WaterMarkPath);
var img =Image.GetInstance(image, System.Drawing.Imaging.ImageFormat.Png);
if (left < 0)
{
left = width - image.Width +left;
}
img.SetAbsolutePosition(left, (height - image.Height) -top);
//每一页加水印,也可以设置某一页加水印
for (int i = 1; i <= numberOfPages; i++)
{
waterMarkContent =pdfStamper.GetUnderContent(i);
waterMarkContent.AddImage(img);
}
return true;
}
catch(Exception ex)
{
ex.Message.Trim();
return false;
}
finally{
if (pdfStamper != null)
pdfStamper.Close();
if (pdfReader != null)
pdfReader.Close();
//把添加水印前的pdf文件刪除,保存最新的文件
if(File.Exists(inputfilepath))
{
File.Delete(inputfilepath);
}
}
}