简体   繁体   中英

Itext hyphenation in Table cells

I´m creating a table using iText.
But the output looks really bad, because of the hyphenation, which seems not to be done properly.
I allready read this question How to hyphenate text?
and this example http://itextpdf.com/sandbox/tables/HyphenationExample
I tried the example in eclipse after i added the itext_hyph_xml.jar to my class path. No error is thrown when i run the code, but the lines

Hyphenator h = new Hyphenator("de", "DE", 2, 2);
Hyphenation s = h.hyphenate("Leistungsscheinziffer");
System.out.println(s);

print null to the console instead of "Lei-stungs-schein-zif-fer" or something similar as i expected.
I tried playing with the parameters in

chunk.setHyphenation(new HyphenationAuto("de", "DE", 2,2));

but the output in the document never differes even slightly.

The code i´m trying to get to work looks kind of like this by the way:

for(String s: interest){
        Chunk chunk = new Chunk(s,FONT[0]);
        chunk.setHyphenation(new HyphenationAuto("de", "DE", 2,3));
        table.addCell(new Phrase(chunk));

}

Ok I figured it out on my own now.
And because i couldn´t find the answer on the internet i thought i will share it here so anyone who might have the same error in the future wouldn´t need a week to figure it out.

It seems that in the class Hyphenator the defaultHyphLocation is set like this:

private static final String defaultHyphLocation = "com/itextpdf/text/pdf/hyphenation/hyph/";

But the structure of itext_hyph.jar looks like this:
com.lowagie.text.pdf.hyphenation.hyph
Obviously loading a hyph file from the jar will surely fail, since the path used by Hyphenator can not be found. I actually thought this would cause an error but it seems simply null is returned when the loading of the hyph file fails.
Calling Hyphenator.setHyphenDir("com/lowagie/text/pdf/hyphenation/hyph/");
however wouldn´t change a thing as one would think, since it changes the wrong string variable.
The only thing i could think of to solve this problem was to recreate the itext_hyph.jar according to the path given in Hyphenator and that acctually fixed it. Hyphenation is now working.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM