I have a PDF in A4 page format that I'm trying to print to a printer that only 
has Letter size paper. In working through other issues, I was using PDFPageable 
specifying a dpi. However, I noticed that class uses the original size of the 
document and I need to be able to shrink the document to fit the printer if 
necessary. I wasn't sure how to combine both the PDFPageable and PDFPrintable, 
so I basically copied the PDFPageable class and created a 
PDFPageableWithScaling class. In the constructor, I pass in 
Scaling.SHRINK_TO_FIT and then pass this into the constructor of PDFPrintable. 
However, the document still prints A4 on my Letter paper. I'm relatively new to 
all of this, so I'm sure I'm missing something. Any help is appreciated.

Calling code:

    String sourcePDF = "C:\\temp\\CPTest\\CMC-MBOM-001_PATTARO Roberto.pdf";
    PrinterJob job = PrinterJob.getPrinterJob();
    try (PDDocument document = PDDocument.load(new File(sourcePDF))) {
      job.setPageable(new PDFPageableWithScaling(document, Orientation.AUTO, 
false, 600, Scaling.SHRINK_TO_FIT));
      job.print();
    } catch (IOException | PrinterException e) {
      throw new RuntumeException("Unable to print file " + sourcePDF + ".", e);
    }


The only code in PDFPageable that I changed was this:

  public Printable getPrintable(int i) {
    if (i >= getNumberOfPages()) {
      throw new IndexOutOfBoundsException(i + " >= " + getNumberOfPages());
    }
    return new PDFPrintable(document, scaling, showPageBorder, dpi);
  }

Original PDFPageable code:

  public Printable getPrintable(int i) {
    if (i >= getNumberOfPages()) {
      throw new IndexOutOfBoundsException(i + " >= " + getNumberOfPages());
    }
    return new PDFPrintable(document, Scaling.ACTUAL_SIZE, showPageBorder, dpi);
  }

I'm using pdfbox 2.0.1.

Jon

Jon Ominsky
Consultant Software Engineer, Life Sciences
OpenText | Enterprise Content Division
Mobile +1 610.322.0558
[email protected]<mailto:[email protected]>

[cid:[email protected]]<http://www.opentext.com/>

Reply via email to