I am using iText PDF 5.4 along with the Java2D interface (java.awt.Graphics
canvas), and I have a severe problem with gradients.
I am painting many rectangular shapes whose paint is a LinearGradientPaint
. This results in large files (e.g., 10 MB), and trying to open the results in e.g. Preview.app
brings the computer to total halt. The problem seems to be memory usage, because the first dozens of boxes paint rather quickly and then performance slows down somewhat linearly with more boxes, which means that for a typical page it takes >10 minutes to open.
Adobe Acrobat is also slow but at least it takes some 4 or 5 seconds instead of several minutes.
Is this a bug of iText? Is there a setting or tweak in iText that controls the representation of gradients? I guess it decomposes them into hundreds of separate paint commands instead of using a direct gradient component (if that exists—I know it exists in SVG, but PDF I have no clue).
The condition is that I stay in the awt.Graphics
, I cannot rewrite my rendering code to not use Java2D.
An alternative idea would be to use Apache Batik and output to SVG instead. There is an example that shows how to enable the correct transcoding of LinearGradientPaint
to the SVG equivalent.
EDIT: There seems to be a new Java2D-to-SVG library JFreeSVG. Recent changes indicate that gradients are implemented.