1

I use iTextSharp in asp.net mvc to return pdf like this:

public class Pdf : IPdf
    {
        public FileStreamResult Make(string s)
        {
            using (var ms = new MemoryStream())
            {
                using (var document = new Document())
                {
                    PdfWriter.GetInstance(document, ms);
                    document.Open();
                    using (var str = new StringReader(s))
                    {
                        var htmlWorker = new HTMLWorker(document);

                        htmlWorker.Parse(str);
                    }
                    document.Close();
                }

                HttpContext.Current.Response.ContentType = "application/pdf";
                HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=MyPdfName.pdf");
                HttpContext.Current.Response.Buffer = true;
                HttpContext.Current.Response.Clear();
                HttpContext.Current.Response.OutputStream.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length);
                HttpContext.Current.Response.OutputStream.Flush();
                HttpContext.Current.Response.End();

                return new FileStreamResult(HttpContext.Current.Response.OutputStream, "application/pdf");
            }
        }
    }

the problem is that characters like: ă ţ ş are not rendered

bzlm
  • 9,626
  • 6
  • 65
  • 92
Omu
  • 69,856
  • 92
  • 277
  • 407
  • did you tried using the hexadecimal value? did you tried for the encoding to support such characters? I use `Æ` `Ø` and `Å` very well with iTextSharp. – balexandre Mar 01 '11 at 14:26
  • will this help? http://stackoverflow.com/questions/1322303/html-to-pdf-some-characters-are-missing-itextsharp – naveen Mar 01 '11 at 14:26
  • This is not an answer, but an observation regarding the code above (not related to your question but it might help you in the future): the ms.GetBuffer() can generate damaged PDF file in some situations, you can read here more details: http://stackoverflow.com/questions/5109384/itextsharp-generated-pdfs-now-cause-save-dialog-in-adobe-reader-x – iPDFdev Mar 01 '11 at 14:29
  • @balexandre your chars work for me too, how would I use the hexadecimal value ? – Omu Mar 01 '11 at 14:32
  • did you tried change the encoding as I said? `HttpContext.Current.Response.ContentEncoding = "ISO-8859-16";` --> http://en.wikipedia.org/wiki/ISO/IEC_8859-16 – balexandre Mar 01 '11 at 14:57
  • @balexandre tried this `CultureInfo ci = new CultureInfo("ro-RO"); Encoding enc = Encoding.GetEncoding(ci.TextInfo.ANSICodePage); Response.ContentEncoding = enc;` doesn't help – Omu Mar 01 '11 at 15:10

2 Answers2

1

yetanothercoder is correct. That would do the trick... but there's another very similar question which I answered in a bit more detail:

iText + HTMLWorker - How to change default font?

Community
  • 1
  • 1
Mark Storer
  • 15,672
  • 3
  • 42
  • 80
1

Please try the folowing:

var unicodeFont = iTextSharp.text.pdf.BaseFont.CreateFont(iTextSharp.text.FontFactory.TIMES_ROMAN, iTextSharp.text.pdf.BaseFont.CP1250, iTextSharp.text.pdf.BaseFont.EMBEDDED);
acroFields.SetFieldProperty("txtContractorBirthPlace", "textfont", unicodeFont, null);

And it should do it for you. You have to add the field property to every field you wish to have diacritics.

Baftă!

Dragos Durlut
  • 8,018
  • 10
  • 47
  • 62