6

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

        }
    }
}
user487533
  • 83
  • 1
  • 6

3 Answers3

11
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..

Serguzest
  • 1,287
  • 10
  • 18
  • After 5 days headache, I think you are absolutely right about reporting tools. – efirat Jul 04 '12 at 12:03
  • 1
    what is the "pdftable". every one is pasting this solution to everywhere. how can i set that font to the my pdf doc. Can you explain that what is the "pdftable" ???? – Mahmut EFE Jan 15 '15 at 12:23
  • @MahmutEFE I don't remember and it is not the point in the question. The point is you need to pass your turkish font when necessary. – Serguzest Jan 15 '15 at 17:07
  • This is working on standalone build but not on Android device. Any advice how to use Turkish characters on Android device ? – Çağatay Kaya Mar 02 '20 at 14:54
0

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
  • 10,650
  • 8
  • 33
  • 35
-1
PdfFont font = PdfFontFactory.CreateFont("Helvetica", "CP1254",PdfFontFactory.EmbeddingStrategy.FORCE_NOT_EMBEDDED);
doc.Add(new Paragraph("P").SetFont(font));
  • Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, **can you [edit] your answer to include an explanation of what you're doing** and why you believe it is the best approach? – Jeremy Caney May 07 '22 at 02:03