pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From le...@apache.org
Subject svn commit: r1601450 - /pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/rendering/PDFPrinter.java
Date Mon, 09 Jun 2014 17:32:44 GMT
Author: lehmi
Date: Mon Jun  9 17:32:44 2014
New Revision: 1601450

URL: http://svn.apache.org/r1601450
Log:
PDFBOX-2119: provide the correct page format when calling getPageFormat

Modified:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/rendering/PDFPrinter.java

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/rendering/PDFPrinter.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/rendering/PDFPrinter.java?rev=1601450&r1=1601449&r2=1601450&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/rendering/PDFPrinter.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/rendering/PDFPrinter.java Mon Jun
 9 17:32:44 2014
@@ -19,7 +19,6 @@ package org.apache.pdfbox.rendering;
 import org.apache.pdfbox.pdmodel.PDDocument;
 import org.apache.pdfbox.pdmodel.PDPage;
 import org.apache.pdfbox.pdmodel.common.PDRectangle;
-import org.apache.pdfbox.rendering.printing.Orientation;
 import org.apache.pdfbox.rendering.printing.Scaling;
 
 import java.awt.BasicStroke;
@@ -42,23 +41,22 @@ import java.io.IOException;
  * Prints a PDF document using AWT.
  * This class may be overridden in order to perform custom printing.
  *
- * @author Andreas Lehmk�hler
+ * @author Andreas Lehmkühler
  * @author John Hewson
  */
 public class PDFPrinter
 {
-    protected final PDDocument document;
-    protected final PDFRenderer renderer;
-    protected final PrinterJob printerJob;
-    protected final Scaling scaling;
-    protected final Orientation orientation;
-    protected final boolean showPageBorder;
-    protected final Paper paper; // may be null
-    protected final float dpi;
+    private final PDDocument document;
+    private final PDFRenderer renderer;
+    private final PrinterJob printerJob;
+    private final Scaling scaling;
+    private final boolean showPageBorder;
+    private final float dpi;
 
     /**
      * Creates a new PDFPrinter.
      * @param document the document to print
+     * @throws PrinterException if something went wrong
      */
     public PDFPrinter(PDDocument document) throws PrinterException
     {
@@ -69,62 +67,49 @@ public class PDFPrinter
      * Creates a new PDFPrinter for a given printer job.
      * @param document the document to print
      * @param printerJob the printer job to use
+     * @throws PrinterException if something went wrong
      */
     public PDFPrinter(PDDocument document, PrinterJob printerJob) throws PrinterException
     {
-        this(document, printerJob, Scaling.SHRINK_TO_FIT, Orientation.AUTO, null, false,
0);
+        this(document, printerJob, Scaling.SHRINK_TO_FIT, false, 0);
     }
 
     /**
-     * Creates a new PDFPrinter with the given page scaling and orientation.
+     * Creates a new PDFPrinter with the given page scaling.
      * @param document the document to print
      * @param scaling page scaling policy
-     * @param orientation page orientation policy
+     * @throws PrinterException if something went wrong
      */
-    public PDFPrinter(PDDocument document, Scaling scaling, Orientation orientation)
+    public PDFPrinter(PDDocument document, Scaling scaling)
             throws PrinterException
     {
-        this(document, PrinterJob.getPrinterJob(), scaling, orientation, null, false, 0);
+        this(document, PrinterJob.getPrinterJob(), scaling, false, 0);
     }
 
     /**
-     * Creates a new PDFPrinter with the given page scaling and orientation.
+     * Creates a new PDFPrinter with the given page scaling.
      * @param document the document to print
      * @param scaling page scaling policy
-     * @param orientation page orientation policy
-     */
-    public PDFPrinter(PDDocument document, Scaling scaling, Orientation orientation, Paper
paper)
-            throws PrinterException
-    {
-        this(document, PrinterJob.getPrinterJob(), scaling, orientation, paper, false, 0);
-    }
-
-    /**
-     * Creates a new PDFPrinter with the given page scaling and orientation.
-     * @param document the document to print
-     * @param scaling page scaling policy
-     * @param orientation page orientation policy
      * @param dpi if non-zero then the image will be rasterized at the given DPI
+     * @throws PrinterException if something went wrong
      */
-    public PDFPrinter(PDDocument document, Scaling scaling, Orientation orientation, Paper
paper,
-                      float dpi) throws PrinterException
+    public PDFPrinter(PDDocument document, Scaling scaling, float dpi) throws PrinterException
     {
-        this(document, PrinterJob.getPrinterJob(), scaling, orientation, paper, false, dpi);
+        this(document, PrinterJob.getPrinterJob(), scaling, false, dpi);
     }
 
     /**
-     * Creates a new PDFPrinter for a given printer job, the given page scaling and orientation,
+     * Creates a new PDFPrinter for a given printer job, the given page scaling,
      * and with optional page borders shown.
      * @param document the document to print
      * @param printerJob the printer job to use
      * @param scaling page scaling policy
-     * @param orientation page orientation 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
-     * @throws PrinterException
+     * @throws PrinterException if something went wrong
      */
     public PDFPrinter(PDDocument document, PrinterJob printerJob, Scaling scaling,
-                      Orientation orientation, Paper paper, boolean showPageBorder, float
dpi)
+                      boolean showPageBorder, float dpi)
             throws PrinterException
     {
         if (document == null)
@@ -143,9 +128,7 @@ public class PDFPrinter
         this.renderer = new PDFRenderer(document);
         this.printerJob = printerJob;
         this.scaling = scaling;
-        this.orientation = orientation;
         this.showPageBorder = showPageBorder;
-        this.paper = paper;
         this.dpi = dpi;
     }
 
@@ -191,6 +174,10 @@ public class PDFPrinter
     }
 
     // todo: new
