如何在A4纵向中正确打印pdf?

时间:2019-01-15 11:09:27

标签: java pdf printing

我设法打印pdf文件,但输出方式错误:打印的页面较小(占A4纸的一半),文档的每个字符都带有一个小方块,就像一个未知的字母

我尝试使用paper.setSize(x,y),setImageableArea和MediaSizeName更改纸张尺寸,但是当我这样做时,打印机出现错误并停止工作(红色指示灯)。

public static void printPdf3 (String filePath, String jobName) throws IOException, PrinterException {

    FileInputStream fileInputStream = new FileInputStream(filePath);
    byte[] pdfContent = new byte[fileInputStream.available()];
    fileInputStream.read(pdfContent, 0, fileInputStream.available());
    ByteBuffer buffer = ByteBuffer.wrap(pdfContent);

    final PDFFile pdfFile = new PDFFile(buffer);
    PDFPrintPage pages = new PDFPrintPage(pdfFile);
    PrinterJob printJob = PrinterJob.getPrinterJob();
    PageFormat pageFormat = PrinterJob.getPrinterJob().defaultPage();
    pageFormat.setOrientation(PageFormat.PORTRAIT);
    printJob.setJobName(jobName);
    Book book = new Book();

    book.append(pages, pageFormat, pdfFile.getNumPages());
    printJob.setPageable(book);
    Paper paper = new Paper();

    paper.setSize(210,297); //A4
    paper.setImageableArea(0, 0, paper.getWidth()-20, paper.getHeight()-20);
    System.out.println(paper.getHeight());
    System.out.println(paper.getWidth());
    pageFormat.setPaper(paper);

    AttributeSet aset = new HashAttributeSet();

    aset.add(new PrinterName("\\\\ber-ser-2\\Laser CED", null));
    PrintService[] pservices = PrintServiceLookup.lookupPrintServices(null, aset);

    PrintRequestAttributeSet attr_set = new HashPrintRequestAttributeSet();
    attr_set.add(MediaSizeName.ISO_A4);

    printJob.print(attr_set);
    fileInputStream.close();
}

我希望以正确的方式(当然,所有字符都可读)和纵向模式以A4格式打印文件。

0 个答案:

没有答案