Click here to Skip to main content
15,885,686 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
does not support any language other than English. show's empty text

What I have tried:

public void ExportToPDF(DataTable dt, string SheetName, string From, string To)
{
HttpContext.Current.Response.ContentType = "application/pdf";
HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + SheetName + ".pdf");
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
To = Convert.ToDateTime(To).AddDays(-1).ToString("yyyy-MM-dd");
string html = getHTMLtableFromDataTable(dt, SheetName, From, To);
System.IO.StringReader sr = new System.IO.StringReader(html);
//BaseFont bf = BaseFont.CreateFont(Environment.GetEnvironmentVariable("windir") + @"\fonts\Mangal.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
//iTextSharp.text.Font font = new iTextSharp.text.Font(bf, 10, iTextSharp.text.Font.NORMAL);

Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, HttpContext.Current.Response.OutputStream);

writer.ViewerPreferences = PdfWriter.PageModeUseOutlines;
PdfFooter eventHandler = new PdfFooter();
writer.PageEvent = eventHandler;

pdfDoc.Open();
//Chunk chunk = new Chunk();
//chunk.Font = font;
//pdfDoc.Add(new Paragraph("Paragraph - This is a test! ÇçĞğİıÖöŞşÜü", font));
//pdfDoc.Add(chunk);
//Chunk c = new Chunk();
//c.Font = font;
//c.Append("Turkish characters: ĞÜŞİÖÇ ğüşıöç");
//pdfDoc.Add(c);
htmlparser.Parse(sr);

pdfDoc.Close();
HttpContext.Current.Response.Write(pdfDoc);
HttpContext.Current.Response.End();
}

also tried following code

public void ExportToPDF(DataTable dt, string SheetName, string From, string To)
{
HttpContext.Current.Response.ContentType = "application/pdf";
HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + SheetName + ".pdf");
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
To = Convert.ToDateTime(To).AddDays(-1).ToString("yyyy-MM-dd");
string html = getHTMLtableFromDataTable(dt, SheetName, From, To);
System.IO.StringReader sr = new System.IO.StringReader(textConvert(html));
//BaseFont bf = BaseFont.CreateFont(Environment.GetEnvironmentVariable("windir") + @"\fonts\Mangal.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
//iTextSharp.text.Font font = new iTextSharp.text.Font(bf, 10, iTextSharp.text.Font.NORMAL);

Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, HttpContext.Current.Response.OutputStream);

writer.ViewerPreferences = PdfWriter.PageModeUseOutlines;
PdfFooter eventHandler = new PdfFooter();
writer.PageEvent = eventHandler;

pdfDoc.Open();
//Chunk chunk = new Chunk();
//chunk.Font = font;
//pdfDoc.Add(new Paragraph("Paragraph - This is a test! ÇçĞğİıÖöŞşÜü", font));
//pdfDoc.Add(chunk);
//Chunk c = new Chunk();
//c.Font = font;
//c.Append("Turkish characters: ĞÜŞİÖÇ ğüşıöç");
//pdfDoc.Add(c);
htmlparser.Parse(sr);

pdfDoc.Close();
HttpContext.Current.Response.Write(pdfDoc);
HttpContext.Current.Response.End();
}

public static string textConvert(string S)
{
if (S == null) { return null; }
try
{
Encoding encFrom = Encoding.UTF8;
Encoding encTo = Encoding.UTF8;
string str = S;
Byte[] b = encFrom.GetBytes(str);
return encTo.GetString(b);
}
catch { return null; }
}
Posted
Updated 8-Mar-16 22:24pm
Comments

1 solution

you need to reference an external unicode true-type font eg

