You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@pdfbox.apache.org by Jonathan Ominsky <jo...@opentext.com> on 2017/05/04 17:04:43 UTC

Issue printing with shrink to fit

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
jominsky@opentext.com<ma...@opentext.com>

[cid:image001.png@01D2C4D3.1D1848C0]<http://www.opentext.com/>


Re: Issue printing with shrink to fit

Posted by Jon Ominsky <jo...@gmail.com>.
I tried with 2.0.5. It did not make any difference.

Jon

On 2017-05-04 13:07 (-0400), Tilman Hausherr <TH...@t-online.de> wrote: 
> Am 04.05.2017 um 19:04 schrieb Jonathan Ominsky:
> > I’m using pdfbox 2.0.1.
> 
> Please retry with 2.0.5 !
> 
> Tilman
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
> For additional commands, e-mail: users-help@pdfbox.apache.org
> 
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org


Re: Issue printing with shrink to fit

Posted by Tilman Hausherr <TH...@t-online.de>.
Am 04.05.2017 um 19:04 schrieb Jonathan Ominsky:
> I’m using pdfbox 2.0.1.

Please retry with 2.0.5 !

Tilman


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org


Re: Issue printing with shrink to fit

Posted by Tilman Hausherr <TH...@t-online.de>.
Could this help? This is example code from Printing.java:



     /**
      * Prints using a custom page size and custom margins.
      */
     private static void printWithPaper(PDDocument document)
             throws IOException, PrinterException
     {
         PrinterJob job = PrinterJob.getPrinterJob();
         job.setPageable(new PDFPageable(document));

         // define custom paper
         Paper paper = new Paper();
         paper.setSize(306, 396); // 1/72 inch
         paper.setImageableArea(0, 0, paper.getWidth(), 
paper.getHeight()); // no margins

         // custom page format
         PageFormat pageFormat = new PageFormat();
         pageFormat.setPaper(paper);

         // override the page format
         Book book = new Book();
         // append all pages
         book.append(new PDFPrintable(document), pageFormat, 
document.getNumberOfPages());
         job.setPageable(book);

         job.print();
     }

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org