ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

CSharp: iTextSharp 5.13.2 create pdf

2022-01-27 20:03:11  阅读:127  来源: 互联网

标签:text AddCellToBody 5.13 iTextSharp tableLayout new pdf Font


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)
 
 
        }
 
    }
}

标签:text,AddCellToBody,5.13,iTextSharp,tableLayout,new,pdf,Font
来源: https://blog.csdn.net/geovindu/article/details/122722839

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有