Skip to content Skip to sidebar Skip to footer

Generate Pdf Using Itextsharp For Different Languages

I have tried different options, but it did not work. The code generated pdf for English but does not work for other languages. using (var ms = new MemoryStream()) {

Solution 1:

Well we had to buy another 3rd party tool which understands UNICODE characters.

Solution 2:

I was able to create multi language PDF in English and Japanese. The prerequisite for achieving this 1. To have appropriate Font which supports the languages you are planning to use. 2. Implement IFontProvider interface from iTextSharp and register the multi language font you will be using.

If you follow these 2 steps, you can create multi language PDF.

Code Sample:

public class smartUIFontProvider: IFontProvider
    {
      public Font GetFont(string fontname, string encoding, bool embedded, float size, int style, BaseColor color)
        {
            string myFont = @"C:\Tasks\Projects\SampleProject\iTextDemo_PDF\SmartFontUI\SmartFontUI.otf";
            iTextSharp.text.pdf.BaseFont bfR;
            bfR = iTextSharp.text.pdf.BaseFont.CreateFont(myFont,
              iTextSharp.text.pdf.BaseFont.IDENTITY_H,
              iTextSharp.text.pdf.BaseFont.EMBEDDED);
            iTextSharp.text.BaseColor clrBlack =
               new iTextSharp.text.BaseColor(0, 0, 0);
            iTextSharp.text.Font fntHead =
              new iTextSharp.text.Font(bfR, 12, iTextSharp.text.Font.NORMAL, clrBlack);
            return fntHead;
        }
       public bool IsRegistered(string fontname)
        {
            return true;
        }
    } 

static void Main (string[] args)
        {
            // step 1
            Document document = new Document();
            Byte[] bytes;
            var fileName = "resources\\test_" + System.DateTime.Now.ToString("ddMMyyyy_hhmm") + ".pdf";
            var testFile = Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), fileName);

            MemoryStream ms = new MemoryStream();
            // step 2

            using (var writer = PdfWriter.GetInstance(document, ms))
            {
                document.Open();

                String example_html = @" <!DOCTYPE html><html><head></head><body><h1>Change of Control Application</h1><br/><p>Please fill out this form if there are updates to your current organization and its relationship Company has on file with the legal binding entity. Company will review the information and may ask you to provide further information before Company agrees to the requested changes.</p><br/><tableclass='bodyText1LIC general'style='width:700px'id='tblInputForm' ><tr><tdclass='bodyText1LIC'colspan='4'>&nbsp;</td></tr><tr><tdclass='tableBody1'colspan='4'><h3>Licensee Information</h3></td></tr><tr><tdclass='bodyText1SerNo'>1.</td><tdclass='bodyText1LIC'>Licensee Name&nbsp;</td><tdvalign='top'class='txtboxtd'> 字詰めなどの調整をおすすめします </td><td>&nbsp;</td></tr><tr><tdclass='bodyText1LIC'colspan='4'>&nbsp;</td></tr><tr><tdclass='tableBody1'colspan='4'><h3>Licensee Primary Contact Information </h3></td></tr><tr><tdclass='bodyText1SerNo'>2.</td><tdclass='bodyText1LIC'>Name</td><tdclass='txtboxtd'> めなどの調整 </td><td>&nbsp;</td></tr><tr><tdclass='bodyText1SerNo'>3.</td><tdclass='bodyText1LIC'>Job Title</td><tdclass='txtboxtd'> T</td><td>&nbsp;</td></tr><tr><tdclass='bodyText1SerNo'>4.</td><tdclass='bodyText1LIC'>Mailing Address</td><tdclass='txtboxtd'>めなどの調整</td><td>&nbsp;</td></tr><tr><tdclass='bodyText1SerNo'>5.</td><tdclass='bodyText1LIC'>Telephone Number&nbsp;</td><tdclass='txtboxtd'>T</td><td>&nbsp;</td></tr><tr><tdclass='bodyText1SerNo'>6.</td><tdclass='bodyText1LIC'>Email Address&nbsp;</td><tdclass='txtboxtd'>abc@test.com</td><td>&nbsp;</td></tr><tr><tdclass='bodyText1LIC'colspan='4'>&nbsp;</td></tr><tr><tdclass='tableBody1'colspan='4'><h3> Organizational Structure</h3></td></tr><tr><tdclass='bodyText1SerNo'> 7.</td><tdclass='bodyText1LIC'colspan='3'> Will your Company's Name Change?&nbsp;</td></tr><tr><td></td><tdcolspan='3'> Yes</td></tr><trclass='nc'id='TrCompanyName'><td></td><tdclass='bodyText1LIC'colspan='2'> a) &nbsp;&nbsp;New company name&nbsp;</td><tdclass='txtboxtd'> DSFDASFASDFDSA</td></tr><trclass='nc'id='TrEffectiveDate'><td></td><tdclass='bodyText1LIC'colspan='2'> b) &nbsp;&nbsp;Effective Date Company change will take place &nbsp;</td><tdclass='txtboxtd'> 04/20/2017</td></tr><tr><tdclass='bodyText1SerNo'>8.</td><tdclass='bodyText1LIC'colspan='3'>Has your Company been Reincorporated or under gone an Internal Reorganization?&nbsp;</td></tr><tr><td></td><tdcolspan='3'> No</td></tr><tr><tdclass='bodyText1LIC'colspan='4'>&nbsp;</td></tr><tr><tdclass='tableBody1'colspan='4'><h3>Assets / Stocks to be Acquired </h3></td></tr><tr><tdclass='tableBody1'colspan='4'><h3>Assets to be Acquired</h3></td></tr><tr><tdclass='bodyText1SerNo'>9.</td><tdclass='bodyText1LIC'colspan='3'>Will some or all of your Company’s Assets be Acquired? &nbsp;</td></tr><tr><td></td><tdcolspan='3'> No</td></tr><tr><tdclass='bodyText1LIC'colspan='4'>&nbsp;</td></tr><tr><tdclass='tableBody1'colspan='4'><h3>Stocks to be Acquired </h3></td></tr><tr><tdclass='bodyText1SerNo'>10.</td><tdclass='bodyText1LIC'colspan='3'>Will some or all of your Company’s Assets be Acquired? &nbsp;</td></tr><tr><td></td><tdcolspan='3'> No</td></tr><tr><tdclass='bodyText1LIC'colspan='4'>&nbsp;</td></tr><tr><tdclass='tableBody1'colspan='4'><h3> Effects of Change to Licensee</h3></td></tr><tr><tdclass='bodyText1SerNo'> 11.</td><tdclass='bodyText1LIC'colspan='3'> Please indicate which Company is the Surviving Legal Entity? &nbsp;</td></tr><tr><td></td><tdclass='txtboxtd'colspan='3'> T</td></tr><tr><tdclass='bodyText1SerNo'>12.</td><tdclass='bodyText1LIC'colspan='3'>Please provide the Company that will be the Licensee?&nbsp;</td></tr><tr><tdclass='txtboxtd'>&nbsp;</td><tdcolspan='3'> T</td></tr><tr><tdvalign='top'class='bodyText1SerNo'>13.</td><tdclass='bodyText1LIC'colspan='3'>Will any companies exist to operate as separate Subsidiaries or Affiliates ?  &nbsp;</td></tr><tr><td></td><tdcolspan='3'> No</td></tr><tr><tdclass='bodyText1LIC'colspan='4'>&nbsp;</td></tr><tr><tdclass='tableBody1'colspan='4'><h3>Existing Agreements</h3></td></tr><tr><tdclass='bodyText1SerNo'>14.</td><tdclass='bodyText1LIC'colspan='3'>Please identify if any Licenses that will be affected by the Changes described above &nbsp;</td></tr><tr><td></td><tdcolspan='3'> No</td></tr><tr><tdclass='bodyText1LIC'colspan='4'>&nbsp;</td></tr><tr><tdclass='tableBody1'colspan='4'><h3>Corporate Documentation</h3></td></tr><tr><tdvalign='top'class='bodyText1SerNo'>15.</td><tdclass='bodyText1LIC'colspan='3'>Please Select from 1 of the 2 options listed below</td></tr><tr><td></td><tdcolspan='3'> Please Describe the Full Legal Structure of this Organization Change in relation to Licensing.</td></tr><tr><td></td><tdcolspan='3'> ASDFSDAFDSAFADSFDASF &nbsp;</td></tr></table></body></html>";
                String example_css = @"html { font-family: SmartFontUI; font-size: 14px; }  h1, h2, strong { font-family: SmartFontUI; font-weight: bold; display: inline; }  h1 { font-size: 18pt; }   .main { font-size: 12pt; color: black; font-family: SmartFontUI, Arial, Sans-Serif; background-color: white; text-align: left; line-height: 1.4em; margin: 2%; } .mainDesc { width: 100%; margin: 10px 0; }  span { float: left; display: block; width: 100%; margin-bottom: 5px; }  .bodyText1SerNo { width: 2em; }  .bodyText1LIC { width: 22em; }  .txtboxspan { width: 32em; }  .tableBody1 { width: 100%; } .nc { margin-left: 100px; } .subtitle { font-size: 16pt; }";
                FontFactory.Register(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "SmartFontUI.otf"), "SmartFontUI");

                XMLWorkerHelper worker = XMLWorkerHelper.GetInstance();

                MemoryStream msHtml = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(example_html));
                MemoryStream msCss = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(example_css));

                smartUIFontProvider smartUIFontObj = new smartUIFontProvider();

                worker.ParseXHtml(writer, document, msHtml, msCss, System.Text.Encoding.UTF8, smartUIFontObj);
                // step 5
                document.Close();

            }

            bytes = ms.ToArray();
            System.IO.File.WriteAllBytes(testFile, bytes);
        }

Post a Comment for "Generate Pdf Using Itextsharp For Different Languages"