You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by ju...@apache.org on 2010/09/03 15:03:37 UTC

svn commit: r992282 - in /pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel: PDDocument.java PDPageable.java

Author: jukka
Date: Fri Sep  3 13:03:37 2010
New Revision: 992282

URL: http://svn.apache.org/viewvc?rev=992282&view=rev
Log:
PDFBOX-788: PrintPDF does not take the windows default printer orientation into account

Add a PDPageable adapter class to avoid problems in accessing the currentPrinterJob variable.

Added:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDPageable.java   (with props)
Modified:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java?rev=992282&r1=992281&r2=992282&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java Fri Sep  3 13:03:37 2010
@@ -16,17 +16,11 @@
  */
 package org.apache.pdfbox.pdmodel;
 
-import java.awt.Dimension;
 import java.awt.print.PageFormat;
 import java.awt.print.Pageable;
-import java.awt.print.Paper;
 import java.awt.print.Printable;
 import java.awt.print.PrinterException;
 import java.awt.print.PrinterJob;
-
-import javax.print.PrintService;
-import javax.print.attribute.standard.OrientationRequested;
-
 import java.io.BufferedInputStream;
 import java.io.File;
 import java.io.FileInputStream;
@@ -35,8 +29,8 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.net.URL;
-import java.util.List;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 
 import org.apache.pdfbox.cos.COSArray;
@@ -68,6 +62,10 @@ import org.apache.pdfbox.pdmodel.encrypt
 /**
  * This is the in-memory representation of the PDF document.  You need to call
  * close() on this object when you are done using it!!
+ * <p>
+ * This class implements the {@link Pageable} interface, but since PDFBox
+ * version 1.3.0 you should be using the {@link PDPageable} adapter instead
+ * (see <a href="https://issues.apache.org/jira/browse/PDFBOX-788">PDFBOX-788</a>).
  *
  * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
  * @version $Revision: 1.47 $
@@ -120,8 +118,6 @@ public class PDDocument implements Pagea
      */
     private boolean allSecurityToBeRemoved = false;
 
