CSharp: iTextSharp 5.13.2 create pdf
using System; using System.Collections.Generic; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using iTextSharp.text; using iTextSharp.awt; using iTextSharp.javaone; using iTextSharp.tool; using iTextSharp.xmp; using iTextSharp.xtra; using iTextSharp.text.pdf; using System.util; using System.IO; using System.Diagnostics; namespace DuiTextCSharpDemoVS2010 { /// <summary> /// geovindu Geovin Du /// 涂聚文 /// iTextSharp5 5.13.2 /// </summary> public partial class _Default : System.Web.UI.Page { string fontfile = "/resources/fonts/SIMLI.TTF"; private BaseFont dfont ;// = BaseFont.CreateFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED); private BaseFont bfChinese = BaseFont.CreateFont("C://WINDOWS//Fonts//simsun.ttc,0", BaseFont.IDENTITY_H, false);//BaseFont.NOT_EMBEDDED /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void Page_Load(object sender, EventArgs e) { dfont = BaseFont.CreateFont(@System.Web.HttpContext.Current.Server.MapPath("resources/fonts/SIMLI.TTF"), "Identity-H", true); //Server.MapPath(fontfile) // iTextSharp.text.Font dufont = new iTextSharp.text.Font(dfont, 30, iTextSharp.text.Font.BOLD); iTextSharp.text.Font cnfont = new iTextSharp.text.Font(bfChinese, 30, iTextSharp.text.Font.BOLD); if (!IsPostBack) { try { //Create document Document doc = new Document(); //Create PDF Table PdfPTable tableLayout = new PdfPTable(4); //Create a PDF file in specific path PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(Server.MapPath("geovindu.pdf"), FileMode.Create)); // writer.PageEvent = new DuEventIsHand(); //Open the PDF document doc.Open(); //doc.AddLanguage(""); doc.AddAuthor("geovindu, Geovin Du,涂聚文"); doc.AddCreator("涂聚文 itextsharp"); doc.AddKeywords("涂聚文itextsharp"); doc.AddSubject("涂聚文itextsharp"); doc.AddTitle("涂聚文itextsharp"); // // PdfDocument pdf = new PdfDocument(); //PdfFont font = new PdfFont(); //Add Content to PDF doc.Add(Add_Content_To_PDF(tableLayout)); // Closing the document doc.Close(); } catch (Exception ex) { Response.Write(ex.Message.ToString()); } } } /// <summary> /// /// </summary> /// <param name="tableLayout"></param> /// <returns></returns> private PdfPTable Add_Content_To_PDF(PdfPTable tableLayout) { // BaseFont bfChinese = BaseFont.CreateFont("C://WINDOWS//Fonts//simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); // iTextSharp.text.Font fontChinese_11 = new iTextSharp.text.Font(bfChinese, 11, iTextSharp.text.Font.BOLD, new iTextSharp.text.BaseColor(0, 0, 0)); dfont = BaseFont.CreateFont(@System.Web.HttpContext.Current.Server.MapPath("resources/fonts/SIMLI.TTF"),"Identity-H", true); // iTextSharp.text.Font dufont = new iTextSharp.text.Font(dfont, 30, iTextSharp.text.Font.BOLD); iTextSharp.text.Font cnfont = new iTextSharp.text.Font(bfChinese, 30, iTextSharp.text.Font.BOLD); float[] headers = { 20, 20, 30, 30 }; //Header Widths tableLayout.SetWidths(headers); //Set the pdf headers tableLayout.WidthPercentage = 80; //Set the PDF File witdh percentage //Add Title to the PDF file at the top //tableLayout.AddCell(new PdfPCell(new Phrase("Creating PDF file using iTextsharp 生成PDF文档", cnfont))); //Add header AddCellToHeader(tableLayout, "姓名"); AddCellToHeader(tableLayout, "高度"); AddCellToHeader(tableLayout, "出生"); AddCellToHeader(tableLayout, "父母"); //Add body AddCellToBody(tableLayout, "Sachin Tendulkar Geovin Du 涂聚文"); AddCellToBody(tableLayout, "1.65 m"); AddCellToBody(tableLayout, "April 24, 1973"); AddCellToBody(tableLayout, "Ramesh Tendulkar, Rajni Tendulkar"); AddCellToBody(tableLayout, "Mahendra Singh Dhoni"); AddCellToBody(tableLayout, "1.75 m"); AddCellToBody(tableLayout, "July 7, 1981"); AddCellToBody(tableLayout, "Devki Devi, Pan Singh"); AddCellToBody(tableLayout, "Virender Sehwag"); AddCellToBody(tableLayout, "1.70 m"); AddCellToBody(tableLayout, "October 20, 1978"); AddCellToBody(tableLayout, "Aryavir Sehwag, Vedant Sehwag"); AddCellToBody(tableLayout, "Virat Kohli"); AddCellToBody(tableLayout, "1.75 m"); AddCellToBody(tableLayout, "November 5, 1988"); AddCellToBody(tableLayout, "Saroj Kohli, Prem Kohli"); return tableLayout; } /// <summary> /// Method to add single cell to the header /// </summary> /// <param name="tableLayout"></param> /// <param name="cellText"></param> private void AddCellToHeader(PdfPTable tableLayout, string cellText) { iTextSharp.text.Font cnfont = new iTextSharp.text.Font(bfChinese, 30, iTextSharp.text.Font.BOLD); dfont = BaseFont.CreateFont(@System.Web.HttpContext.Current.Server.MapPath("resources/fonts/SIMLI.TTF"), "Identity-H", true); iTextSharp.text.Font dufont = new iTextSharp.text.Font(dfont, 30, iTextSharp.text.Font.BOLD); tableLayout.AddCell(new PdfPCell(new Phrase(cellText, cnfont))); //new Font(Font.HELVETICA, 8, 1, iTextSharp.text.Color.WHITE) } /// <summary> /// Method to add single cell to the body /// </summary> /// <param name="tableLayout"></param> /// <param name="cellText"></param> private void AddCellToBody(PdfPTable tableLayout, string cellText) { iTextSharp.text.Font cnfont = new iTextSharp.text.Font(bfChinese, 30, iTextSharp.text.Font.BOLD); dfont = BaseFont.CreateFont(@System.Web.HttpContext.Current.Server.MapPath("resources/fonts/SIMLI.TTF"), "Identity-H", true); iTextSharp.text.Font dufont = new iTextSharp.text.Font(dfont, 30, iTextSharp.text.Font.BOLD); tableLayout.AddCell(new PdfPCell(new Phrase(cellText, cnfont))); // new Font(Font.HELVETICA, 8, 1, iTextSharp.text.Color.BLACK) } } }
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using iTextSharp.text; using iTextSharp.awt; using iTextSharp.javaone; using iTextSharp.tool; using iTextSharp.xmp; using iTextSharp.xtra; using iTextSharp.text.pdf; using System.util; using System.IO; using System.Diagnostics; using com.itextpdf.text.pdf; using Org.BouncyCastle; using System.Text; namespace DuiTextCSharpdemo { /// <summary> /// geovindu Geovin Du /// </summary> public partial class _Default : System.Web.UI.Page { string fontfile = "/resources/fonts/SIMLI.TTF"; private BaseFont dfont;// = BaseFont.CreateFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED); private BaseFont bfChinese = BaseFont.CreateFont("C://WINDOWS//Fonts//simsun.ttc,0", BaseFont.IDENTITY_H, false);//BaseFont.NOT_EMBEDDED byte[] userPassWord = Encoding.ASCII.GetBytes("geovindu"); //UTF8 ok byte[] OwnerPassWord = Encoding.ASCII.GetBytes("geovindu"); /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void Page_Load(object sender, EventArgs e) { dfont = BaseFont.CreateFont(@System.Web.HttpContext.Current.Server.MapPath("resources/fonts/SIMLI.TTF"), "Identity-H", true); //Server.MapPath(fontfile) // iTextSharp.text.Font dufont = new iTextSharp.text.Font(dfont, 30, iTextSharp.text.Font.BOLD); iTextSharp.text.Font cnfont = new iTextSharp.text.Font(bfChinese, 30, iTextSharp.text.Font.BOLD); if (!IsPostBack) { try { //Create document Document doc = new Document(); //Create PDF Table PdfPTable tableLayout = new PdfPTable(4); //Create a PDF file in specific path PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(Server.MapPath("geovindu.pdf"), FileMode.Create)); writer.SetEncryption(userPassWord, OwnerPassWord, PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128); //Open the PDF document doc.Open(); // //writer.PageEvent = new DuEventIsHand(); DateTime _dtm = DateTime.Now; writer.PageEvent = new HeaderAndFooterEvent(); HeaderAndFooterEvent.PAGE_NUMBER = true;//实现页眉跟页脚 HeaderAndFooterEvent.tpl = writer.DirectContent.CreateTemplate(500, 500); //定义模板 HeaderAndFooterEvent.HeaderLeft = "涂聚文"; HeaderAndFooterEvent.HeaderRight = "文件制作系统中心"; HeaderAndFooterEvent.FooterLeft = "TEL:010-87922095"; HeaderAndFooterEvent.FooterRight = _dtm.ToString("yyyy-MM-dd HH:mm:ss") + "(UTC)"; //doc.AddLanguage(""); doc.AddAuthor("geovindu, Geovin Du,涂聚文"); doc.AddCreator("涂聚文 itextsharp"); doc.AddKeywords("涂聚文itextsharp"); doc.AddSubject("涂聚文itextsharp"); doc.AddTitle("涂聚文itextsharp"); // // PdfDocument pdf = new PdfDocument(); //PdfFont font = new PdfFont(); //Add Content to PDF doc.Add(Add_Content_To_PDF(tableLayout)); // Closing the document doc.Close(); } catch (Exception ex) { Response.Write(ex.Message.ToString()); } } } /// <summary> ///geovindu 涂聚文 /// </summary> /// <param name="tableLayout"></param> /// <returns></returns> private PdfPTable Add_Content_To_PDF(PdfPTable tableLayout) { // BaseFont bfChinese = BaseFont.CreateFont("C://WINDOWS//Fonts//simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); // iTextSharp.text.Font fontChinese_11 = new iTextSharp.text.Font(bfChinese, 11, iTextSharp.text.Font.BOLD, new iTextSharp.text.BaseColor(0, 0, 0)); dfont = BaseFont.CreateFont(@System.Web.HttpContext.Current.Server.MapPath("resources/fonts/SIMLI.TTF"), "Identity-H", true); // iTextSharp.text.Font dufont = new iTextSharp.text.Font(dfont, 30, iTextSharp.text.Font.BOLD); iTextSharp.text.Font cnfont = new iTextSharp.text.Font(bfChinese, 30, iTextSharp.text.Font.BOLD); float[] headers = { 20, 20, 30, 30 }; //Header Widths tableLayout.SetWidths(headers); //Set the pdf headers tableLayout.WidthPercentage = 80; //Set the PDF File witdh percentage //Add Title to the PDF file at the top tableLayout.AddCell(new PdfPCell(new Phrase("Creating PDF file using iTextsharp 生成PDF文档", cnfont))); //Add header AddCellToHeader(tableLayout, "姓名"); AddCellToHeader(tableLayout, "高度"); AddCellToHeader(tableLayout, "出生"); AddCellToHeader(tableLayout, "父母"); //Add body AddCellToBody(tableLayout, "Sachin Tendulkar Geovin Du 涂聚文"); AddCellToBody(tableLayout, "1.65 m"); AddCellToBody(tableLayout, "April 24, 1973"); AddCellToBody(tableLayout, "Ramesh Tendulkar, Rajni Tendulkar"); AddCellToBody(tableLayout, "Mahendra Singh Dhoni"); AddCellToBody(tableLayout, "1.75 m"); AddCellToBody(tableLayout, "July 7, 1981"); AddCellToBody(tableLayout, "Devki Devi, Pan Singh"); AddCellToBody(tableLayout, "Virender Sehwag"); AddCellToBody(tableLayout, "1.70 m"); AddCellToBody(tableLayout, "October 20, 1978"); AddCellToBody(tableLayout, "Aryavir Sehwag, Vedant Sehwag"); AddCellToBody(tableLayout, "Virat Kohli"); AddCellToBody(tableLayout, "1.75 m"); AddCellToBody(tableLayout, "November 5, 1988"); AddCellToBody(tableLayout, "Saroj Kohli, Prem Kohli"); return tableLayout; } /// <summary> /// Method to add single cell to the header /// </summary> /// <param name="tableLayout"></param> /// <param name="cellText"></param> private void AddCellToHeader(PdfPTable tableLayout, string cellText) { iTextSharp.text.Font cnfont = new iTextSharp.text.Font(bfChinese, 30, iTextSharp.text.Font.BOLD); dfont = BaseFont.CreateFont(@System.Web.HttpContext.Current.Server.MapPath("resources/fonts/SIMLI.TTF"), "Identity-H", true); iTextSharp.text.Font dufont = new iTextSharp.text.Font(dfont, 30, iTextSharp.text.Font.BOLD); tableLayout.AddCell(new PdfPCell(new Phrase(cellText, cnfont))); //new Font(Font.HELVETICA, 8, 1, iTextSharp.text.Color.WHITE) } /// <summary> /// Method to add single cell to the body /// </summary> /// <param name="tableLayout"></param> /// <param name="cellText"></param> private void AddCellToBody(PdfPTable tableLayout, string cellText) { iTextSharp.text.Font cnfont = new iTextSharp.text.Font(bfChinese, 30, iTextSharp.text.Font.BOLD); dfont = BaseFont.CreateFont(@System.Web.HttpContext.Current.Server.MapPath("resources/fonts/SIMLI.TTF"), "Identity-H", true); iTextSharp.text.Font dufont = new iTextSharp.text.Font(dfont, 30, iTextSharp.text.Font.BOLD); tableLayout.AddCell(new PdfPCell(new Phrase(cellText, cnfont))); // new Font(Font.HELVETICA, 8, 1, iTextSharp.text.Color.BLACK) } } }
PdfPCell cell = new PdfPCell(); cell.Phrase = new Phrase("涂聚文 生成PDF文档"); cell.Colspan=4;//合并单元格 tableLayout.AddCell(cell); PdfPCell cell2 = new PdfPCell(new Phrase("Creating PDF file using iTextsharp ", cnfont)); cell2.Colspan = 4;//合并单元格 tableLayout.AddCell(cell2);
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)