Click here to Skip to main content
15,885,917 members
Please Sign up or sign in to vote.
1.67/5 (2 votes)
See more:
Hi there ! i got a problem with my project about Unicode char ! plzz help me !
C#
HtmlForm form = new HtmlForm();
        string imagepath = Server.MapPath("img");
        form.Controls.Add(GroupingGridView1);
        StringWriter sw = new StringWriter();
        Response.ContentEncoding = System.Text.Encoding.UTF8;

        HtmlTextWriter hTextWriter = new HtmlTextWriter(sw);
        form.Controls[0].RenderControl(hTextWriter);
        string html = HttpUtility.HtmlDecode(sw.ToString());
        string pathHTML = "c:\\test.html";
        StreamWriter filehtml = new System.IO.StreamWriter(pathHTML);
        filehtml.Write(html);
        filehtml.Dispose();
        Document Doc = new Document(PageSize.A4);
        
        string Path = Environment.GetFolderPath
        (Environment.SpecialFolder.Desktop)
        + "\\baocao.pdf";
        PdfWriter.GetInstance
        (Doc, new FileStream(Path, FileMode.Create));
        Doc.Open();

        iTextSharp.text.Image clogo = iTextSharp.text.Image.GetInstance(imagepath + "/slogo.jpg");
        clogo.Alignment = iTextSharp.text.Image.ALIGN_LEFT;
        BaseFont bf = BaseFont.CreateFont(@"C:\WINDOWS\Fonts\TIMES.TTF", BaseFont.IDENTITY_H, true);
        iTextSharp.text.Font fonttitle = new iTextSharp.text.Font(bf, 15);
        iTextSharp.text.Font font = new iTextSharp.text.Font(bf, 10);
        iTextSharp.text.Font fonttext = new iTextSharp.text.Font(bf, 12);


        Paragraph header = new Paragraph();
        header.Alignment = Element.ALIGN_CENTER;
        Paragraph text = new Paragraph();

        Chunk c1 = new Chunk("Báo cáo thống kê thiết bị \n", fonttitle);
        Chunk c2 = new Chunk(DateTime.Now.ToShortDateString() + "\n\n", font);
        header.Add(c1);
        header.Add(c2);
        Doc.Add(clogo);
        Doc.Add(header);
        List<IElement> objects;
        var stream = new StreamReader(pathHTML, Encoding.UTF8);//.ReadToEnd();
        
        objects = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(stream, new StyleSheet());
        Doc.NewPage();
        for (int k = 0; k < objects.Count; k++)
        {
            PdfPTable table = new PdfPTable(1);

            table.RunDirection = PdfWriter.RUN_DIRECTION_RTL;
            var els = (IElement)objects[k];
            //i can not add UNICODE when I ADD to DOCUMENT
            Doc.Add((IElement)objects[k]);           
        }  
            stream.Dispose();
            Doc.Close();

        Response.ClearContent();
        Response.ClearHeaders();
        Response.ContentType = "application/pdf";
        Response.AddHeader
        ("Content-Disposition", "attachment; filename=" + strS);
        Response.TransmitFile(strS);
        Response.End();
        //Response.WriteFile(strS);
        Response.Flush();
        Response.Clear();
Posted
Updated 25-Oct-11 20:52pm
v2
Comments
Richard MacCutchan 26-Oct-11 7:44am    
What problem? Please explain which part of your code is not working and why.
Marc A. Brown 26-Oct-11 9:42am    
You have a comment in your code saying that you cannot add unicode when you add to document; however, you haven't said what happens. Exception? The content just doesn't show up? Computer explodes? Cats and dogs live in sin? Seriously, you need to specify what exactly goes wrong since it's unlikely that someone is going to take the time to create a test project to run your code in.
Hua Trung 26-Oct-11 23:13pm    
hi Marc A. Brown and Richard MacCutchan ! it creat a pdf not exception ! my font in pdf is broken at some chars like 'ộ' 'ế' ..etc.. !
in my CODE this line:
Chunk c1 = new Chunk("Báo cáo thống kê thiết bị \n", fonttitle);
it show right format ! and it not broken. i can not add a pdftable (unicode format) to my documentlike this CODE:
for (int k = 0; k < objects.Count; k++)
{
PdfPTable table = new PdfPTable(1);

table.RunDirection = PdfWriter.RUN_DIRECTION_RTL;
var els = (IElement)objects[k];
//i can not add UNICODE when I ADD to DOCUMENT
Doc.Add((IElement)objects[k]);
}

plzzz help me ! thanx for reply !

1 solution

 
Share this answer
 
Comments
Hua Trung 27-Oct-11 5:07am    
Dear Mehdi Gholam ! i know to add font with a Paragraph , Pharase, but i dun know how to add font with a IElement - PdfPTable, when i debuged my code, i saw my stream variable,it contain unicode, and when i convert to a PdfPTable (i dun know how to access row value- and my pdf it not contain a UNICODE CHARS , could u help me plz ! :((

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900