-    private PrinterJob currentPrinterJob = null;
-
     /**
      * Constructor, creates a new PDF Document with no pages.  You need to add
      * at least one page for the document to be valid.
@@ -950,46 +946,22 @@ public class PDDocument implements Pagea
     }
 
     /**
-     * {@inheritDoc}
+     * Returns the format of the page at the given index when using a
+     * default printer job returned by  {@link PrinterJob#getPrinterJob()}.
+     *
+     * @deprecated Use the {@link PDPageable} adapter class
+     * @param i page index, zero-based
+     * @return page format
+     * @throws IndexOutOfBoundsException if the page index is invalid
      */
     public PageFormat getPageFormat(int pageIndex)
     {
-        PDPage page = (PDPage)getDocumentCatalog().getAllPages().get( pageIndex );
-        Dimension mediaBox = page.findMediaBox().createDimension();
-        Dimension cropBox = page.findCropBox().createDimension();
-        PrintService printService = currentPrinterJob.getPrintService();
-        Object ob = printService.getDefaultAttributeValue(OrientationRequested.class);
-
-        double diffWidth = 0;
-        double diffHeight = 0;
-        double mediaWidth = mediaBox.getWidth();
-        double mediaHeight = mediaBox.getHeight();
-        double cropWidth = cropBox.getWidth();
-        double cropHeight = cropBox.getHeight();
-        // we have to center the ImageableArea if the cropBox is smaller than the mediaBox
-        if (!mediaBox.equals(cropBox))
-        {
-            diffWidth = (mediaWidth - cropWidth)/2;
-            diffHeight = (mediaHeight - cropHeight)/2;
-        }
-        PageFormat format = currentPrinterJob.defaultPage();
-        Paper paper = format.getPaper();
-
-        if ( "landscape" == ob.toString() )
-        {
-           format.setOrientation(PageFormat.LANDSCAPE);
-           paper.setImageableArea( diffHeight, diffWidth, cropHeight, cropWidth);
-           paper.setSize( mediaHeight, mediaWidth );
+        try {
+            PrinterJob printerJob = PrinterJob.getPrinterJob();
+            return new PDPageable(this, printerJob).getPageFormat(pageIndex);
+        } catch (PrinterException e) {
+            throw new RuntimeException(e);
         }
-        else
-        {
-           format.setOrientation(PageFormat.PORTRAIT);
-           paper.setImageableArea( diffWidth, diffHeight, cropWidth, cropHeight);
-           paper.setSize( mediaWidth, mediaHeight );
-        }
-
-        format.setPaper( paper );
-        return format;
     } 
 
     /**
@@ -1060,12 +1032,8 @@ public class PDDocument implements Pagea
     private void print(PrinterJob job, boolean silent) throws PrinterException {
         if (job == null) {
             throw new PrinterException("The given printer job is null.");
-        } else if (!getCurrentAccessPermission().canPrint()) {
-            throw new PrinterException(
-                    "You do not have permission to print this document.");
         } else {
-            currentPrinterJob = job;
-            job.setPageable(this);
+            job.setPageable(new PDPageable(this, job));
             if (silent || job.printDialog()) {
                 job.print();
             }

Added: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDPageable.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDPageable.java?rev=992282&view=auto
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDPageable.java (added)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDPageable.java Fri Sep  3 13:03:37 2010
@@ -0,0 +1,167 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.pdfbox.pdmodel;
+
+import static javax.print.attribute.standard.OrientationRequested.LANDSCAPE;
+
+import java.awt.Dimension;
+import java.awt.print.PageFormat;
+import java.awt.print.Pageable;
+import java.awt.print.Paper;
+import java.awt.print.Printable;
+import java.awt.print.PrinterException;
+import java.awt.print.PrinterJob;
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.print.PrintService;
+import javax.print.attribute.standard.OrientationRequested;
+
+/**
+ * Adapter class that implements the {@link Pageable} interface for
+ * printing a given PDF document. Note that the given PDF document should
+ * not be modified (pages added, removed, etc.) while an instance of this
+ * class is being used.
+ *
+ * @since Apache PDFBox 1.3.0
+ * @see <a href="https://issues.apache.org/jira/browse/PDFBOX-788">PDFBOX-788</a>
+ */
+public class PDPageable implements Pageable {
+
+    /**
+     * List of all pages in the given PDF document.
+     */
+    private final List<PDPage> pages = new ArrayList<PDPage>();
+
+    /**
+     * The printer job for printing the given PDF document.
+     */
+    private final PrinterJob job;
+
+    /**
+     * Creates a {@link Pageable} adapter for the given PDF document and
+     * printer job.
+     *
+     * @param document PDF document
+     * @param job printer job
+     * @throws IllegalArgumentException if an argument is <code>null</code>
+     * @throws PrinterException if the document permissions prevent printing
+     */
+    public PDPageable(PDDocument document, PrinterJob job)
+            throws IllegalArgumentException, PrinterException {
+        if (document == null || job == null) {
+            throw new IllegalArgumentException(
+                    "PDPageable(" + document + ", " + job + ")");
+        } else if (!document.getCurrentAccessPermission().canPrint()) {
+            throw new PrinterException(
+                "You do not have permission to print this document");
+        } else {
+            document.getDocumentCatalog().getPages().getAllKids(pages);
+            this.job = job;
+        }
+    }
+
+    /**
+     * Creates a {@link Pageable} adapter for the given PDF document using
+     * a default printer job returned by {@link PrinterJob#getPrinterJob()}.
+     *
+     * @param document PDF document
+     * @throws IllegalArgumentException if the argument is <code>null</code>
+     * @throws PrinterException if the document permissions prevent printing
+     */
+    public PDPageable(PDDocument document)
+            throws IllegalArgumentException, PrinterException {
+        this(document, PrinterJob.getPrinterJob());
+    }
+
+    /**
+     * Returns the printer job for printing the given PDF document.
+     *
+     * @return printer job
+     */
+    public PrinterJob getPrinterJob() {
+        return job;
+    }
+
+    //------------------------------------------------------------< Pageable >
+
+    /**
+     * Returns the number of pages in the given PDF document.
+     *
+     * @return number of pages
+     */
+    public int getNumberOfPages() {
+        return pages.size();
+    }
+
+    /**
+     * Returns the format of the page at the given index.
+     *
+     * @param i page index, zero-based
+     * @return page format
+     * @throws IndexOutOfBoundsException if the page index is invalid
+     */
+    public PageFormat getPageFormat(int i) throws IndexOutOfBoundsException {
+        PageFormat format = job.defaultPage();
+
+        PDPage page = pages.get(i); // can throw IOOBE
+        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)) {
+            diffWidth = (media.getWidth() - crop.getWidth()) / 2.0;
+            diffHeight = (media.getHeight() - crop.getHeight()) / 2.0;
+        }
+
+        Paper paper = format.getPaper();
+        PrintService service = job.getPrintService(); // can be null
+        Class<OrientationRequested> orientation = OrientationRequested.class;
+        if (service != null
+                && service.getDefaultAttributeValue(orientation) == LANDSCAPE) {
+           format.setOrientation(PageFormat.LANDSCAPE);
+           paper.setImageableArea(
+                   diffHeight, diffWidth, crop.getHeight(), crop.getWidth());
+           paper.setSize(media.getHeight(), media.getWidth());
+        } else {
+           format.setOrientation(PageFormat.PORTRAIT);
+           paper.setImageableArea(
+                   diffWidth, diffHeight, crop.getWidth(), crop.getHeight());
+           paper.setSize(media.getWidth(), media.getHeight());
+        }
+        format.setPaper(paper);
+
+        return format;
+    }
+
+    /**
+     * Returns a {@link Printable} for the page at the given index.
+     * Currently this method simply returns the underlying {@link PDPage}
+     * object that directly implements the {@link Printable} interface, but
+     * future versions may choose to return a different adapter instance.
+     *
+     * @param i page index, zero-based
+     * @return printable
+     * @throws IndexOutOfBoundsException if the page index is invalid
+     */
+    public Printable getPrintable(int i) throws IndexOutOfBoundsException {
+        return pages.get(i);
+    }
+
+}

Propchange: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDPageable.java
------------------------------------------------------------------------------
    svn:eol-style = native