Data table to PDF with Dubai (Arabic) font using iTextSharp not working in Console Application

Last Reply 5 months ago By dharmendr

Posted 5 months ago

Dear,

I am facing one issue with ITextSharp Dubai font, I tried with the sample code its working fine but the individual character is not showing in the pdf file

eg: the sentence الخطة التشغيلية لمبادرة دراية لعام 2020 م ignore the character "م" or any another single character. Kindly guide me how to resolve this issue.

Can you please create a console application using Dubai font.

Font download link : https://dubaifont.com/download

PdfPTable hedertable = new PdfPTable(new float[] { 20, 60, 20 });
BaseFont bf1 = BaseFont.CreateFont(Server.MapPath("/fonts/") + "Dubai-Regular.ttf",BaseFont.IDENTITY_H,BaseFont.EMBEDDED);
string cellTextm = "الخطة التشغيلية لمبادرة دراية لعام" + thisDay + "م";
iTextSharp.text.Font fontheader = new iTextSharp.text.Font(bf1, 20, iTextSharp.text.Font.NORMAL);
iTextSharp.text.pdf.PdfPCell mcell = new iTextSharp.text.pdf.PdfPCell(new Phrase(16, cellTextm, fontheader))
{ HorizontalAlignment = iTextSharp.text.Element.ALIGN_CENTER };
mcell.VerticalAlignment = Element.ALIGN_MIDDLE;
mcell.RunDirection = PdfWriter.RUN_DIRECTION_RTL;
hedertable.AddCell(mcell);
//Create PDF
Document pdfDoc = new Document(PageSize.A4.Rotate(), 10f, 10f, 10f, 10f);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
pdfDoc.Add(hedertable);
pdfDoc.Add(table);
pdfDoc.Close();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Write(pdfDoc);
Response.End();

 

Results 1 - 5 of 7 12
Posted 5 months ago Modified on 5 months ago

For console application code you can refer below code.

C#

class Program
{
    static void Main(string[] args)
    {
        string folderPath = "D:\\PDFs\\";
        if (!Directory.Exists(folderPath))
        {
            Directory.CreateDirectory(folderPath);
        }
        using (FileStream stream = new FileStream(folderPath + "Test.pdf", FileMode.Create))
        {
            PdfPTable hedertable = new PdfPTable(1);
            //BaseFont bf1 = BaseFont.CreateFont(Environment.GetEnvironmentVariable("windir") + @"\fonts\ARIALUNI.TTF", BaseFont.IDENTITY_H, true);
            BaseFont bf1 = BaseFont.CreateFont(AppDomain.CurrentDomain.BaseDirectory.Replace("bin\\Debug\\", "") + "\\fonts\\Dubai-Regular.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED, true);
            string cellTextm = "الخطة التشغيلية لمبادرة دراية لعام" + " 2020 " + "م";
            Font fontheader = new Font(bf1, 20, Font.NORMAL);

            PdfPCell mcell = new PdfPCell(new Phrase(16, cellTextm, fontheader))
            {
                HorizontalAlignment = Element.ALIGN_TOP,
                VerticalAlignment = Element.ALIGN_TOP,
                RunDirection = PdfWriter.RUN_DIRECTION_RTL,
                Border = 0
            };
            hedertable.AddCell(mcell);

            //Create PDF
            Document pdfDoc = new Document(PageSize.A4.Rotate(), 10f, 10f, 10f, 10f);
            PdfWriter.GetInstance(pdfDoc, stream);
            pdfDoc.Open();
            pdfDoc.Add(hedertable);
            pdfDoc.Close();
        }
    }
}

VB.Net

Class Program
    Private Shared Sub Main(ByVal args As String())
        Dim folderPath As String = "D:\PDFs\"

        If Not Directory.Exists(folderPath) Then
            Directory.CreateDirectory(folderPath)
        End If

        Using stream As FileStream = New FileStream(folderPath & "Test.pdf", FileMode.Create)
            Dim hedertable As PdfPTable = New PdfPTable(1)
            Dim bf1 As BaseFont = BaseFont.CreateFont(AppDomain.CurrentDomain.BaseDirectory.Replace("bin\Debug\", "") & "\fonts\Dubai-Regular.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED, True)
            Dim cellTextm As String = "الخطة التشغيلية لمبادرة دراية لعام" & " 2020 " & "م"
            Dim fontheader As Font = New Font(bf1, 20, Font.NORMAL)
            Dim mcell As PdfPCell = New PdfPCell(New Phrase(16, cellTextm, fontheader)) With {
                .HorizontalAlignment = Element.ALIGN_TOP,
                .VerticalAlignment = Element.ALIGN_TOP,
                .RunDirection = PdfWriter.RUN_DIRECTION_RTL,
                .Border = 0
            }
            hedertable.AddCell(mcell)
            Dim pdfDoc As Document = New Document(PageSize.A4.Rotate(), 10F, 10F, 10F, 10F)
            PdfWriter.GetInstance(pdfDoc, stream)
            pdfDoc.Open()
            pdfDoc.Add(hedertable)
            pdfDoc.Close()
        End Using
    End Sub
End Class

 


Posted 5 months ago

Dear Dharmendr,

 

Can you please create a console application using Dubai font, I hope your expertise can resolve the issue.

 

Thank you


Posted 5 months ago

Hi sajeerma,

Use ARIALUNI.TTF font. Refer below code.

There might be the issue with the font Dubai font or it is not supported in iTextSharp.

PdfPTable hedertable = new PdfPTable(1);
BaseFont bf1 = BaseFont.CreateFont(Environment.GetEnvironmentVariable("windir") + @"\fonts\ARIALUNI.TTF", BaseFont.IDENTITY_H, true);
string cellTextm = "الخطة التشغيلية لمبادرة دراية لعام" + " 2020 " + "م";
iTextSharp.text.Font fontheader = new iTextSharp.text.Font(bf1, 20, iTextSharp.text.Font.NORMAL);
iTextSharp.text.pdf.PdfPCell mcell = new iTextSharp.text.pdf.PdfPCell(new Phrase(16, cellTextm, fontheader))
{ HorizontalAlignment = iTextSharp.text.Element.ALIGN_TOP };
mcell.VerticalAlignment = Element.ALIGN_TOP;
mcell.RunDirection = PdfWriter.RUN_DIRECTION_RTL;
mcell.Border = 0;
hedertable.AddCell(mcell);
        
//Create PDF
Document pdfDoc = new Document(PageSize.A4.Rotate(), 10f, 10f, 10f, 10f);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
pdfDoc.Add(hedertable);
//pdfDoc.Add(table);
pdfDoc.Close();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);

Response.Write(pdfDoc);
Response.End();

 


Posted 5 months ago

Dear dharmendr,

 

I am facing issue with Dubai font only.

 

Thank you