Getting Unicode
I am using iText to generate a PDF file that will load to my browser. The thing is, I have Japanese characters that I want to show in the PDF file. These Japanese characters are from the database. With iText, I need to get the Unicode of the Japanese strings that I have. I want to have my Japanese string converted to something like this: \u53d6\u6e96\u53d7
Can this be done in code? I know native2ascii exists but I don't know if I can use it in my code.
Thank you.
Hi ,
we can see the japanese text in PDF from database.
can u let me know what is the database charset?
ur PDF will display chinese or japanese only if the
format is UCS-2.
so we need to convert whatever charset in Database to UCS-2 in java before u write it as pdf.
so what is ur database charset?
I am surprised to hear that iText requires that. I also doubt it, but I don't feel like checking myself so I will just give you some code:char c = // something
String s = "\\u" + Integer.toHexString((int) c);
Now I'm sure you're getting bad advice. ASCII can only represent Western letters, digits, and punctuation. You can't convert Japanese characters to ASCII. But they are Unicode characters and you can convert them to Java Unicode escapes, the way I already showed you.
I would be interested to know where you are getting these ideas from.
You need not convert to unicode......use following code to print japanese characters in a pdf. You will also have to download a jar for the same i.e. iTextAsian.jar.
Document document = new Document();
try {
// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file
PdfWriter.getInstance(document,
new FileOutputStream("D:\\deploy\\AOMS.ear\\AOMS.war\\HelloWorld.pdf"));
// step 3: we open the document
document.open();
// step 4: we add a paragraph to the document
/*document.add(new Paragraph("Hello World"));
document.add(new Paragraph(japanese));*/
BaseFont bfJapanese = BaseFont.createFont("HeiseiMin-W3", "UniJIS-UCS2-H", BaseFont.NOT_EMBEDDED);
Font FontJapanese = new Font(bfJapanese, 12, Font.NORMAL);
Paragraph p = new Paragraph(japanese, FontJapanese);
document.add(p);
}