Skip to content

Instantly share code, notes, and snippets.

@dedico
Created January 5, 2012 23:06
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dedico/1567859 to your computer and use it in GitHub Desktop.
Save dedico/1567859 to your computer and use it in GitHub Desktop.
iTextSharp and unicode characters
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
namespace RussianPDFtest
{
class Program
{
static void Main(string[] args)
{
germanPDF();
russianPDF();
internationalPDF();
}
static private void germanPDF()
{
try
{
Document document = new Document(PageSize.A4, 72, 65, 72, 65);
string filename = "german" + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + ".pdf";
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("C:\\temp\\" + filename, FileMode.Create));
document.AddAuthor("RussianPDFtest");
document.AddTitle("Buchungsbestätigung");
document.AddCreationDate();
string sylfaenpath = Environment.GetEnvironmentVariable("SystemRoot") + "\\fonts\\sylfaen.ttf";
BaseFont sylfaen = BaseFont.CreateFont(sylfaenpath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Font head = new Font(sylfaen, 12f, Font.NORMAL, BaseColor.BLUE);
Font normal = new Font(sylfaen, 10f, Font.NORMAL, BaseColor.BLACK);
Font underline = new Font(sylfaen, 10f, Font.UNDERLINE, BaseColor.BLACK);
document.Open();
document.Add(new Paragraph("CZ-Reservation / Buchungsbestätigung BU-16122011-37", head));
document.Add(new Paragraph(" ", normal));
document.Add(new Paragraph(" ", normal));
document.Add(new Paragraph("Hiermit bestätigen wir Ihre Buchung mit folgenden Daten:", underline));
document.Add(new Paragraph(" ", normal));
PdfPTable table1 = new PdfPTable(2);
table1.TotalWidth = document.PageSize.Width - 72f - 65f;
table1.LockedWidth = true;
float[] widths1 = new float[] { 1f, 4f };
table1.SetWidths(widths1);
table1.HorizontalAlignment = 0;
PdfPCell table1cell11 = new PdfPCell(new Phrase("Objekt:", normal));
table1cell11.Border = 0;
table1.AddCell(table1cell11);
PdfPCell table1cell12 = new PdfPCell(new Phrase("Ferienhaus 'Waldesruh'", normal));
table1cell12.Border = 0;
table1.AddCell(table1cell12);
PdfPCell table1cell21 = new PdfPCell(new Phrase("Anschrift:", normal));
table1cell21.Border = 0;
table1.AddCell(table1cell21);
PdfPCell table1cell22 = new PdfPCell(new Phrase("15344 Strausberg, Am Marienberg 45", normal));
table1cell22.Border = 0;
table1.AddCell(table1cell22);
PdfPCell table1cell31 = new PdfPCell(new Phrase("Objektnummer:", normal));
table1cell31.Border = 0;
table1.AddCell(table1cell31);
PdfPCell table1cell32 = new PdfPCell(new Phrase("czr04012012", normal));
table1cell32.Border = 0;
table1.AddCell(table1cell32);
PdfPCell table1cell41 = new PdfPCell(new Phrase("Anreisetag:", normal));
table1cell41.Border = 0;
table1.AddCell(table1cell41);
PdfPCell table1cell42 = new PdfPCell(new Phrase("12.02.2012", normal));
table1cell42.Border = 0;
table1.AddCell(table1cell42);
PdfPCell table1cell51 = new PdfPCell(new Phrase("Abreisetag:", normal));
table1cell51.Border = 0;
table1.AddCell(table1cell51);
PdfPCell table1cell52 = new PdfPCell(new Phrase("18.02.2012", normal));
table1cell52.Border = 0;
table1.AddCell(table1cell52);
PdfPCell table1cell61 = new PdfPCell(new Phrase("Personen:", normal));
table1cell61.Border = 0;
table1.AddCell(table1cell61);
PdfPCell table1cell62 = new PdfPCell(new Phrase("5", normal));
table1cell62.Border = 0;
table1.AddCell(table1cell62);
document.Add(table1);
document.Close();
}
catch (Exception ex)
{
Console.WriteLine("Try-Catch-Fehler!");
Console.WriteLine(ex.Message);
Console.ReadLine();
}
}
static private void russianPDF()
{
try
{
Document document = new Document(PageSize.A4, 72, 65, 72, 65);
string filename = "russian" + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + ".pdf";
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("C:\\temp\\" + filename, FileMode.Create));
document.AddAuthor("RussianPDFtest");
document.AddTitle("Подтверждение бронирования");
document.AddCreationDate();
string sylfaenpath = Environment.GetEnvironmentVariable("SystemRoot") + "\\fonts\\sylfaen.ttf";
BaseFont sylfaen = BaseFont.CreateFont(sylfaenpath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Font head = new Font(sylfaen, 12f, Font.NORMAL, BaseColor.BLUE);
Font normal = new Font(sylfaen, 10f, Font.NORMAL, BaseColor.BLACK);
Font underline = new Font(sylfaen, 10f, Font.UNDERLINE, BaseColor.BLACK);
document.Open();
document.Add(new Paragraph("CZ-Reservation / Подтверждение бронирования BU-16122011-37", head));
document.Add(new Paragraph(" ", normal));
document.Add(new Paragraph(" ", normal));
document.Add(new Paragraph("Мы подтверждаем бронирование, содержащее следующую информацию:", underline));
document.Add(new Paragraph(" ", normal));
PdfPTable table1 = new PdfPTable(2);
table1.TotalWidth = document.PageSize.Width - 72f - 65f;
table1.LockedWidth = true;
float[] widths1 = new float[] { 1f, 4f };
table1.SetWidths(widths1);
table1.HorizontalAlignment = 0;
PdfPCell table1cell11 = new PdfPCell(new Phrase("Объект:", normal));
table1cell11.Border = 0;
table1.AddCell(table1cell11);
PdfPCell table1cell12 = new PdfPCell(new Phrase("Ferienhaus 'Waldesruh'", normal));
table1cell12.Border = 0;
table1.AddCell(table1cell12);
PdfPCell table1cell21 = new PdfPCell(new Phrase("Адрес:", normal));
table1cell21.Border = 0;
table1.AddCell(table1cell21);
PdfPCell table1cell22 = new PdfPCell(new Phrase("15344 Strausberg, Am Marienberg 45", normal));
table1cell22.Border = 0;
table1.AddCell(table1cell22);
PdfPCell table1cell31 = new PdfPCell(new Phrase("Номер объекта:", normal));
table1cell31.Border = 0;
table1.AddCell(table1cell31);
PdfPCell table1cell32 = new PdfPCell(new Phrase("czr04012012", normal));
table1cell32.Border = 0;
table1.AddCell(table1cell32);
PdfPCell table1cell41 = new PdfPCell(new Phrase("Дата заезда:", normal));
table1cell41.Border = 0;
table1.AddCell(table1cell41);
PdfPCell table1cell42 = new PdfPCell(new Phrase("12.02.2012", normal));
table1cell42.Border = 0;
table1.AddCell(table1cell42);
PdfPCell table1cell51 = new PdfPCell(new Phrase("Дата выезда:", normal));
table1cell51.Border = 0;
table1.AddCell(table1cell51);
PdfPCell table1cell52 = new PdfPCell(new Phrase("18.02.2012", normal));
table1cell52.Border = 0;
table1.AddCell(table1cell52);
PdfPCell table1cell61 = new PdfPCell(new Phrase("Человек:", normal));
table1cell61.Border = 0;
table1.AddCell(table1cell61);
PdfPCell table1cell62 = new PdfPCell(new Phrase("5", normal));
table1cell62.Border = 0;
table1.AddCell(table1cell62);
document.Add(table1);
document.Close();
}
catch (Exception ex)
{
Console.WriteLine("Try-Catch-Fehler!");
Console.WriteLine(ex.Message);
Console.ReadLine();
}
}
static private void internationalPDF()
{
try
{
Document document = new Document(PageSize.A4, 72, 65, 72, 65);
string filename = "international" + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + ".pdf";
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("C:\\temp\\" + filename, FileMode.Create));
document.AddAuthor("RussianPDFtest");
document.AddTitle("Załączone zdjęcie");
document.AddCreationDate();
string sylfaenpath = Environment.GetEnvironmentVariable("SystemRoot") + "\\fonts\\sylfaen.ttf";
BaseFont sylfaen = BaseFont.CreateFont(sylfaenpath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Font head = new Font(sylfaen, 12f, Font.NORMAL, BaseColor.BLUE);
Font normal = new Font(sylfaen, 10f, Font.NORMAL, BaseColor.BLACK);
Font underline = new Font(sylfaen, 10f, Font.UNDERLINE, BaseColor.BLACK);
document.Open();
document.Add(new Paragraph("CZ-Reservation / Załączone zdjęcie BU-16122011-37", head));
document.Add(new Paragraph(" ", normal));
document.Add(new Paragraph(" ", normal));
document.Add(new Paragraph("Informacje te są przesyłane bezpośrednio do właściciela:", underline));
document.Add(new Paragraph(" ", normal));
PdfPTable table1 = new PdfPTable(2);
table1.TotalWidth = document.PageSize.Width - 72f - 65f;
table1.LockedWidth = true;
float[] widths1 = new float[] { 1f, 4f };
table1.SetWidths(widths1);
table1.HorizontalAlignment = 0;
PdfPCell table1cell11 = new PdfPCell(new Phrase("Usunięcie się powiodło:", normal));
table1cell11.Border = 0;
table1.AddCell(table1cell11);
PdfPCell table1cell12 = new PdfPCell(new Phrase("Ferienhaus 'Waldesruh'", normal));
table1cell12.Border = 0;
table1.AddCell(table1cell12);
PdfPCell table1cell21 = new PdfPCell(new Phrase("Menu główne:", normal));
table1cell21.Border = 0;
table1.AddCell(table1cell21);
PdfPCell table1cell22 = new PdfPCell(new Phrase("15344 Strausberg, Am Marienberg 45", normal));
table1cell22.Border = 0;
table1.AddCell(table1cell22);
PdfPCell table1cell31 = new PdfPCell(new Phrase("D'évènement:", normal));
table1cell31.Border = 0;
table1.AddCell(table1cell31);
PdfPCell table1cell32 = new PdfPCell(new Phrase("czr04012012", normal));
table1cell32.Border = 0;
table1.AddCell(table1cell32);
PdfPCell table1cell41 = new PdfPCell(new Phrase("Centres bien-être:", normal));
table1cell41.Border = 0;
table1.AddCell(table1cell41);
PdfPCell table1cell42 = new PdfPCell(new Phrase("12.02.2012", normal));
table1cell42.Border = 0;
table1.AddCell(table1cell42);
PdfPCell table1cell51 = new PdfPCell(new Phrase("Accès handicapés:", normal));
table1cell51.Border = 0;
table1.AddCell(table1cell51);
PdfPCell table1cell52 = new PdfPCell(new Phrase("18.02.2012", normal));
table1cell52.Border = 0;
table1.AddCell(table1cell52);
PdfPCell table1cell61 = new PdfPCell(new Phrase("Août:", normal));
table1cell61.Border = 0;
table1.AddCell(table1cell61);
PdfPCell table1cell62 = new PdfPCell(new Phrase("5", normal));
table1cell62.Border = 0;
table1.AddCell(table1cell62);
document.Add(table1);
document.Close();
}
catch (Exception ex)
{
Console.WriteLine("Try-Catch-Fehler!");
Console.WriteLine(ex.Message);
Console.ReadLine();
}
}
}
}
@Elvin1492
Copy link

