the source code of cjkdemo.aspx on my free asp.net hosting

Recently, there are many people asked the source code of cjkdemo.aspx on my free asp.net hosting, so I decide release it here, there is no special.
to run it, you need iTextSharp, iTextAsian-1.0.dll and iTextAsianCmaps-1.0.dll, you can download them from http://itextsharp.sourceforge.net/.

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
 
using iTextSharp.text;
using iTextSharp.text.pdf;
 
public partial class CJKDemo : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
 
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        txtMessage.Text = string.Empty;
    }
    protected void btnConvert_Click(object sender, EventArgs e)
    {
        try
        {
            Document document = new Document();
            //Document.Compress = false;
            try
            {
                Response.Cache.SetCacheability(HttpCacheability.Public);
                Response.Cache.SetExpires(DateTime.Now.AddSeconds(5));
                Response.ContentType = "application/pdf";
                Response.AddHeader("content-disposition", "attachment; filename=CJKDemo.pdf");
                //BaseFont.AddToResourceSearch(MapPath(@".\itextsharpasian\") + "iTextAsian-1.0.dll");
                //BaseFont.AddToResourceSearch(MapPath(@".\itextsharpasian\") + "iTextAsianCmaps-1.0.dll");
                PdfWriter.GetInstance(document, Response.OutputStream);
 
                //PdfWriter.GetInstance(document, new FileStream(@"E:\java\Japanese.pdf", FileMode.Create));
                document.Open();
 
                BaseFont bf = BaseFont.CreateFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
 
                Font font1 = new Font(bf, 12, Font.NORMAL);
                Paragraph par = null;
                ChapterAutoNumber charp = null;
				 par = new Paragraph("iTextSharp支持中文输出", font1);
				 charp = new ChapterAutoNumber(par);
                for (int i = 0; i < 20; i++)
                {   
                    document.Add(charp);
		            document.Add(new Paragraph(txtMessage.Text, font1));	                		                		                
                }
 
 
            }
            catch (Exception ex)
            {
                Response.Clear();
                Response.ContentType = "text/plain";
                Response.Write(ex.Message);
                return;
            }
            document.Close();
        }
        catch (Exception)
        {
            throw;
        }
        Response.End();
    }
}

Leave a Reply

You must be logged in to post a comment.