Can't Get Czech Characters While Generating a PDF

iText : Unable to print mathematical characters like ∈, ∩, ∑, ∫, ∆ √, ∠

Please take a look at the MathSymbols example.

  • First you need a font that supports the symbols you need. Incidentally, FreeSans.ttf is such a font. Then you need to use the right encoding.
  • You're using UNICODE, so you need Identity-H as the encoding.
  • You should also use notations such as \u2208, \u2229, \u2211, \u222b, \u2206. That's not a must, but it's good practice.

This is how it's done:

public static final String DEST = "results/fonts/math_symbols.pdf";
public static final String FONT = "resources/fonts/FreeSans.ttf";
public static final String TEXT = "this string contains special characters like this \u2208, \u2229, \u2211, \u222b, \u2206";

public void createPdf(String dest) throws IOException, DocumentException {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
BaseFont bf = BaseFont.createFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Font f = new Font(bf, 12);
Paragraph p = new Paragraph(TEXT, f);
document.add(p);
document.close();
}

The result looks like this: math_symbols.pdf

Sample Image

Important: you should always use the most recent, official version of iText. iText 4.0.2 is not an official version.

Only English characters visible in PDF reader after adding field's value of existing pdf file using itext

Please read my answer to Can't get Czech characters while generating a PDF first. It is about Czech characters, but it is also relevant for your question:

  1. you are using a bad programming practice by adding non-Western characters in your code instead of using the UNICODE notation.
  2. you need to create a font that is able to write Thai characters (e.g. arialuni.ttf).

Then read Chapter 8 of my book. More specifically the text that explains figure 8.3 and figure 8.4.
Creating text fields containing Unicode characters
In figure 8.3, the upper document shows the problem you are experiencing. In this case, we are adding a String with Western text as well as Chinese text as a field value. The Western text is shown, the Chinese text isn't.
The second and third window, show what happens when you fill out the field correctly.
Filling out text fields containing Unicode characters
In figure 8.3, we try adding some Korean text. This fails in the first and third window, but we solved the problem in the second, third and fourth window.

If you do not own a copy of my book, you may benefit from trying the TextFieldFont example that produces all the PDFs shown in the screen shots.

Suppose that you would decide to use MS Arial Unicode, you'd choose the solution where this font is added as substitution font:

AcroFields form = stamper.getAcroFields();
BaseFont unicode =
BaseFont.createFont("c:/windows/fonts/arialuni.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
form.addSubstitutionFont(unicode);

There are, of course, other options, but this is one way to solve your problem.

itextpdf HTML to PDF containing Cyrillic letters

I tried many things, but everytime I was missing something. Thanks @BrunoLowagie and @SubOptimal. Here's my code that I make it run for a custom fonts. It also contains a simple html as a string, but there is shown (in comments) how it can be done with actual html and css files.

public class HtmlToPdf {
public static final String DEST = "/home/christian/Desktop/testDoc.pdf";

public void createPdf(String file) throws IOException, DocumentException {
// step 1
Document document = new Document();

// step 2
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
writer.setInitialLeading(12.5f);

// step 3
document.open();

// step 4

// CSS
CSSResolver cssResolver = new StyleAttrCSSResolver();
// CssFile cssFile = XMLWorkerHelper.getCSS(new FileInputStream(CSS));
// cssResolver.addCss(cssFile);

// HTML
XMLWorkerFontProvider fontProvider = new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS);
fontProvider.register("fonts/Arimo-Regular.ttf");
fontProvider.register("fonts/Arimo-Bold.ttf");
fontProvider.register("fonts/Arimo-Italic.ttf");
fontProvider.addFontSubstitute("lowagie", "Arimo");
CssAppliers cssAppliers = new CssAppliersImpl(fontProvider);
HtmlPipelineContext htmlContext = new HtmlPipelineContext(cssAppliers);
htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());

// Pipelines
PdfWriterPipeline pdf = new PdfWriterPipeline(document, writer);
HtmlPipeline html = new HtmlPipeline(htmlContext, pdf);
CssResolverPipeline css = new CssResolverPipeline(cssResolver, html);

// XML Worker
XMLWorker worker = new XMLWorker(css, true);
XMLParser p = new XMLParser(worker);

// p.parse(new FileInputStream(HTML));
String htmlContent = " HERE GOES HTML CODE ";
p.parse(new StringReader(htmlContent));
// step 5
document.close();
}

public static void main(String[] args) throws IOException, DocumentException {
new D06_ParseHtmlFonts().createPdf(DEST);
}
}

I noticed that it is important to have font-family: actual font that supports wished encoding; in css/html and for email clients always to use inline css.

Croatian letters in iTextSharp

I have solved my problem. This is the code that helped me:

BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1250, false);
Font titleFont = new Font(bf,20);
Font infoFont = new Font(bf,16);

Thank you all



Related Topics



Leave a reply



Submit