views:

40

answers:

2

Hello All, I want to convert a ASP.NET web page to pdf using ITextSharp. I did write some code but I can not make it show the Turkish Characters. Can anyone help me?

Here is the code:

using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;

using System.Web.UI;
using System.Web;
using iTextSharp.text.html.simpleparser;
using System.Text;
using System.Text.RegularExpressions;

namespace Presentation
{
    public partial class TemporaryStudentFormPrinter : System.Web.UI.Page
    {
        protected override void Render(HtmlTextWriter writer)
        {
            MemoryStream mem = new MemoryStream();
            StreamWriter twr = new StreamWriter(mem);
            HtmlTextWriter myWriter = new HtmlTextWriter(twr);
            base.Render(myWriter);
            myWriter.Flush();
            myWriter.Dispose();
            StreamReader strmRdr = new StreamReader(mem);
            strmRdr.BaseStream.Position = 0;
            string pageContent = strmRdr.ReadToEnd();
            strmRdr.Dispose();
            mem.Dispose();
            writer.Write(pageContent);
            CreatePDFDocument(pageContent);
        }
        public void CreatePDFDocument(string strHtml)
        {
            string strFileName = HttpContext.Current.Server.MapPath("test.pdf");
            Document document = new Document(PageSize.A4, 80, 50, 30, 65);
            PdfWriter.GetInstance(document, new FileStream(strFileName, FileMode.Create));

            StringReader se = new StringReader(strHtml);
            HTMLWorker obj = new HTMLWorker(document);

            document.Open();

            obj.Parse(se);
            document.Close();
            ShowPdf(strFileName);
        }
        public void ShowPdf(string strFileName)
        {
            Response.ClearContent();
            Response.ClearHeaders();
            Response.AddHeader("Content-Disposition", "inline;filename=" + strFileName);
            Response.ContentType = "application/pdf";
            Response.WriteFile(strFileName);
            Response.Flush();
            Response.Clear();
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }
    }
}
A: 

You'll need to make sure you are writing the text in a Font that supports the Turkish character set (or at least the caracters you are trying to write out).

I don't know what HtmlTextWriter does in terms of font use - it will presumably use one of the standard built-in fonts that are unlikely to support the characters you want to print if the fall outside of the Latin1 or Latin1-extended Unicode ranges.

I use BaseFont.createFont(...) to have an external Font included in my PDF in iText (Java) - one which supports all the characters that I am writing. You might be able to create your Font object and then pass it to HtmlTextWriter?

Steve Claridge
Let me try, thank you.
It's not working I suppose. Any other ideas?
A: 
iTextSharp.text.pdf.BaseFont STF_Helvetica_Turkish = iTextSharp.text.pdf.BaseFont.CreateFont("Helvetica", "CP1254", iTextSharp.text.pdf.BaseFont.NOT_EMBEDDED);

iTextSharp.text.Font fontNormal = new iTextSharp.text.Font(STF_Helvetica_Turkish, 12, iTextSharp.text.Font.NORMAL);

You should pass the font as an argument in itextsharp manipulation commands like that :

pdftable.AddCell(new Phrase(nn.InnerText.Trim(), fontNormal));

You might want to consider working with reporting tools with pdf exporting capability instead of direclty working with pdf which can be a real headache..

AhmetC