【问题标题】:How to print PDF with cropped printer's hard margins instead of shift?如何使用裁剪的打印机硬边距而不是移位打印 PDF?
【发布时间】:2019-09-03 21:43:59
【问题描述】:

我正在尝试在特殊类型的纸张上打印 PDF,其中内容的位置很重要,并且不允许移动。

我正在使用java.awt.print.PrinterJoborg.apache.pdfbox.printing.PDFPrintable


    public void printPDF(byte[] pdf) throws IOException, PrinterException {
        MediaSize media = MediaSize.getMediaSizeForName(MediaSizeName.ISO_A4);

        PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
        attributes.add(media.getMediaSizeName());
        attributes.add(new MediaPrintableArea(
                0, 0, media.getSize(1)[0], media.getSize(1)[1],
                1
        ));

        PrinterJob job = PrinterJob.getPrinterJob();

        if (job.printDialog(attributes)) {
            PageFormat pageFormat = (PageFormat) job.getPageFormat(attributes).clone();


            Paper paper = pageFormat.getPaper();
            paper.setImageableArea(0, 0, paper.getWidth(), paper.getHeight());
            pageFormat.setPaper(paper);

            PDDocument document = PDDocument.load(pdf);
            PDFPrintable printable = new PDFPrintable(
                    document,
                    Scaling.ACTUAL_SIZE,
                    false,
                    0,
                    false
            );
            job.setPrintable(printable, pageFormat);
            job.print(attributes);
        }
    }

原始 PDF 如下所示:

但是,打印出来的是移位的:

因此,我希望不打印虚线边框而不是移位,而不是整个文档的移位。

很遗憾,我无法在不移动内容的情况下打印 PDF,..

【问题讨论】:

    标签: java printing pdfbox java-print


    【解决方案1】:

    如果PrinterJob的内容是使用setPrintable方法设置的,那么在PrinterJob内部会做一些机器,并修改指定的PageFormat,...

    工作方案#1:使用setPageablePDFPageable,使用文档的页面格式进行打印:

        public void printPDF(byte[] pdf) throws IOException, PrinterException {
            PDDocument document = PDDocument.load(pdf);
    
            PrinterJob job = PrinterJob.getPrinterJob();
            job.setPageable(new PDFPageable(document));
    
            if (job.printDialog()) {
                job.print();
            }
        }
    
    

    工作解决方案 #2:使用 setPageableBook 可分页。 Book 使用指定的页面格式进行打印:

        public void printPDF(byte[] pdf) throws IOException, PrinterException {
            PDDocument document = PDDocument.load(pdf);
    
            MediaSize media = MediaSize.getMediaSizeForName(MediaSizeName.ISO_A4);
            PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
            attributes.add(media.getMediaSizeName());
    
            PrinterJob job = PrinterJob.getPrinterJob();
    
            if (job.printDialog(attributes)) {
                PageFormat pageFormat = (PageFormat) job.getPageFormat(attributes).clone();
    
                Paper paper = pageFormat.getPaper();
                paper.setImageableArea(0, 0, paper.getWidth(), paper.getHeight());
                pageFormat.setPaper(paper);
    
                PDFPrintable printable = new PDFPrintable(document, Scaling.ACTUAL_SIZE, false, 0, false);
    
                Book book = new Book();
                book.append(printable, pageFormat);
                job.setPageable(book);
    
                job.print(attributes);
            }
        }
    

    其他解决方案:这是我自己对自己问题的回答。答案基于在此问题上花费的几个小时的工作/调查,幸运的是找到了一些解决方案。

    如果您知道下面发生了什么,并且可以提供更详细的答案,解释实际发生的情况,以及 PrinterJob 的工作原理,那么我会很高兴,并很乐意批准您更详细的答案。

    【讨论】:

      猜你喜欢
      • 2013-08-10
      • 1970-01-01
      • 2021-11-02
      • 1970-01-01
      • 2011-11-09
      • 2013-09-09
      • 2016-10-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多