You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by ja...@apache.org on 2015/07/10 09:14:17 UTC
svn commit: r1690215 - in /pdfbox/trunk:
examples/src/main/java/org/apache/pdfbox/examples/printing/
pdfbox/src/main/java/org/apache/pdfbox/printing/
tools/src/main/java/org/apache/pdfbox/tools/
Author: jahewson
Date: Fri Jul 10 07:14:16 2015
New Revision: 1690215
URL: http://svn.apache.org/r1690215
Log:
PDFBOX-2844: Replace PDFPrinter with PDFPageable and PDFPrintable
Added:
pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/printing/
pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/printing/Printing.java (with props)
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/printing/PDFPageable.java (with props)
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/printing/PDFPrintable.java (with props)
Removed:
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/printing/PDFPrinter.java
Modified:
pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/PDFReader.java
pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/PrintPDF.java
Added: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/printing/Printing.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/printing/Printing.java?rev=1690215&view=auto
==============================================================================
--- pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/printing/Printing.java (added)
+++ pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/printing/Printing.java Fri Jul 10 07:14:16 2015
@@ -0,0 +1,143 @@
+/*
+ * 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.examples.printing;
+
+import java.awt.print.Book;
+import java.awt.print.PageFormat;
+import java.awt.print.Paper;
+import java.awt.print.PrinterException;
+import java.awt.print.PrinterJob;
+import java.io.File;
+import java.io.IOException;
+import javax.print.attribute.HashPrintRequestAttributeSet;
+import javax.print.attribute.PrintRequestAttributeSet;
+import javax.print.attribute.standard.PageRanges;
+import org.apache.pdfbox.pdmodel.PDDocument;
+import org.apache.pdfbox.printing.PDFPageable;
+import org.apache.pdfbox.printing.PDFPrintable;
+
+/**
+ * Examples of various different ways to print PDFs using PDFBox.
+ */
+public class Printing
+{
+ /**
+ * Entry point.
+ */
+ public static void main(String args[]) throws PrinterException, IOException
+ {
+ if (args.length != 1)
+ {
+ System.err.println("usage: java org.apache.pdfbox.examples.printing.Printing <input>");
+ System.exit(1);
+ }
+
+ String filename = args[0];
+ PDDocument document = PDDocument.load(new File(filename));
+
+ // choose your printing method:
+ print(document);
+ //printWithAttributes(document);
+ //printWithDialog(document);
+ //printWithDialogAndAttributes(document);
+ //printWithPaper(document);
+ }
+
+ /**
+ * Prints the document at its actual size. This is the recommended way to print.
+ */
+ private static void print(PDDocument document) throws IOException, PrinterException
+ {
+ PrinterJob job = PrinterJob.getPrinterJob();
+ job.setPageable(new PDFPageable(document));
+ job.print();
+ }
+
+ /**
+ * Prints using custom PrintRequestAttribute values.
+ */
+ private static void printWithAttributes(PDDocument document)
+ throws IOException, PrinterException
+ {
+ PrinterJob job = PrinterJob.getPrinterJob();
+ job.setPageable(new PDFPageable(document));
+
+ PrintRequestAttributeSet attr = new HashPrintRequestAttributeSet();
+ attr.add(new PageRanges(1, 1)); // pages 1 to 1
+
+ job.print(attr);
+ }
+
+ /**
+ * Prints with a print preview dialog.
+ */
+ private static void printWithDialog(PDDocument document) throws IOException, PrinterException
+ {
+ PrinterJob job = PrinterJob.getPrinterJob();
+ job.setPageable(new PDFPageable(document));
+
+ if (job.printDialog())
+ {
+ job.print();
+ }
+ }
+
+ /**
+ * Prints with a print preview dialog and custom PrintRequestAttribute values.
+ */
+ private static void printWithDialogAndAttributes(PDDocument document)
+ throws IOException, PrinterException
+ {
+ PrinterJob job = PrinterJob.getPrinterJob();
+ job.setPageable(new PDFPageable(document));
+
+ PrintRequestAttributeSet attr = new HashPrintRequestAttributeSet();
+ attr.add(new PageRanges(1, 1)); // pages 1 to 1
+
+ if (job.printDialog(attr))
+ {
+ job.print(attr);
+ }
+ }
+
+ /**
+ * Prints using a custom page size and custom margins.
+ */
+ private static void printWithPaper(PDDocument document)
+ throws IOException, PrinterException
+ {
+ PrinterJob job = PrinterJob.getPrinterJob();
+ job.setPageable(new PDFPageable(document));
+
+ // define custom paper
+ Paper paper = new Paper();
+ paper.setSize(306, 396); // 1/72 inch
+ paper.setImageableArea(0, 0, paper.getWidth(), paper.getHeight()); // no margins
+
+ // custom page format
+ PageFormat pageFormat = new PageFormat();
+ pageFormat.setPaper(paper);
+
+ // override the page format
+ Book book = new Book();
+ book.append(new PDFPrintable(document), pageFormat);
+ job.setPageable(book);
+
+ job.print();
+ }
+}
Propchange: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/printing/Printing.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/printing/PDFPageable.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/printing/PDFPageable.java?rev=1690215&view=auto
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/printing/PDFPageable.java (added)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/printing/PDFPageable.java Fri Jul 10 07:14:16 2015
@@ -0,0 +1,151 @@
+/*
+ * 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.printing;
+
+import java.awt.print.PageFormat;
+import java.awt.print.Pageable;
+import java.awt.print.Paper;
+import java.awt.print.Printable;
+import org.apache.pdfbox.pdmodel.PDDocument;
+import org.apache.pdfbox.pdmodel.PDPage;
+import org.apache.pdfbox.pdmodel.common.PDRectangle;
+
+/**
+ * Prints a PDF document using its original paper size.
+ *
+ * @author John Hewson
+ */
+public final class PDFPageable implements Pageable
+{
+ private final PDDocument document;
+ private final boolean showPageBorder;
+ private final float dpi;
+ private final Orientation orientation;
+
+ /**
+ * Creates a new PDFPageable.
+ *
+ * @param document the document to print
+ */
+ public PDFPageable(PDDocument document)
+ {
+ this(document, Orientation.AUTO, false, 0);
+ }
+
+ /**
+ * Creates a new PDFPageable with the given page orientation.
+ *
+ * @param document the document to print
+ * @param orientation page orientation policy
+ */
+ public PDFPageable(PDDocument document, Orientation orientation)
+ {
+ this(document, orientation, false, 0);
+ }
+
+ /**
+ * Creates a new PDFPageable with the given page orientation and with optional page borders
+ * shown. The image will be rasterized at the given DPI before being sent to the printer.
+ *
+ * @param document the document to print
+ * @param orientation page orientation policy
+ * @param showPageBorder true if page borders are to be printed
+ */
+ public PDFPageable(PDDocument document, Orientation orientation, boolean showPageBorder)
+ {
+ this(document, orientation, showPageBorder, 0);
+ }
+
+ /**
+ * Creates a new PDFPageable with the given page orientation and with optional page borders
+ * shown. The image will be rasterized at the given DPI before being sent to the printer.
+ *
+ * @param document the document to print
+ * @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
+ */
+ public PDFPageable(PDDocument document, Orientation orientation, boolean showPageBorder,
+ float dpi)
+ {
+ this.document = document;
+ this.orientation = orientation;
+ this.showPageBorder = showPageBorder;
+ this.dpi = dpi;
+ }
+
+ @Override
+ public int getNumberOfPages()
+ {
+ return document.getNumberOfPages();
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * Returns the actual physical size of the pages in the PDF file. May not fit the local printer.
+ */
+ @Override
+ public PageFormat getPageFormat(int pageIndex)
+ {
+ PDPage page = document.getPage(pageIndex);
+ PDRectangle mediaBox = page.getMediaBox();
+ PDRectangle cropBox = PDFPrintable.getRotatedCropBox(page);
+
+ Paper paper = new Paper();
+ paper.setSize(mediaBox.getWidth(), mediaBox.getHeight());
+ paper.setImageableArea(cropBox.getLowerLeftX(), cropBox.getLowerLeftY(),
+ cropBox.getWidth(), cropBox.getHeight());
+
+ PageFormat format = new PageFormat();
+ format.setPaper(paper);
+
+ // auto portrait/landscape
+ if (orientation == Orientation.AUTO)
+ {
+ if (cropBox.getWidth() > cropBox.getHeight())
+ {
+ format.setOrientation(PageFormat.LANDSCAPE);
+ }
+ else
+ {
+ format.setOrientation(PageFormat.PORTRAIT);
+ }
+ }
+ else if (orientation == Orientation.LANDSCAPE)
+ {
+ format.setOrientation(PageFormat.LANDSCAPE);
+ }
+ else if (orientation == Orientation.PORTRAIT)
+ {
+ format.setOrientation(PageFormat.PORTRAIT);
+ }
+
+ return format;
+ }
+
+ @Override
+ public Printable getPrintable(int i)
+ {
+ if (i >= getNumberOfPages())
+ {
+ throw new IndexOutOfBoundsException(i + " >= " + getNumberOfPages());
+ }
+ return new PDFPrintable(document, Scaling.ACTUAL_SIZE, showPageBorder, dpi);
+ }
+}
Propchange: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/printing/PDFPageable.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/printing/PDFPrintable.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/printing/PDFPrintable.java?rev=1690215&view=auto
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/printing/PDFPrintable.java (added)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/printing/PDFPrintable.java Fri Jul 10 07:14:16 2015
@@ -0,0 +1,213 @@
+/*
+ * 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.printing;
+
+import java.awt.BasicStroke;
+import java.awt.Color;
+import java.awt.Graphics;
+import java.awt.Graphics2D;
+import java.awt.geom.AffineTransform;
+import java.awt.image.BufferedImage;
+import java.awt.print.PageFormat;
+import java.awt.print.Printable;
+import java.awt.print.PrinterException;
+import java.awt.print.PrinterIOException;
+import java.io.IOException;
+import org.apache.pdfbox.pdmodel.PDDocument;
+import org.apache.pdfbox.pdmodel.PDPage;
+import org.apache.pdfbox.pdmodel.common.PDRectangle;
+import org.apache.pdfbox.rendering.PDFRenderer;
+
+/**
+ * Prints pages from a PDF document using any page size or scaling mode.
+ *
+ * @author John Hewson
+ */
+public final class PDFPrintable implements Printable
+{
+ private final PDDocument document;
+ private final PDFRenderer renderer;
+
+ private final boolean showPageBorder;
+ private final Scaling scaling;
+ private final float dpi;
+
+ /**
+ * Creates a new PDFPrintable.
+ *
+ * @param document the document to print
+ */
+ public PDFPrintable(PDDocument document)
+ {
+ this(document, Scaling.SHRINK_TO_FIT);
+ }
+
+ /**
+ * Creates a new PDFPrintable with the given page scaling.
+ *
+ * @param document the document to print
+ * @param scaling page scaling policy
+ */
+ public PDFPrintable(PDDocument document, Scaling scaling)
+ {
+ this(document, scaling, false, 0);
+ }
+
+ /**
+ * Creates a new PDFPrintable with the given page scaling and with optional page borders shown.
+ *
+ * @param document the document to print
+ * @param scaling page scaling policy
+ * @param showPageBorder true if page borders are to be printed
+ */
+ public PDFPrintable(PDDocument document, Scaling scaling, boolean showPageBorder)
+ {
+ this(document, scaling, showPageBorder, 0);
+ }
+
+ /**
+ * Creates a new PDFPrintable with the given page scaling and with optional page borders shown.
+ * The image will be rasterized at the given DPI before being sent to the printer.
+ *
+ * @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
+ */
+ public PDFPrintable(PDDocument document, Scaling scaling, boolean showPageBorder, float dpi)
+ {
+ this.document = document;
+ this.renderer = new PDFRenderer(document);
+ this.scaling = scaling;
+ this.showPageBorder = showPageBorder;
+ this.dpi = dpi;
+ }
+
+ @Override
+ public int print(Graphics graphics, PageFormat pageFormat, int pageIndex)
+ throws PrinterException
+ {
+ if (pageIndex < 0 || pageIndex >= document.getNumberOfPages())
+ {
+ return NO_SUCH_PAGE;
+ }
+ try
+ {
+ Graphics2D graphics2D = (Graphics2D)graphics;
+
+ PDPage page = document.getPage(pageIndex);
+ PDRectangle cropBox = getRotatedCropBox(page);
+
+ // the imageable area is the area within the page margins
+ final double imageableWidth = pageFormat.getImageableWidth();
+ final double imageableHeight = pageFormat.getImageableHeight();
+
+ double scale = 1;
+ if (scaling != Scaling.ACTUAL_SIZE)
+ {
+ // scale to fit
+ double scaleX = imageableWidth / cropBox.getWidth();
+ double scaleY = imageableHeight / cropBox.getHeight();
+ scale = Math.min(scaleX, scaleY);
+
+ // only shrink to fit when enabled
+ if (scale > 1 && scaling == Scaling.SHRINK_TO_FIT)
+ {
+ scale = 1;
+ }
+ }
+
+ // set the graphics origin to the origin of the imageable area (i.e the margins)
+ graphics2D.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
+
+ // center on page
+ graphics2D.translate((imageableWidth - cropBox.getWidth() * scale) / 2,
+ (imageableHeight - cropBox.getHeight() * scale) / 2);
+
+ // rasterize to bitmap (optional)
+ Graphics2D printerGraphics = null;
+ BufferedImage image = null;
+ if (dpi > 0)
+ {
+ float dpiScale = dpi / 72;
+ image = new BufferedImage((int)(imageableWidth * dpiScale),
+ (int)(imageableHeight * dpiScale),
+ BufferedImage.TYPE_INT_ARGB);
+
+ printerGraphics = graphics2D;
+ graphics2D = image.createGraphics();
+
+ // rescale
+ printerGraphics.scale(scale / dpiScale, scale / dpiScale);
+ scale = dpiScale;
+ }
+
+ // draw to graphics using PDFRender
+ AffineTransform transform = (AffineTransform)graphics2D.getTransform().clone();
+ graphics2D.setBackground(Color.WHITE);
+ renderer.renderPageToGraphics(pageIndex, graphics2D, (float)scale);
+
+ // draw crop box
+ if (showPageBorder)
+ {
+ graphics2D.setTransform(transform);
+ graphics2D.setClip(0, 0, (int)imageableWidth, (int)imageableHeight);
+ graphics2D.scale(scale, scale);
+ graphics2D.setColor(Color.GRAY);
+ graphics2D.setStroke(new BasicStroke(0.5f));
+ graphics.drawRect(0, 0, (int)cropBox.getWidth(), (int)cropBox.getHeight());
+ }
+
+ // draw rasterized bitmap (optional)
+ if (printerGraphics != null)
+ {
+ printerGraphics.setBackground(Color.WHITE);
+ printerGraphics.clearRect(0, 0, image.getWidth(), image.getHeight());
+ printerGraphics.drawImage(image, 0, 0, null);
+ graphics2D.dispose();
+ }
+
+ return PAGE_EXISTS;
+ }
+ catch (IOException e)
+ {
+ throw new PrinterIOException(e);
+ }
+ }
+
+ /**
+ * This will find the CropBox with rotation applied, for this page by looking up the hierarchy
+ * until it finds them.
+ *
+ * @return The CropBox at this level in the hierarchy.
+ */
+ static PDRectangle getRotatedCropBox(PDPage page)
+ {
+ PDRectangle cropBox = page.getCropBox();
+ int rotationAngle = page.getRotation();
+ if (rotationAngle == 90 || rotationAngle == 270)
+ {
+ return new PDRectangle(cropBox.getLowerLeftY(), cropBox.getLowerLeftX(),
+ cropBox.getHeight(), cropBox.getWidth());
+ }
+ else
+ {
+ return cropBox;
+ }
+ }
+}
Propchange: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/printing/PDFPrintable.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/PDFReader.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/PDFReader.java?rev=1690215&r1=1690214&r2=1690215&view=diff
==============================================================================
--- pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/PDFReader.java (original)
+++ pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/PDFReader.java Fri Jul 10 07:14:16 2015
@@ -46,7 +46,7 @@ import javax.swing.JScrollBar;
import javax.swing.AbstractAction;
import org.apache.pdfbox.pdmodel.PDPageTree;
-import org.apache.pdfbox.printing.PDFPrinter;
+import org.apache.pdfbox.printing.PDFPageable;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.apache.pdfbox.tools.util.RecentFiles;
import org.apache.pdfbox.tools.gui.PageWrapper;
@@ -179,9 +179,8 @@ public class PDFReader extends JFrame
{
if (document != null)
{
- PDFPrinter printer = new PDFPrinter(document);
PrinterJob job = PrinterJob.getPrinterJob();
- job.setPageable(printer.getPageable());
+ job.setPageable(new PDFPageable(document));
if (job.printDialog())
{
job.print();
Modified: pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/PrintPDF.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/PrintPDF.java?rev=1690215&r1=1690214&r2=1690215&view=diff
==============================================================================
--- pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/PrintPDF.java (original)
+++ pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/PrintPDF.java Fri Jul 10 07:14:16 2015
@@ -18,11 +18,9 @@ package org.apache.pdfbox.tools;
import java.awt.print.PrinterJob;
import java.io.File;
-
import javax.print.PrintService;
-
import org.apache.pdfbox.pdmodel.PDDocument;
-import org.apache.pdfbox.printing.PDFPrinter;
+import org.apache.pdfbox.printing.PDFPageable;
/**
* This is a command line program that will print a PDF document.
@@ -115,15 +113,11 @@ public class PrintPDF
}
}
}
-
- PDFPrinter printer = new PDFPrinter(document);
- if (silentPrint)
- {
- printer.silentPrint(printJob);
- }
- else
+ printJob.setPageable(new PDFPageable(document));
+
+ if (silentPrint || printJob.printDialog())
{
- printer.print(printJob);
+ printJob.print();
}
}
finally