C#
//Reference a Unicode font to be sure that the symbols are present. 
    BaseFont bfArialUniCode = BaseFont.CreateFont(@"drive:\path\ARIALUNI.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED); 
    //Create a font from the base font 
    Font font = new Font(bfArialUniCode, 12);


obviously the 'drive:\path\' needs to be correct - I've just shown that as a placeholder - it could be "C:\Windows\Fonts", whatever is correct for your machine - this could work

C#
const string arialunicodepath          = Environment.GetEnvironmentVariable( "SystemRoot" ) + "\\fonts\\ARIALUNI.TTF";


.... Another way to always ensure the font is where you want it is to deploy a copy of the font file to for example, the directory where you install your program and a subdirectory for fonts - gettit ?

[Edit] Putting it all together

C#
String uniText1 = @"Any Text You Want With Unicode Characters\n";
String uniText2 = @"Line 2 Of Text";
// Get Font File ? Windows Font Directory
const string arialunicodepath = Environment.GetEnvironmentVariable( "SystemRoot" ) + "\\fonts\\ARIALUNI.TTF";
// Reference Unicode font
BaseFont bfArialUniCode = BaseFont.CreateFont(arialunicodepath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
// Create Instance of font from the base 
Font font = new Font(bfArialUniCode, 12);
// Basic Elements = Chunks

Chunk c1 = new Chunk(uniText1, font);

Chunk c2 = new Chunk(uniText2, font);

// Chunks make Phrases

Phrase p1 = new Phrase(c1);

p1.Add(c2);

// Phrases Make Paragraphs

Paragraph p = new Paragraph();

p.Add(p1);

// Add Paragraph to doc (assuming you’ve done a doc.Open();)

doc.Add([p);

doc.Close();


[/Edit]
 
Share this answer
 
v3
Comments
aparnaChandras 9-Mar-16 4:51am    
how to assign this font to my pdfdoc
Garth J Lancaster 9-Mar-16 5:56am    
see my edit above - Ive applied the font to the chunks (chunks being the lowest element)
aparnaChandras 10-Mar-16 4:27am    
It's not working it still shows blank for another language code
Garth J Lancaster 10-Mar-16 19:34pm    
'not working' ? not helpful ... have you used a debugger ? have you tried the basic code using a standard font, ie, can you get anything non unicode to print first ?? (there is one small typo, doc.Add([p); should be doc.Add(p); ) show all the code you're using including the instantiation of the doc object (pdfDoc) in your case
aparnaChandras 11-Mar-16 0:01am    
public void ExportToPDF(DataTable dt, string SheetName, string From, string To)
{
HttpContext.Current.Response.ContentType = "application/pdf";
HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + SheetName + ".pdf");

HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
To = Convert.ToDateTime(To).AddDays(-1).ToString("yyyy-MM-dd");
string html = getHTMLtableFromDataTable(dt, SheetName, From, To);

System.IO.StringWriter stringWrite = new StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

System.IO.StringReader sr = new System.IO.StringReader(html);
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, HttpContext.Current.Response.OutputStream);

writer.ViewerPreferences = PdfWriter.PageModeUseOutlines;
PdfFooter eventHandler = new PdfFooter();
writer.PageEvent = eventHandler;

pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
HttpContext.Current.Response.Write(pdfDoc);
HttpContext.Current.Response.End();
}

private string getHTMLtableFromDataTable(DataTable dt, string SheetName,string From, string To)
{
try
{
StringBuilder sb = new StringBuilder();
sb.Append(@"<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.0 Transitional//EN"">");
//sb.Append("<Head><meta http-equiv=\"content-type\" content=\"application/vnd.ms-excel; charset=UTF-16\"></Head>");

sb.Append("<Head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"></Head>");

sb.Append("<span style='font-size:14.0pt;'>");
sb.Append("<BR><BR><BR>");
sb.Append("RytNow Reporting");
sb.Append("<BR><BR><BR>");
sb.Append("");
sb.Append(SheetName);
sb.Append("<BR><BR><BR>");
sb.Append("");
sb.Append("</span>");
sb.Append("<span style='color: #1F497D; font-size:12.0pt;'>");
if (To != "")
{

sb.Append("From : " + From);
sb.Append("<BR>");
sb.Append("To : " + To);
}
else{

sb.Append("Month/Date: " + From);
}
sb.Append("</span>");
sb.Append("<BR><BR><BR>");
//sets the table border, cell spacing, border color, font of the text, background, foreground, font height
sb.Append("<Table border='1' bgColor='#ffffff' " +
"borderColor='#000000' cellSpacing='0' cellPadding='0' " +
"style='font-size:12.0pt; background:white;'> <TR>");
//am getting my grid's column headers
int columnscount = dt.Columns.Count;

for (int j = 0; j < columnscount; j++)
{ //write in new column
sb.Append("<Td>");
//Get column headers and make it as bold in excel columns
sb.Append("");
sb.Append(dt.Columns[j].ColumnName.ToString());
sb.Append("
");
sb.Append("</Td>");
}
sb.Append("</TR>");
foreach (DataRow row in dt.Rows)
{//write in new row
sb.Append("<TR>");
for (int i = 0; i < dt.Columns.Count; i++)
{

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900