Click here to Skip to main content
Click here to Skip to main content
Go to top

An intermediate way to generate Indic PDFs using iText/iTextsharp

, 12 Aug 2008
Rate this:
Please Sign up or sign in to vote.
All about generating PDF files for Indic scripts using iText/iTextSharp.

Introduction

I am working/dealing with Indic scripts for my project and I faced many problems due to partial/no support given by libraries/languages/frameworks. I have been trying to resolve my problems. One of them is generating PDFs dynamically.

iTextSharp is a wonderful library for generating PDf Files dynamically. It supports Unicode but not Indic Scripts. iText/iTextSharp has a ready answer for this problem/bug/non-support, but no solution. I am not criticizing iText/iTextsharp here. Because it's Open Source, we have the option to improve it.

Background and Terms

I assume you have a basic idea of iText/iTextSharp and GIST and the terms ISCII, Unicode, IsFoc.

My Intermediate Solution

Going back to history. I use GIST (developed by CDAC) and ISCII.

  • Step 1: First you have to convert the Unicode HTML string to an ISCII HtmlString.
  • Step 2: Convert the ISCII string to ISFOC and send this string to the iText HtmlParser.
  • Step 2 and step 3 can be merged by directly converting the Unicode string to ISFoc. This method is exposed by GIST. Refer to the GIST documentation.
  • Step 3: Publish the generated PDF.

Step 1

public static string UniCode2ISCII(string  S)
{
    if (S == null) { return null; }
    try
    {
        Encoding encFrom = Encoding.GetEncoding(57002);//Hindi
        //Encoding encFrom = Encoding.GetEncoding(57005);//Telugu
       
        Encoding encTo = Encoding.GetEncoding(1252);
        string str = S;
        Byte[] b = encFrom.GetBytes(str);
        return encTo.GetString(b);
    }
    catch { return null; }
}

Step 2

Public Class ISCII2ISFOC
     Declare Function Iscii2Isfoc Lib "ismapi32.dll" (ByVal s1 As String,
         ByVal s2 As String, ByVal ilen As Integer, ByVal sc As String) As Integer
    'Declare Function Isfoc2Iscii Lib "ismapi32.dll" (ByVal s1 As String,
       '  ByVal s2 As String, ByVal ilen As Integer, ByVal sc As String) As Integer
    'Declare Function Iscii2AccessIsfoc Lib "ismapi32.dll" (ByVal s1 As String,
        ' ByVal s2 As String, ByVal ilen As Integer, ByVal sc As String) As Integer
    ' sc = "DVB"   Devanagari  B-  Bilingial 
    '                      Devanagari (DV - catering to Hindi, Marathi, etc.), 
    '                     Gujarati (GJ), Kannada (KN), Malayalam (ML), Manipuri 
    '                     (MN), Oriya (OR), Punjabi (PN), Tamil (TM), Telugu (TL)  
                           
   Public Function ToIsfoc(ByVal str1 As String,ByVal sc as String ) As String
        Dim stext1 As String
        Dim ilen As Integer
        Dim iret As Integer    
       stext1 = Space(str1.Length * 2) 'I Don't know   why it has to multiplied by 2
        ilen = str1.Length
       
        iret = Iscii2Isfoc(str1, stext1, ilen, sc)

        ToIsfoc = stext1

    End Function

End Class

Note: The above method is written in VB.NET so you have to compile it as a separate library and then consume it. I also tried it with C# ref variables (did not get fruitful results; needs to be written in C#).

Step 3

//The  font name depends onthe Target Indic Language.  It can be made Dyanmic.
publc void PublishPdf(string IsFochtmlString){
    string htmlString = "<html><body><font face=\"DVBSR0XT.ttf\">" +
        IsFochtmlString+ "</font></body></html>";

    HttpRequest Request = System.Web.HttpContext.Current.Request;
    HttpResponse Response = System.Web.HttpContext.Current.Response;
    Response.Clear();
     Response.ContentType = "application/pdf";
    System.IO.MemoryStream m = new System.IO.MemoryStream();
    iTextSharp.text.Document document = new iTextSharp.text.Document();

    iTextSharp.text.pdf.PdfWriter writer =
        iTextSharp.text.pdf.PdfWriter.GetInstance(document, m);
 
    document.Open();

    System.Xml.XmlTextReader _xmlr = new System.Xml.XmlTextReader(
        new StringReader(htmlString));

    iTextSharp.text.html.HtmlParser.Parse(document, _xmlr);

    writer.Flush();

    Response.OutputStream.Write(m.GetBuffer(), 0, m.GetBuffer().Length);
    Response.OutputStream.Flush();
    Response.OutputStream.Close();
   Response.End();
}

Advantages

  • Compact and tiny compared with Unicode (obviously).

Limitations

  • It inherits all the limitations from ISCII .
  • It supports bilingual only (i.e., English + an Indic language).
  • It's not the recommended way/fails when there are multiple languages (can overcome this by splitting the string but it's difficult at runtime, but still you can try; it's possible) because it can't recognize the difference between the languages (Indic).

History

  • Article first version posted on 13 Aug. 2008.

License

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

Share

About the Author

Dileep.M

India India
No Biography provided

Comments and Discussions

 
GeneralRe:Help PinmemberMember 417493424-Mar-09 20:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140922.1 | Last Updated 12 Aug 2008
Article Copyright 2008 by Dileep.M
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid