Problem in Urdu formatting while converting into pdf using itext#

  • Thread starter Thread starter Shahid Sultan
  • Start date Start date
S

Shahid Sultan

Guest
Well, i have already responded this in my earlier post but m creating this new discussion so to have a fresh start about new issue regarding arabic text conversion into pdf. Well i have converted rtf into html as well as successfully created pdf for English Language (with and without formatting) but as i put Arabic or Urdu into the richtextbox, i get no text in pdf and just blank. I dont know why, so is this font rendering problem or some else? If its a font rendering problem so could you please show me via example that how and where to specify font property while parsing html content by referring to below mentioned code?

Like i have TWO code modules for this as:

RichTextBox rtbnew = new RichTextBox();
rtbnew.Rtf = this.rtb.Rtf;
String abc = this.markupConverter.ConvertRtfToHtml(rtbnew.Rtf);
MessageBox.Show(abc);
//String html = abc;
String html = @"<?xml version=""1.0"" encoding=""UTF-8""?>
<!DOCTYPE html
PUBLIC ""-//W3C//DTD XHTML 1.0 Strict//EN""
""http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
<html xmlns=""http://www.w3.org/1999/xhtml"" xml:lang=""en"" lang=""en"
<head>
<title>Minimal XHTML 1.0 Document with W3C DTD</title>
</head>
<body>
" + abc + " </body></html>";
TextReader reader = new StringReader(html);

// step 1: creation of a document-object
Document document = new Document(PageSize.A4, 30, 30, 30, 30);

// step 2:
// we create a writer that listens to the document
// and directs a XML-stream to a file
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(path + "/Doc2.pdf",FileMode.Create));
BaseFont bfArialUniCode = BaseFont.CreateFont(@"fonts\\ARIALUNI_2.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
//Create a font from the base font
iTextSharp.text.Font font = new iTextSharp.text.Font(bfArialUniCode, 20);
// step 3: we create a worker parse the document
HTMLWorker worker = new HTMLWorker(document);

// step 4: we open document and start the worker on the document
document.Open();
worker.StartDocument();

// step 5: parse the html into the document
worker.Parse(reader);
//iTextSharp.text.Chunk chunk = worker.CreateChunk(reader.ReadToEnd());

//document.Add(new Phrase(reader.ToString(),font));
// step 6: close the document and the worker
worker.EndDocument();
worker.Close();

document.Close();

and second one which is nort working as well:

public void rtfToPdf()
{
RichTextBox rtbnew = new RichTextBox();
rtbnew.Rtf = this.rtb.Rtf;
String abc = this.markupConverter.ConvertRtfToHtml(rtbnew.Rtf);
MessageBox.Show(abc);
rtbnew.Text = this.rtb.Text;
string str = rtbnew.Text;
TextReader tr = new StringReader(str);
Document doc = new Document();
PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(@path + "/Doc2.pdf", FileMode.Create));
//////////////////
doc.Open();

//Sample HTML
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.Append(abc);

//Path to our font
string arialuniTff = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "ARIALUNI.TTF");
MessageBox.Show(arialuniTff);
//Register the font with iTextSharp
iTextSharp.text.FontFactory.Register(arialuniTff);

//Create a new stylesheet
iTextSharp.text.html.simpleparser.StyleSheet ST = new iTextSharp.text.html.simpleparser.StyleSheet();
//Set the default body font to our registered fonts internal name
ST.LoadTagStyle(HtmlTags.BODY, HtmlTags.FACE, "Arial Unicode MS");
//Set the default encoding to support Unicode characters
ST.LoadTagStyle(HtmlTags.BODY, HtmlTags.ENCODING, BaseFont.IDENTITY_H);

//Parse our HTML using the stylesheet created above
List<IElement> list = HTMLWorker.ParseToList(new StringReader(stringBuilder.ToString()), ST);

//Loop through each element, dont bother wrapping in P tags
foreach (var element in list)
{
doc.Add(element);
}

doc.Close();
}


In second code m giving font but its not rendering even the plain Arabic text why?

I want to know where i can put this font of my own into somewhere in the code to get it working for Arabic as well?

Continue reading...
 
Back
Top