> -----Original Message-----
> From: Erik Norvelle [mailto:signu...@norvelle.org]
> Sent: Friday, September 11, 2009 5:06 PM
> To: iText-questions@lists.sourceforge.net
> Subject: [iText-questions] Outputting texts in PDF including
> arbitrary UTF-8 characters (including CJK)
>
> Greetings to Bruno and the iText community,
>
> My question is a bit complicated, so please bear with me as I
> explain what my issue is. I don't think my particular
> problem has been answered on the mailing lists, due to the
> fact that my situation is rather special.
>
>
> I've been using iText in my program (TextCite,
> http://textcite.sourceforge.net) for several years now, in
> order to export text in PDF and RTF formats. My program, as
> designed, is able to output PDFs with text from nearly any
> UTF-8 block, except CJK. My program currently uses iText
> 1.4.5, although I am in the process of updating it to use the
> 2.1.7 version.
>
> Because my program is intended for use on any platform with
> Java installed (including Linux of various flavors), I cannot
> count on the user having Unicode fonts with all the necessary
> character blocks. So, I have embedded the GNU free fonts
> (FreeSerif, FreeSans and FreeMono) in my program (i.e. inside
> the Jar file). When I create a PDF, I read the font file and
> create a new embedded font, calling the routine
> BaseFont.createFont(String name, String encoding, boolean
> embedded, boolean cached, byte ttfAfm[], byte pfb[]). Note
> that I have to create and pass the ttfAfm byte array because
> the fonts are stored inside my program's Jar file, and not in
> the filesystem.
>
No need to do it, BaseFont.createFont() will laso look for the fonts as
resources.
> Unfortunately, despite the fact that the GNU fonts contain
> the necessary CJK characters, my PDFs come out with blank
> spaces where the CJK characters ought to be.
>
> I have included the iTextAsian.jar and the
> iTextAsianCmaps.jar libraries in my classpath, along with
> iText.jar, as suggested in the Examples online.
>
If you have a TTF or CFF font with the characters then iTextAsian.jar and
iTextAsianCmaps.jar are not needed.
> I know that iText isn't choking on the CJK characters,
> because I can export to RTF and all the characters appear in
> the resulting file. So, it's *just* PDF output that is doing
> something funny with CJK.
>
> So, the basic question is: am I doing something wrong? What
> do I need to change? I include a snippet of sample code,
> that shows how I am create Chunks which I then add to my PDF file.
>
The font must be created with BaseFont.createFont("font.ttf",
BaseFont.IDENTITY_H, true).
Paulo
> Thanks for any help,
> Cheers,
> Erik Norvelle
>
> =============== snip ===================
> /**
> * Recursively parse a given text into Chunks, with each Chunk
> given the font styling
> * appropriate to it, based on the HTML tags in the text.
> * @param text The text to parse into Chunks
> * @param font The current font to apply
> * @param phrase The currently existing Phrase to be added to.
> * @return The completed Phrase, made up of formatted Chunks.
> */
> protected Phrase chunkText(String text, Font font, Phrase phrase) {
> Matcher m = tagPattern.matcher(text);
> if (m.matches()) {
> // Separate out the text and the tag
> String currFontText = m.group(1);
> String tag = m.group(2);
> String remainderText = m.group(3);
> phrase.add(new Chunk(currFontText, font));
>
>
>
>
> int family;
> try {
> String fontFileName = font.getBaseFont().getTTFileName();
> if (fontFileName.contains("Mono")) family = Font.COURIER;
> else if (fontFileName.contains("Sans")) family = Font.HELVETICA;
> else family = Font.TIMES_ROMAN;
> } catch (Exception e) {
> family = Font.TIMES_ROMAN;
> }
>
>
>
>
> // Depending on the tag, adjust the font styling.
> // UnicodeFontFactory is my custom code for getting a Font
> object corresponding to the
> // GNU Free Fonts, which are embedded in the PDF.
> if (tag.equals("<i>"))
> font = UnicodeFontFactory.getFont(family, font.getSize(),
> font.getStyle() | Font.ITALIC);
> else if (tag.equals("<b>"))
> font = UnicodeFontFactory.getFont(family, font.getSize(),
> font.getStyle() | Font.BOLD);
> else if (tag.equals("<u>"))
> font = UnicodeFontFactory.getFont(family, font.getSize(),
> font.getStyle() | Font.UNDERLINE);
> else if (tag.equals("</i>"))
> font = UnicodeFontFactory.getFont(family, font.getSize(),
> font.getStyle() & ~Font.ITALIC);
> else if (tag.equals("</b>"))
> font = UnicodeFontFactory.getFont(family, font.getSize(),
> font.getStyle() & ~Font.BOLD);
> else
> font = UnicodeFontFactory.getFont(family, font.getSize(),
> font.getStyle() & ~Font.UNDERLINE);
>
>
>
>
> // Recurse to handle the rest of the text remaining
> phrase = chunkText(remainderText, font, phrase);
> }
> else
> // If we're at the end of the text, and there's no more tags,
> just add a final Chunk to the phrase.
> phrase.add(new Chunk(text, font));
> return phrase;
> }
>
>
>
>
>
>
>
Aviso Legal:
Esta mensagem é destinada exclusivamente ao destinatário. Pode conter
informação confidencial ou legalmente protegida. A incorrecta transmissão desta
mensagem não significa a perca de confidencialidade. Se esta mensagem for
recebida por engano, por favor envie-a de volta para o remetente e apague-a do
seu sistema de imediato. É proibido a qualquer pessoa que não o destinatário de
usar, revelar ou distribuir qualquer parte desta mensagem.
Disclaimer:
This message is destined exclusively to the intended receiver. It may contain
confidential or legally protected information. The incorrect transmission of
this message does not mean the loss of its confidentiality. If this message is
received by mistake, please send it back to the sender and delete it from your
system immediately. It is forbidden to any person who is not the intended
receiver to use, distribute or copy any part of this message.
------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now. http://p.sf.net/sfu/bobj-july
_______________________________________________
iText-questions mailing list
iText-questions@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/itext-questions
Buy the iText book: http://www.1t3xt.com/docs/book.php
Check the site with examples before you ask questions:
http://www.1t3xt.info/examples/
You can also search the keywords list: http://1t3xt.info/tutorials/keywords/