You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@pdfbox.apache.org by Rich Stafford <ri...@equorum.com> on 2025/01/15 19:24:36 UTC
PDFBox render image to SVG, without transparency?
PDFBox:
We are using PDFBox (3.0.3) and Batik (1.18) to render PDF files to SVG, with great success. However, we have found that a text 'highlight' is rendered as stroked text, and an image overlay. Which is fine, except the <img> element for the highlight is defined without a transparency attribute, and thus obscures the underlying text.
I've attached a simple PDF test case, along with the rendered SVG result. If I add opacity="0.40" to the <img> element, it will display properly.
If we render this same PDF to JPG, the result is correct, with the text showing on top of the highlight, so it would appear that the render code does know that the image should be non-opaque?
Any suggestions as to how to handle this? What PDFBox call is handling the image rendering? I've already derived a sub-class from the SVGGraphics2d object to handle other issues?
Rich Stafford
Chief Scientist
eQuorum Corporation
Re: PDFBox render image to SVG, without transparency?
Posted by Rich Stafford <ri...@equorum.com>.
Tilman,
I ended up handling this case by copying and overriding the SVGGraphics2D drawImage() method as follows:
/**
* method to replace the SVGGraphics2D implementation and handle image transparency by
* adding an "opacity" to the DOM element.
*
* called with:
* oImage Image object
* nX int width of image in pels
* nY int height of image in pels
* ImageObserver ImageObserver object
*/
public boolean drawImage( Image oImage,
int nX,
int nY,
ImageObserver oObserver)
{
// setup the DOM element to add to doc
Element oImageElement = getGenericImageHandler().createElement(getGeneratorContext());
// set the transform and image handler
AffineTransform oTransForm = getGenericImageHandler().handleImage(
oImage,
oImageElement,
nX,
nY,
oImage.getWidth(null),
oImage.getHeight(null),
getGeneratorContext());
// determine it we need to add opacity to element
if ( oImage instanceof BufferedImage )
{ // is a buffered image, get it's transparency flag
int nTransparentType = ((BufferedImage) oImage).getTransparency();
if ( nTransparentType == ColorModel.TRANSLUCENT )
{ // yes, add attribute to element, using ImageSite default opacity value
String sOpacity = DbSystemDefaults.DEF_SVG_IMAGE_OPACITY;
oImageElement.setAttribute(CSS_OPACITY_PROPERTY, sOpacity);
}
}
// then add the element to the DOM
if (oTransForm == null)
{ // no transform, just add to DOM
domGroupManager.addElement(oImageElement);
}
else
{ // have a transform, calc the inverse
AffineTransform oInverseTransform = null;
try
{
oInverseTransform = oTransForm.createInverse();
}
catch(NoninvertibleTransformException e)
{
// This should never happen since handleImage
// always returns invertible transform
throw new SVGGraphics2DRuntimeException(ERR_UNEXPECTED);
}
// add transform, then the element, then invert the transform
gc.transform(oTransForm);
domGroupManager.addElement(oImageElement);
gc.transform(oInverseTransform);
}
return true;
}
Rich Stafford
Chief Scientist
eQuorum Corporation
***
At 11:44 PM 1/15/2025, Tilman Hausherr wrote:
>Hi,
>
>The PDF didn't get through. Please upload it to a sharehoster. Normally we would also need some code, but I have some that I can use.
>
>However I think I've seen this complaint before. The drawing is done in PageDrawer.drawBufferedImage(). The opacity is done in graphics.setComposite(....);
>
>Tilman
>
>On 15.01.2025 20:24, Rich Stafford wrote:
>>PDFBox:
>>
>>We are using PDFBox (3.0.3) and Batik (1.18) to render PDF files to SVG, with great success. However, we have found that a text 'highlight' is rendered as stroked text, and an image overlay. Which is fine, except the <img> element for the highlight is defined without a transparency attribute, and thus obscures the underlying text.
>>
>>I've attached a simple PDF test case, along with the rendered SVG result. If I add opacity="0.40" to the <img> element, it will display properly.
>>
>>If we render this same PDF to JPG, the result is correct, with the text showing on top of the highlight, so it would appear that the render code does know that the image should be non-opaque?
>>
>>Any suggestions as to how to handle this? What PDFBox call is handling the image rendering? I've already derived a sub-class from the SVGGraphics2d object to handle other issues?
>>
>>Rich Stafford
>>Chief Scientist
>>eQuorum Corporation
>>
>>---------------------------------------------------------------------
>>To unsubscribe, e-mail:users-unsubscribe@pdfbox.apache.org
>>For additional commands, e-mail:users-help@pdfbox.apache.org
>
Re: PDFBox render image to SVG, without transparency?
Posted by Tilman Hausherr <TH...@t-online.de>.
Hi,
The PDF didn't get through. Please upload it to a sharehoster. Normally
we would also need some code, but I have some that I can use.
However I think I've seen this complaint before. The drawing is done in
PageDrawer.drawBufferedImage(). The opacity is done in
graphics.setComposite(....);
Tilman
On 15.01.2025 20:24, Rich Stafford wrote:
> PDFBox:
>
> We are using PDFBox (3.0.3) and Batik (1.18) to render PDF files to
> SVG, with great success. However, we have found that a text
> 'highlight' is rendered as stroked text, and an image overlay. Which
> is fine, except the <img> element for the highlight is defined without
> a transparency attribute, and thus obscures the underlying text.
>
> I've attached a simple PDF test case, along with the rendered SVG
> result. If I add opacity="0.40" to the <img> element, it will display
> properly.
>
> If we render this same PDF to JPG, the result is correct, with the
> text showing on top of the highlight, so it would appear that the
> render code does know that the image should be non-opaque?
>
> Any suggestions as to how to handle this? What PDFBox call is
> handling the image rendering? I've already derived a sub-class from
> the SVGGraphics2d object to handle other issues?
>
> Rich Stafford
> Chief Scientist
> eQuorum Corporation
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail:users-unsubscribe@pdfbox.apache.org
> For additional commands, e-mail:users-help@pdfbox.apache.org