【问题标题】:How to prevent PDFBox 1.8's print to leave a margin on the top of printed document?如何防止 PDFBox 1.8 的打印在打印文档的顶部留下边距?
【发布时间】:2016-08-24 20:29:31
【问题描述】:

我使用 PDFBox (1.8.12) 从 java 打印 pdf 文档:

PDDocument pdf = PDDocument.load(new File(args[0]));
PrinterJob job = PrinterJob.getPrinterJob();

PrintRequestAttributeSet attr_set = new HashPrintRequestAttributeSet();
attr_set.add(MediaSizeName.ISO_A4); // <<< supposedly prints in A4 format
attr_set.add(Sides.ONE_SIDED);

PDPageable p = new PDPageable(pdf);


job.setPageable(p);

PrintService ps = null;
for  (PrintService i : PrintServiceLookup.lookupPrintServices(null,null)) {
    if (i.getName().equals(args[1])) {
        ps = i;
    }
}

if (ps == null) {
    try {
        throw new SystemException(ErrorCode.NO_PRINTER_FOUND);
    } catch (SystemException e) {
        e.printStackTrace();
    }
}
else
{

    job.setPrintService(ps);
    job.print(attr_set);

}

问题是,打印的文档有一个被裁切的边距,我不知道为什么。我进行了测试,将pdf循环回pdf虚拟打印机,似乎是一样的,我认为这意味着PDFbox不会以错误的方式处理pdf。

经过进一步研究,打印纸上的文字似乎被放大了,它比原来的开始更高,而最终更低(当我直接从混凝土打印机打印时)。

我打印的纸张是A4格式,所以我尝试像上面一样将格式设置为A4,但问题仍然存在。

【问题讨论】:

    标签: java pdf printing pdfbox


    【解决方案1】:

    我找到了这个解决方案:

    PDFPageable p = new PDFPageable(pdf);
    PDFPrintable printable = new PDFPrintable(pdf,Scaling.SCALE_TO_FIT);
    
    job.setPageable(p);
    job.setPrintable(printable);
    

    应用正确的格式。如果我必须删除它,我将进行编辑,但现在,我将把整个内容发布在 github 上: GPierre-Antoine:Print_A4_APACHE_PDFBOX)

    如果我犯了错误,请随时 fork/评论/帮助。

    这里我对上述代码做了一个摘录:

    package com.pierreantoineguillaume;
    
    import org.apache.pdfbox.pdmodel.PDDocument;
    import org.apache.pdfbox.printing.PDFPageable;
    import org.apache.pdfbox.printing.PDFPrintable;
    import org.apache.pdfbox.printing.Scaling;
    
    import javax.print.PrintService;
    import javax.print.PrintServiceLookup;
    import javax.print.attribute.HashPrintRequestAttributeSet;
    import javax.print.attribute.PrintRequestAttributeSet;
    import javax.print.attribute.standard.MediaSizeName;
    import java.awt.print.PrinterException;
    import java.awt.print.PrinterJob;
    import java.io.File;
    import java.io.IOException;
    import java.util.NoSuchElementException;
    import java.util.Optional;
    
    public class BasicPrinter {
    
        public static void main(String[] args) {
            BasicPrinter basicPrinter = new BasicPrinter();
            String printerName = "name of my printer";
            String filename = "my file to print";
    
            try {
                Optional<PrintService> printService = basicPrinter.getMatchingPrintService(printerName);
                basicPrinter.printA4(printService.get(), PDDocument.load(new File(filename)));
            } catch (NoSuchElementException e) {
                System.err.println("Could not locate printer " + printerName);
            } catch (PrinterException e) {
                System.err.println("Could not print file because some error occurred during the print job or a compatibility error with the service");
            } catch (IOException e) {
                System.err.println("Could not find file to print");
            }
        }
    
        public void printA4(PrintService printer, PDDocument documentToPrint) throws PrinterException {
    
            PrinterJob job = PrinterJob.getPrinterJob();
    
            job.setPageable(new PDFPageable(documentToPrint));
            job.setPrintable(new PDFPrintable(documentToPrint, Scaling.SCALE_TO_FIT));
    
    
            job.setPrintService(printer);
            job.print(getA4Attributes());
        }
    
        public Optional<PrintService> getMatchingPrintService(String printerName) {
            for (PrintService i : PrintServiceLookup.lookupPrintServices(null, getA4Attributes())) {
                if (i.getName().equals(printerName)) {
                    return Optional.of(i);
                }
            }
    
            return Optional.empty();
        }
    
        private PrintRequestAttributeSet getA4Attributes() {
            PrintRequestAttributeSet attr_set = new HashPrintRequestAttributeSet();
            attr_set.add(MediaSizeName.ISO_A4);
            return attr_set;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-08
      相关资源
      最近更新 更多