Darshan Kshirasagar

Darshan Kshirasagar

  • NA
  • 317
  • 68.9k

iTextSharp PDF Unicode Not Showing properly

Feb 1 2017 12:52 AM

 Hello ,
 
Please Check Below is when i convert HTML Text to PDF using iTextSharp Unicode are not coming properly.
 
 
 
 My Code Below:
public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
}
public class UnicodeFontFactory : FontFactoryImp
{
private static readonly string FontPath = Path.Combine("C:\\Windows\\Fonts\\","ARIALUNI.TTF");

private readonly BaseFont _baseFont;

public UnicodeFontFactory()
{
_baseFont = BaseFont.CreateFont(FontPath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
iTextSharp.text.Font fontNormal = new iTextSharp.text.Font(_baseFont, 10, iTextSharp.text.Font.NORMAL);

//_baseFont = BaseFont.CreateFont(Environment.GetEnvironmentVariable("windir") + @"\fonts\ARIALUNI.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//Font NormalFont = new iTextSharp.text.Font(_baseFont, 10, Font.NORMAL, iTextSharp.text.BaseColor.BLACK);
}

public override Font GetFont(string fontname, string encoding, bool embedded, float size, int style, BaseColor color,
bool cached)
{
return new Font(_baseFont, size, style, color);
}
}

//First Amazing Working Now
protected void btnExport_Click(object sender, EventArgs e)
{
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
pnlPerson.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());

string filename = "PaySlip123";
string outXml = sw.ToString();
outXml = outXml.Replace("px", "");
outXml = outXml.Replace("<br>", "<br/>");
outXml = outXml.Replace("&nbsp", " ");

var css = ".tHead{font-family: \"Arial_Unicode_MS\";font-size: 12px;line-height: 14px;} "+
".tBody{page-break-after: always; font-family: \"Arial_Unicode_MS\"; font-size: 12px; line-height: 14px;} " +
".dPrint{font-family: \"Courier-New\"; font-size: 15px;line-height: 5px;} "+
" td.LineStyleTopBottom{border-top : 1px dashed black; border-bottom: 1px dashed black;padding-top:5px;padding-bottom:5px;} "+
" td.LineStyleTop{border-top : 1px dashed black; padding-top:5px;padding-bottom:5px;} "+
" td.LineStyleBottom{border-bottom: 1px dashed black;padding-top:5px;padding-bottom:5px;}"+
" .Ellipsis {display: table-cell;max-width:1px;white-space: nowrap;text-overflow:ellipsis;overflow: hidden;}";

MemoryStream memStream = new MemoryStream();
TextReader xmlString = new StringReader(outXml);
using (Document document = new Document(PageSize.A4,0,0,0,0))
{


PdfWriter writer = PdfWriter.GetInstance(document, memStream);
document.Open();

var msCSS = new MemoryStream(Encoding.UTF8.GetBytes(css));

//FontFactory.Register("C:\\Windows\\Fonts\\ARIALUNI.TTF", "arial unicode ms");
//BaseFont bf = BaseFont.CreateFont(Environment.GetEnvironmentVariable("windir") + @"\fonts\ARIALUNI.TTF", BaseFont.CP1252, BaseFont.EMBEDDED);
//Font font = new Font(bf, 10);

byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(outXml);
MemoryStream ms = new MemoryStream(byteArray);
XMLWorkerHelper.GetInstance().ParseXHtml(writer, document, ms, msCSS, System.Text.Encoding.UTF8, new UnicodeFontFactory());

document.Close();
}

Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=" + filename + ".pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.BinaryWrite(memStream.ToArray());
Response.End();
Response.Flush();

 
 
 

Answers (1)