Hi,
Armando Singer schrieb:
Attached is a patch that allows a transparent pdf background to be retained if an
rgba image type is selected. Rather than switching on BufferedImage TYPE constants,
it appears the simplest solution is to just use fully transparent white. Image
types with alpha will get the expected transparent background while others get white.
Does this make sense for inclusion into pdfbox?
Sounds reasonable. Please create an issue on JIRA [1] and attach your patch to
it and don't forget to check the box to grant the license to the ASF. If
possible attach an example pdf as well.
Thanks in advance and sorry for the late answer
Andreas Lehmkühler
[1] https://issues.apache.org/jira/browse/PDFBOX
Examples:
- convert to png image with rgba color -> image w/ transparent background (new)
- convert to png image with rgb color -> image w/ white background (as before)
- convert to png image with rgb color -> image w/ white background (as before)
- convert to jpg image with rgb image type -> image w/ white background (as
before)
...
Index: pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDPage.java
===================================================================
--- pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDPage.java (revision
945442)
+++ pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDPage.java (working copy)
@@ -69,7 +69,12 @@
private static final int DEFAULT_USER_SPACE_UNIT_DPI = 72;
private static final float MM_TO_UNITS = 1/(10*2.54f)*DEFAULT_USER_SPACE_UNIT_DPI;
-
+
+ /**
+ * Fully transparent that can fall back to white when image type has no
alpha.
+ */
+ private static final Color TRANSPARENT_WHITE = new Color( 255, 255, 255, 0
);
+
private COSDictionary page;
/**
@@ -706,7 +711,7 @@
BufferedImage retval = new BufferedImage( widthPx, heightPx, imageType );
Graphics2D graphics = (Graphics2D)retval.getGraphics();
- graphics.setBackground( Color.WHITE );
+ graphics.setBackground( TRANSPARENT_WHITE );
graphics.clearRect( 0, 0, retval.getWidth(), retval.getHeight() );
graphics.scale( scaling, scaling );
PageDrawer drawer = new PageDrawer();
------------------------------------------------------------------------
Thank you,
Armando
On May 17, 2010, at 3:30 PM, Armando Singer wrote:
PDFToImage / PDFImageWriter seem to work very well for converting
mainly text pdf documents to an image.
However, my goal is to convert mainly text pdf documents into a png with
multiple levels of alpha transparency and to retain the transparent
background in the original PDF.
It looks like the only thing preventing this is:
graphics.setBackground( Color.WHITE );
Inside PDPage convertToImage(int, int);
I can get the desired result with:
graphics.setBackground( new Color( 0, 0, 0, 0 ) );
Questions:
1. Should the pdfbox default to a transparent background, at least if
the chosen color mode is rgba? I'd rather not have to maintain a
patch on the source!
2. Is there a better way to accomplish this? I'm not sure if just
settign the background to transparent is the right thing to do in
all cases, but it does acheive the desired result whent the pdf is
just text (including antialiased parts with more than one bit of
alpha).
Thank you!
Armando