Hi,

I've committed your suggestion with some minor changes, among them this: I did not use "if (rasterDpi != RASTERIZE_OFF)" and used "if (dpi > 0 || dpi == RASTERIZE_DPI_AUTO)" because in the past, a negative dpi would be treated like dpi 0 (i.e. no rastering) and I wanted to keep it that way. I've corrected the chaining of the constructors (this is cosmetic only) and added more text for the command line application.
Snapshot available at
https://repository.apache.org/content/groups/snapshots/org/apache/pdfbox/pdfbox-app/3.0.4-SNAPSHOT/
Tilman

On 03.12.2024 16:13, Kevin Day wrote:
I just tested and the new code works great.  Thank you!

One thought on the code changes:  It may be beneficial to compute a
printerDpi variable at the top of the method.  My concern is that if
graphics2D ever has a scale operation before we hit the dpi check, it could
cause an unexpected regression when we compute the printer dpi later on.
This is *not* an issue today (the graphics2D operations are all
translation) - just thinking to the future.

Here is how I originally implemented it when I was doing my testing (this
patch also adds constants and updates the javadocs).  I'm fine either way,
just trying to keep the cyclometric complexity down so it'll be easier to
maintain.


Index: src/main/java/org/apache/pdfbox/printing/PDFPrintable.java
===================================================================
--- src/main/java/org/apache/pdfbox/printing/PDFPrintable.java (revision
1922281)
+++ src/main/java/org/apache/pdfbox/printing/PDFPrintable.java (working
copy)
@@ -46,6 +46,8 @@
  public final class PDFPrintable implements Printable
  {
      private static final Log LOG = LogFactory.getLog(PDFPrintable.class);
+    public static final float RASTERIZE_OFF = 0f;
+    public static final float RASTERIZE_DPI_AUTO = -1f;

      private final PDPageTree pageTree;
      private final PDFRenderer renderer;
@@ -75,7 +77,7 @@
       */
      public PDFPrintable(PDDocument document, Scaling scaling)
      {
-       this(document, scaling, false, 0);
+       this(document, scaling, false, RASTERIZE_OFF);
      }

      /**
@@ -87,7 +89,7 @@
       */
      public PDFPrintable(PDDocument document, Scaling scaling, boolean
showPageBorder)
      {
-        this(document, scaling, showPageBorder, 0);
+        this(document, scaling, showPageBorder, RASTERIZE_OFF);
      }

      /**
@@ -97,7 +99,7 @@
       * @param document the document to print
       * @param scaling page scaling policy
       * @param showPageBorder true if page borders are to be printed
-     * @param dpi if non-zero then the image will be rasterized at the
given DPI
+     * @param dpi if non-zero then the image will be rasterized at the
given DPI. If set to the special value RASTERIZE_DPI_AUTO, the dpi of the
printer will be used.
       */
      public PDFPrintable(PDDocument document, Scaling scaling, boolean
showPageBorder, float dpi)
      {
@@ -111,7 +113,7 @@
       * @param document the document to print
       * @param scaling page scaling policy
       * @param showPageBorder true if page borders are to be printed
-     * @param dpi if non-zero then the image will be rasterized at the
given DPI
+     * @param dpi if non-zero then the image will be rasterized at the
given DPI. If set to the special value RASTERIZE_DPI_AUTO, the dpi of the
printer will be used.
       * @param center true if the content is to be centered on the page
(otherwise top-left).
       */
      public PDFPrintable(PDDocument document, Scaling scaling, boolean
showPageBorder, float dpi,
@@ -127,7 +129,7 @@
       * @param document the document to print
       * @param scaling page scaling policy
       * @param showPageBorder true if page borders are to be printed
-     * @param dpi if non-zero then the image will be rasterized at the
given DPI
+     * @param dpi if non-zero then the image will be rasterized at the
given DPI. If set to the special value RASTERIZE_DPI_AUTO, the dpi of the
printer will be used.
       * @param center true if the content is to be centered on the page
(otherwise top-left).
       * @param renderer the document renderer. Useful if {@link
PDFRenderer} has been subclassed.
       */
@@ -202,6 +204,9 @@
          try
          {
              Graphics2D graphics2D = (Graphics2D)graphics;
+
+            float rasterDpi = dpi; // capture the DPI that will be used
for rasterizing the image if rasterizing is specified (i.e. dpi != 0)
+            if (rasterDpi == RASTERIZE_DPI_AUTO) rasterDpi =
(float)graphics2D.getTransform().getScaleX() * 72.0f;

              PDPage page = pageTree.get(pageIndex);
              PDRectangle cropBox = getRotatedCropBox(page);
@@ -254,21 +259,15 @@
              // rasterize to bitmap (optional)
              Graphics2D printerGraphics = null;
              BufferedImage image = null;
-            if (dpi > 0 || dpi == -1)
+            if (rasterDpi != RASTERIZE_OFF)
              {
-                float dpiScale;
-                if (dpi == -1)
-                {
-                    dpiScale = (float)
graphics2D.getTransform().getScaleX();
-                    if (LOG.isDebugEnabled())
-                    {
-                        LOG.debug("dpi set to " +
Math.round(graphics2D.getTransform().getScaleX() * 72));
-                    }
-                }
-                else
+                if (LOG.isDebugEnabled())
                  {
-                    dpiScale = dpi / 72;
+                    LOG.debug("Rasterize dpi set to " + rasterDpi);
                  }
+
+                float dpiScale = rasterDpi / 72;
+
                  image = new BufferedImage((int)(imageableWidth * dpiScale
/ scale),
                                            (int)(imageableHeight * dpiScale
/ scale),
                                            BufferedImage.TYPE_INT_ARGB);


Kevin Day

*trumpet**p| *480.961.6003 x1002
*e| *ke...@trumpetinc.com
*www.trumpetinc.com<http://trumpetinc.com/> | *LinkedIn
<https://www.linkedin.com/company/trumpet-inc.>


On Tue, Dec 3, 2024 at 7:36 AM Kevin Day<ke...@trumpetinc.com> wrote:

My apologies, I did not see the notice that changes were available. I will
review and test this morning.

On Tue, Dec 3, 2024, 2:51 AM Tilman Hausherr<thaush...@t-online.de>
wrote:

On 02.12.2024 17:50, Kevin Day wrote:
Just to capture it, I got the printer model one of our customers is
having
the problem with:

Konica Minolta bizhubC300i
Have you tested the changes?

Tilman




---------------------------------------------------------------------
To unsubscribe, e-mail:users-unsubscr...@pdfbox.apache.org
For additional commands, e-mail:users-h...@pdfbox.apache.org


Reply via email to