Click here to Skip to main content
15,882,113 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to write Bengali and Hindi text using iTextSharp, in normal pdfDocument of an A4size page and in a pdfTable(cell of a pdf table). In everycase except when I use 'Phrase' to write the text, only the Hindi text (\u0915…) is correctly written but Bengali text(\u0985\0986) are missing every where(in paragraph.Add and pdfPTable).

Can anyone please tell me what is going wrong and suggest me how to make it work properly.

What I have tried:

private void SaveAsPdfbutton_MouseClick(object sender, MouseEventArgs e)
{
Document document = new Document();

PdfWriter pr = PdfWriter.GetInstance(document, new FileStream(@"A:\Software Development\text_database_2\Output\Bnghindi.pdf",FileMode.Create));

document.Open();
const string pfont = @"A:\Software Development\text_database_2\text_database\Resources\Fonts\KOKILAI.TTF";
var f = FontFactory.GetFont(pfont, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);

var p1 = new Paragraph("\u0915\u093e\u0930 \u092a\u093e\u0930\u094d\u0915\u093f\u0902\u0917", f);
document.Add(p1);

String foobar = "আমার নাম রাজীব \u0985\u0986 \u0915\u093e\u0930\u092a\u093e\u0930\u094d\u0915\u093f\u0902\u0917";

Phrase phrase = new Phrase(foobar, f);
ColumnText.ShowTextAligned(cb,Element.ALIGN_LEFT, phrase, 200, 572, 0);

var pcb = pr.DirectContent;
var table = new PdfPTable(2) {TotalWidth = 450f};

table.SetWidthPercentage(new float[] { 150,300 }, PageSize.A4);

var customerLblCell = new PdfPCell(new Phrase("CUSTOMERS"));
var balanceLblCell = new PdfPCell(new Phrase("\u0985\u0986 \u0915\u093e\u0930\u092a\u093e\u0930\u094d\u0915\u093f\u0902\u0917", f));

table.AddCell(customerLblCell);
table.AddCell(balanceLblCell);
table.CompleteRow();

//document.Add(table);

table.WriteSelectedRows(0, -1, 50, 730, pcb);

document.Close();

Process.Start(@"A:\Software Development\text_database_2\Output\hindi.pdf");





PdfPTable pdfTable = new PdfPTable(RegViewdataGridView.ColumnCount);
pdfTable.DefaultCell.Padding = 3;
pdfTable.HorizontalAlignment = Element.ALIGN_LEFT;
pdfTable.DefaultCell.BorderWidth = 1;

PdfWriter ablePWr = PdfWriter.GetInstance(document, new FileStream(@"A:\Software Development\text_database_2\Output\hindi.pdf", FileMode.Create));
PdfContentByte tablePcb = ablePWr.DirectContent;

var width = 0;
var i = 0;
var colwidths = new int[RegViewdataGridView.Columns.Count];
//Adding Header row
foreach (DataGridViewColumn column in RegViewdataGridView.Columns)
{
var cell = new PdfPCell(new Phrase(column.HeaderText + "রাজীব", f))
{
BackgroundColor = new BaseColor(240, 240, 240),
NoWrap = false
};
colwidths[i] = column.Width;
pdfTable.AddCell(cell);
width = width + column.Width;
i++;
}

pdfTable.SetWidths(colwidths);
width = width + RegViewdataGridView.RowHeadersWidth;


var height = 0;
//I am not Adding any DataRow

const string folderPath = @"A:\Software Development\text_database_2\text_database\bin\";
if (!Directory.Exists(folderPath))
{
Directory.CreateDirectory(folderPath);
}
using (FileStream stream = new FileStream(folderPath + "test.pdf", FileMode.Create))
{
Document pdfDoc = new Document(new iTextSharp.text.Rectangle(width, height), 20f, 10f, 20f, 10f);
PdfWriter.GetInstance(pdfDoc, stream);
pdfDoc.Open();
pdfDoc.Add(pdfTable);
pdfDoc.Close();
stream.Close();
MessageBox.Show(@"Exported to pdf,
Saved at "+folderPath,@"Done");
}

Process.Start(folderPath + "test.pdf");

}
Posted

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