首页

关于使用pdfbox给予java.awt.print对PDF文件进行打印输出的示例代码

标签:Printing,打印pdf,pdfbox     发布时间:2018-04-16   

一、前言

下面通过pdfboxpdfbox-2.0.9源码中org.apache.pdfbox.examples.printing.Printing基于java.awt.print.*、org.apache.pdfbox.pdmodel.PDDocument进行打印输出内容的代码示例。

二、代码示例

package org.apache.pdfbox.examples.printing;@b@@b@import java.awt.print.Book;@b@import java.awt.print.PageFormat;@b@import java.awt.print.Paper;@b@import java.awt.print.PrinterException;@b@import java.awt.print.PrinterJob;@b@import java.io.File;@b@import java.io.IOException;@b@import javax.print.attribute.HashPrintRequestAttributeSet;@b@import javax.print.attribute.PrintRequestAttributeSet;@b@import javax.print.attribute.standard.PageRanges;@b@import javax.print.attribute.standard.Sides;@b@import org.apache.pdfbox.pdmodel.PDDocument;@b@import org.apache.pdfbox.pdmodel.interactive.viewerpreferences.PDViewerPreferences;@b@import org.apache.pdfbox.printing.PDFPageable;@b@import org.apache.pdfbox.printing.PDFPrintable;@b@@b@/**@b@ * Examples of various different ways to print PDFs using PDFBox.@b@ */@b@public final class Printing@b@{@b@    private Printing()@b@    {@b@    }   @b@@b@    /**@b@     * Entry point.@b@     */@b@    public static void main(String args[]) throws PrinterException, IOException@b@    {@b@        if (args.length != 1)@b@        {@b@            System.err.println("usage: java " + Printing.class.getName() + " <input>");@b@            System.exit(1);@b@        }@b@@b@        String filename = args[0];@b@        PDDocument document = PDDocument.load(new File(filename));@b@        @b@        // choose your printing method:@b@        print(document); @b@        //printWithAttributes(document);@b@        //printWithDialog(document);@b@        //printWithDialogAndAttributes(document);@b@        //printWithPaper(document);@b@        document.close();@b@    }@b@@b@    /**@b@     * Prints the document at its actual size. This is the recommended way to print.@b@     */@b@    private static void print(PDDocument document) throws IOException, PrinterException@b@    {@b@        PrinterJob job = PrinterJob.getPrinterJob();@b@        job.setPageable(new PDFPageable(document));@b@        job.print();@b@    }@b@@b@    /**@b@     * Prints using custom PrintRequestAttribute values.@b@     */@b@    private static void printWithAttributes(PDDocument document)@b@            throws IOException, PrinterException@b@    {@b@        PrinterJob job = PrinterJob.getPrinterJob();@b@        job.setPageable(new PDFPageable(document));@b@@b@        PrintRequestAttributeSet attr = new HashPrintRequestAttributeSet();@b@        attr.add(new PageRanges(1, 1)); // pages 1 to 1@b@@b@        job.print(attr);@b@    }@b@@b@    /**@b@     * Prints with a print preview dialog.@b@     */@b@    private static void printWithDialog(PDDocument document) throws IOException, PrinterException@b@    {@b@        PrinterJob job = PrinterJob.getPrinterJob();@b@        job.setPageable(new PDFPageable(document));@b@@b@        if (job.printDialog())@b@        {@b@            job.print();@b@        }@b@    }@b@@b@    /**@b@     * Prints with a print preview dialog and custom PrintRequestAttribute values.@b@     */@b@    private static void printWithDialogAndAttributes(PDDocument document)@b@            throws IOException, PrinterException@b@    {@b@        PrinterJob job = PrinterJob.getPrinterJob();@b@        job.setPageable(new PDFPageable(document));@b@@b@        PrintRequestAttributeSet attr = new HashPrintRequestAttributeSet();@b@        attr.add(new PageRanges(1, 1)); // pages 1 to 1@b@        @b@        PDViewerPreferences vp = document.getDocumentCatalog().getViewerPreferences();@b@        if (vp != null && vp.getDuplex() != null)@b@        {@b@            String dp = vp.getDuplex();@b@            if (PDViewerPreferences.DUPLEX.DuplexFlipLongEdge.toString().equals(dp))@b@            {@b@                attr.add(Sides.TWO_SIDED_LONG_EDGE);@b@            }@b@            else if (PDViewerPreferences.DUPLEX.DuplexFlipShortEdge.toString().equals(dp))@b@            {@b@                attr.add(Sides.TWO_SIDED_SHORT_EDGE);@b@            }@b@            else if (PDViewerPreferences.DUPLEX.Simplex.toString().equals(dp))@b@            {@b@                attr.add(Sides.ONE_SIDED);@b@            }@b@        }@b@@b@        if (job.printDialog(attr))@b@        {@b@            job.print(attr);@b@        }@b@    }@b@    @b@    /**@b@     * Prints using a custom page size and custom margins.@b@     */@b@    private static void printWithPaper(PDDocument document)@b@            throws IOException, PrinterException@b@    {@b@        PrinterJob job = PrinterJob.getPrinterJob();@b@        job.setPageable(new PDFPageable(document));@b@@b@        // define custom paper@b@        Paper paper = new Paper();@b@        paper.setSize(306, 396); // 1/72 inch@b@        paper.setImageableArea(0, 0, paper.getWidth(), paper.getHeight()); // no margins@b@@b@        // custom page format@b@        PageFormat pageFormat = new PageFormat();@b@        pageFormat.setPaper(paper);@b@        @b@        // override the page format@b@        Book book = new Book();@b@        // append all pages@b@        book.append(new PDFPrintable(document), pageFormat, document.getNumberOfPages());@b@        job.setPageable(book);@b@        @b@        job.print();@b@    }@b@}


<<热门下载>>