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.1.32 /// </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) } } }