On 16/05/2012 19:34, [email protected] wrote:
Hi, please advice. I have text with czech characters like "Ččůřšě" but I can convert them to PDF document. Please advise me?
Your example is wrong for many different reasons. This is a non-exhaustive list:
1. you store special characters in your code, but: - are you sure your code is stored as UTF-8? - are you sure your compiler compiles the code as UTF-8? 2. you have special characters in your String, but: - you don't tell XML Worker which encoding to use 3. you have special characters in your String, but: - you don't specify a font that knows those characters. I've solved these problems with the example in attachment:1. I changed the special characters into \u010C\u010D\u016F\u0159\u0161\u011B which is safer. 2. I specified the CharSet when converting the String to bytes and when parsing the XML.
3. I use arial, because when I use Helvetica, only the š can be rendered.
czech.pdf
Description: Adobe PDF document
package com.lowagie;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorkerHelper;
public class ParseCzech {
public static final String HTML = "<p
style=\"font-family:arial\">\u010C\u010D\u016F\u0159\u0161\u011B</p>";
/**
* Creates a PDF with the words "Hello World"
* @param file
* @throws IOException
* @throws DocumentException
*/
public void createPdf(File file) throws IOException, DocumentException {
// step 1
Document document = new Document();
// step 2
PdfWriter writer = PdfWriter.getInstance(document, new
FileOutputStream(file));
// step 3
document.open();
// step 4
XMLWorkerHelper.getInstance().parseXHtml(writer, document,
new
ByteArrayInputStream(HTML.getBytes("UTF-8")), Charset.forName("UTF-8"));
// step 5
document.close();
}
/**
* Main method
*/
public static void main(String[] args) throws IOException,
DocumentException {
File dir = new File("results");
dir.mkdirs();
new ParseCzech().createPdf(new File(dir, "czech.pdf"));
}
}
------------------------------------------------------------------------------ Live Security Virtual Conference Exclusive live event will cover all the ways today's security and threat landscape has changed and how IT managers can respond. Discussions will include endpoint security, mobile security and the latest in malware threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________ iText-questions mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/itext-questions iText(R) is a registered trademark of 1T3XT BVBA. Many questions posted to this list can (and will) be answered with a reference to the iText book: http://www.itextpdf.com/book/ Please check the keywords list before you ask for examples: http://itextpdf.com/themes/keywords.php
