This patch for JasperReports Library 5.6.1 adds hyphenation support in text fields for PDF outputs.
The patch uses the IText hyphenation feature implemented in the classes Hyphenator and HyphenationAuto.
http://api.itextpdf.com/itext/com/itextpdf/text/pdf/hyphenation/Hyphenator.html
http://api.itextpdf.com/itext/com/itextpdf/text/pdf/HyphenationAuto.html
Since JasperReports uses already IText, no additional library dependency is required.
However the IText hyphenation feature depends on language specific TEX pattern files.
These TEX pattern files have to be available in the classpath under 'com/lowagie/text/pdf/hyphenation/hyph/'
The attached itext-hyph-xml.jar (made from the IText maven artifact) contains these pattern files.
Most of the pattern files are available under an Apache license, because they are also used in the Apache FOP project, but some XML files were sent to IText by individual developers who obtained the files from different sources.
Please check the header of each individual file you need for its license!
Usage (make sure itext-hyph-xml.jar is available in the classpath):
net.sf.jasperreports.text.hyphenation=true
To activate hyphenation, set the property 'net.sf.jasperreports.text.hyphenation' to 'true'.
If this property is set to true, during fill time of the report, Jasper will use the IText Hyphenator to break the words. During generation of the PDF report with IText, the actual Hyphenation will be done by IText via HyphenationAuto. Since Jasper and IText do not use exactly the same logic for line breaks, this approach can lead to missing words in the PDF output. To fix this issue, use the property 'net.sf.jasperreports.text.hyphenation' together with the property 'net.sf.jasperreports.text.save.line.breaks'.
(Without this patch, use the property 'net.sf.jasperreports.export.pdf.force.linebreak.policy' to solve the issue of lost words in PDF outputs)
net.sf.jasperreports.text.save.line.breaks=true
This existing JasperReports property causes JasperReports to save the line breaks calculated during filling of the report. Jasper uses these line breaks when generating HTML output.
With this patch applied, JasperReports will now also use the saved line breaks for PDF outputs.
Both properties can be set globally, at report level or at text element level. (Only text element level tested!)
Its recommended to also enable net.sf.jasperreports.text.save.line.breaks when using net.sf.jasperreports.text.hyphenation. At least net.sf.jasperreports.export.pdf.force.linebreak.policy should be enabled when using the hyphenator feature.
If net.sf.jasperreports.export.pdf.force.linebreak.policy and net.sf.jasperreports.text.save.line.breaks are both enabled, net.sf.jasperreports.text.save.line.breaks will have priority.
Disclaimer:
This patch was created to solve a specific case creating PDF output. No guaranty is given that it will work in all circumstances.
Use at your own risk.
Recommended Comments