The international Character "Ə" doesn't appear in pdf. How can I solve this? please help me

@nguyenthevinhkcn
Copy link

Great post. It worked well with vietnameses characters also. You can take a look at my result at
http://qlkh.tnu.edu.vn/
I converted all documents into pdf in every page

@amirmostaghim
Copy link

amirmostaghim commented May 9, 2020

I search a lot of links in google but not found any valuable docs for it.

I want to export pdf file using ITextSharp library that contains Persian, English, Emoji and special

Environment

iTextSharp version: 5.5.10
.NET Core SDK version: 3.1.101
IDE: vs 2019

Example code/Steps to reproduce:

public PdfGenerator( BaseFont bf = null)
{
    _bf = bf ?? BaseFont.CreateFont(Environment.GetEnvironmentVariable("windir") + @"\fonts\iransans.ttf",
              BaseFont.IDENTITY_H, true);

}

var fontNormal = new Font(_bf, 12, Font.NORMAL, BaseColor.Black);

table.AddCell(new PdfPCell(new Phrase(12, text, fontNormal))
{
   RunDirection = PdfWriter.RUN_DIRECTION_RTL,
   BorderWidth = 0,
   Padding = 20,
   Colspan = isLongText ? 3 : 2,
   CellEvent = new RoundedBorder2()
});

@vatsalprajapati25
Copy link

It does not support Gujarati language

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment