2011-03-01 52 views
1

渲染我使用iTextSharp的在asp.net mvc的,以这样的返回PDF:A S牛逼字符不被iTextSharp的

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"); 
      } 
     } 
    } 

的问题是,像字符:ă ţ ş不会呈现

+0

您是否尝试过使用十六进制值?你是否试过编码来支持这些字符? iTextSharp使用了'Ø''Ø'和'Å'。 – balexandre 2011-03-01 14:26:00

+0

这会有帮助吗? http://stackoverflow.com/questions/1322303/html-to-pdf-some-characters-are-missing-itextsharp – naveen 2011-03-01 14:26:47

+0

这不是一个答案,而是关于上述代码的观察(不涉及你的问题,但它可能在将来帮助你):ms.GetBuffer()可以在某些情况下生成受损的PDF文件,你可以在这里阅读更多细节:http://stackoverflow.com/questions/5109384/itextsharp-generated-pdfs-now-cause -save-dialog-in-adobe-reader-x – iPDFdev 2011-03-01 14:29:41

回答

1

请尝试如下因素:

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

它应该这样做您。 您必须将字段属性添加到您希望具有变音符号的每个字段。