CSharp: iTextSharp 5.13.2 create pdf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | 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) } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | 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) } } } |
1 2 3 4 5 6 7 | 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(涂聚文)
分类:
CSharp code
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
2018-01-27 JqGrid: Add,Edit,Del in asp.net