+    /**
+     * Returns a newly create PDFPageable.
+     * @return an newly created instance of PDFPageable
+     */
     public PDFPageable getPageable()
     {
         return new PDFPageable();
@@ -213,6 +200,9 @@ public class PDFPrinter
         }
     }
 
+    /**
+     * PDFPageable implements the interface java.awt.print.Pageable.
+     */
     protected class PDFPageable implements Pageable
     {
         @Override
@@ -222,44 +212,66 @@ public class PDFPrinter
         }
 
         @Override
-        public PageFormat getPageFormat(int pageIndex) throws IndexOutOfBoundsException
+        public PageFormat getPageFormat(int pageIndex)
         {
-            PageFormat format = printerJob.defaultPage();
             PDPage page = document.getPage(pageIndex);
-
-            // auto portrait/landscape
-            if (orientation == Orientation.AUTO)
+            Dimension media = page.findMediaBox().createDimension();
+            Dimension crop = page.findCropBox().createDimension();
+            // Center the ImageableArea if the crop is smaller than the media
+            double diffWidth = 0.0;
+            double diffHeight = 0.0;
+            if( !media.equals( crop ) )
             {
-                Dimension cropBox = page.findRotatedCropBox().createDimension();
-                if (cropBox.getWidth() > cropBox.getHeight())
-                {
-                    format.setOrientation(PageFormat.LANDSCAPE);
-                }
-                else
-                {
-                    format.setOrientation(PageFormat.PORTRAIT);
-                }
+                   diffWidth = (media.getWidth() - crop.getWidth()) / 2.0;
+                   diffHeight = (media.getHeight() - crop.getHeight()) / 2.0;
             }
-            else if (orientation == Orientation.LANDSCAPE)
+            PageFormat wantedFormat = new PageFormat();
+            Paper wantedPaper = new Paper();
+            boolean hasRotation = page.findRotation() != 0;
+            Dimension rotatedCrop = null;
+            if (hasRotation)
             {
-                format.setOrientation(PageFormat.LANDSCAPE);
+                rotatedCrop = page.findRotatedCropBox().createDimension();
             }
-            else if (orientation == Orientation.PORTRAIT)
+            else
             {
-                format.setOrientation(PageFormat.PORTRAIT);
+                rotatedCrop = crop;
             }
 
-            // custom paper
-            if (paper != null)
+            if( rotatedCrop.getWidth() <= rotatedCrop.getHeight() )
             {
-                format.setPaper(paper);
+                   wantedFormat.setOrientation( PageFormat.PORTRAIT );
+                   if (hasRotation)
+                   {
+                       wantedPaper.setSize( media.getHeight(), media.getWidth() );
+                       wantedPaper.setImageableArea( diffHeight, diffWidth, crop.getHeight(),
crop.getWidth() );
+                   }
+                   else
+                   {
+                       wantedPaper.setSize( media.getWidth(), media.getHeight() );
+                       wantedPaper.setImageableArea( diffWidth, diffHeight, crop.getWidth(),
crop.getHeight() );
+                   }
             }
-
-            return format;
+            else
+            {
+                   wantedFormat.setOrientation( PageFormat.LANDSCAPE );
+                   if (hasRotation)
+                   {
+                       wantedPaper.setSize( media.getWidth(), media.getHeight() );
+                       wantedPaper.setImageableArea( diffWidth, diffHeight, crop.getWidth(),
crop.getHeight() );
+                   }
+                   else
+                   {
+                       wantedPaper.setSize( media.getHeight(), media.getWidth() );
+                       wantedPaper.setImageableArea( diffHeight, diffWidth, crop.getHeight(),
crop.getWidth() );
+                   }
+            }
+            wantedFormat.setPaper( wantedPaper );
+            return printerJob.validatePage( wantedFormat );
         }
 
         @Override
-        public Printable getPrintable(int i) throws IndexOutOfBoundsException
+        public Printable getPrintable(int i)
         {
             if (i >= getNumberOfPages())
             {
@@ -269,6 +281,9 @@ public class PDFPrinter
         }
     }
 
+    /**
+     * PDFPageable implements the interface java.awt.print.Printable.
+     */
     protected class PDFPrintable implements Printable
     {
         @Override



Mime
View raw message