【问题标题】:Orientation sets only for first page in PDF printing - java方向设置仅适用于 PDF 打印的第一页 - java
【发布时间】:2014-10-02 09:43:52
【问题描述】:

我正在使用 PDFRenderer.jar,这是我用来设置打印格式的方法,

private void initialize(byte[] pdfContent, String jobName) throws IOException, PrinterException {
        ByteBuffer bb = ByteBuffer.wrap(pdfContent);
        // Create PDF Print Page
        PDFFile pdfFile = new PDFFile(bb);
        PDFPrintPage pages = new PDFPrintPage(pdfFile);

        // Create Print Job
        pjob = PrinterJob.getPrinterJob();
        PageFormat pf = PrinterJob.getPrinterJob().defaultPage();

        pf.setOrientation(PageFormat.PORTRAIT);
        pjob.setJobName(jobName);

        Book book = new Book();
        book.append(pages, pf, pdfFile.getNumPages());
        pjob.setPageable(book);

//         to remove margins
        Paper paper = new Paper();
        paper.setSize(paper.getWidth(), paper.getHeight());
        paper.setImageableArea(0, 0, paper.getWidth(), paper.getHeight());

        pf.setPaper(paper);
    }

问题是,方向 PORTRAIT 仅设置为 pdf 的第一页,第二页位于 REVERSE_LANDSCAPE 中,这是我没有从代码中设置的。

有什么想法吗?

【问题讨论】:

    标签: java pdf printing pdf-reader pdfrenderer


    【解决方案1】:

    终于找到了

    我们不能仅通过将属性设置为 java.awt.print.PageFormat 来设置打印格式,我们必须覆盖 java.awt.print.Printable 中的 print(Graphics g, PageFormat format, int index) 方法。这使得文件以 pdf 格式(文本和对齐方式)打印。

    因此这是重写的打印方法,

      public int print(Graphics g, PageFormat format, int index) throws PrinterException {
            int pagenum = index + 1;
            if ((pagenum >= 1) && (pagenum <= file.getNumPages())) {
                Graphics2D g2 = (Graphics2D) g;
                PDFPage page = file.getPage(pagenum);
    
                // fit the PDFPage into the printing area
                Rectangle imageArea = new Rectangle((int) format.getImageableX(), (int) format.getImageableY(),
                        (int) format.getImageableWidth(), (int) format.getImageableHeight());
                g2.translate(0, 0);
                PDFRenderer pgs = new PDFRenderer(page, g2, imageArea, null, null);
                try {
                    page.waitForFinish();
                    pgs.run();
                } catch (InterruptedException ie) {
                    // nothing to do
                }
                return PAGE_EXISTS;
            } else {
                return NO_SUCH_PAGE;
            }
        }
    

    所以,这里是使用 pdf 渲染器打印 pdf 的完整代码(对于不支持直接打印的打印机),

        public static void main(String[] args) throws IOException, PrinterException {
        if (args.length != 1) {
            System.err.println("The first parameter must have the location of the PDF file to be printed");
        }
    
        FileInputStream fis = new FileInputStream(new File("x.pdf"));//file path
    
        PrintPdf printPDFFile = new PrintPdf(fis, "Test Print PDF");
        printPDFFile.print();
    }
    
    public static PrintService setPrintService(String argPrintServiceName) throws PrinterException {
        PrintService psr = null;
        PrintService[] printServices = PrinterJob.lookupPrintServices();
        int i;
        for (i = 0; i < printServices.length; i++) {
            if (printServices[i].getName().equalsIgnoreCase(argPrintServiceName)) {
                psr = printServices[i];
                break;
            }
        }
        if (i == printServices.length) {
            throw new PrinterException("Invalid print service name: " + argPrintServiceName);
        }
        return psr;
    }
    
    /**
     * Constructs the print job based on the input stream
     *
     * @param inputStream
     * @param jobName
     * @throws IOException
     * @throws PrinterException
     */
    public PrintPdf(InputStream inputStream, String jobName) throws IOException, PrinterException {
        byte[] pdfContent = new byte[inputStream.available()];
        inputStream.read(pdfContent, 0, inputStream.available());
        initialize(pdfContent, jobName);
    }
    
    /**
     * Initializes the job
     *
     * @param pdfContent
     * @param jobName
     * @throws IOException
     * @throws PrinterException
     */
    private void initialize(byte[] pdfContent, String jobName) throws IOException, PrinterException {
        ByteBuffer bb = ByteBuffer.wrap(pdfContent);
        /* Create PDF Print Page*/
        PDFFile pdfFile = new PDFFile(bb);
        PDFPrintPage pages = new PDFPrintPage(pdfFile);
    
        /* Create Print Job  */
        pjob = PrinterJob.getPrinterJob();
        pf = PrinterJob.getPrinterJob().defaultPage();
    
        pf.setOrientation(PageFormat.PORTRAIT);
        pjob.setJobName(jobName);
    
        Book book = new Book();
        book.append(pages, pf, pdfFile.getNumPages());
        pjob.setPageable(book);
    
        /* to remove margins  */
        Paper paper = new Paper();
        paper.setImageableArea(0, 0, paper.getWidth(), paper.getHeight());
    
        pf.setPaper(paper);
    }
    
    public void print() throws PrinterException {
        // Send print job to default printer
        pjob.print();
    }
    
    /**
     * Class that actually converts the PDF file into Printable format
     */
    class PDFPrintPage implements Printable {
    
        private PDFFile file;
    
        PDFPrintPage(PDFFile file) {
            this.file = file;
        }
    
        public int print(Graphics g, PageFormat format, int index) throws PrinterException {
            int pagenum = index + 1;
            if ((pagenum >= 1) && (pagenum <= file.getNumPages())) {
                Graphics2D g2 = (Graphics2D) g;
                PDFPage page = file.getPage(pagenum);
    
                // fit the PDFPage into the printing area
                Rectangle imageArea = new Rectangle((int) format.getImageableX(),(int)format.getImageableY(),(int) format.getImageableWidth(), (int) format.getImageableHeight());
                g2.translate(0, 0);
                PDFRenderer pgs = new PDFRenderer(page, g2, imageArea, null, null);
                try {
                    page.waitForFinish();
                    pgs.run();
                } catch (InterruptedException ie) {
                    // nothing to do
                }
                return PAGE_EXISTS;
            } else {
                return NO_SUCH_PAGE;
            }
        }
    
    }
    

    感谢任何试图找到该错误的人以及我的高级工程师对找到此问题的支持。

    【讨论】:

      猜你喜欢
      • 2019-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-09
      相关资源
      最近更新 更多