I still can't figure this out.  Does anyone have an example on how to
properly print a PDF with mixed page orientations?

On Tue, Jan 31, 2012 at 4:33 PM, John Manko <[email protected]> wrote:

> I have a PDF file where the first page is portrait and the second page is
> landscape.  When I open the PDF in Acrobat Reader and print, the
> orientation prints correctly for each page.  If I print using PDFBox, the
> orientation is portrait for both pages, which is incorrect.  Is it possible
> to print a mixed-orientation document with PDFBox?
>
>
> Here is the code I use to print:
>
>
> public class PDFPrint {
>
>     public PDFPrint() {
>     }
>
>     public static void main(String[] args) {
>         String file = System.getProperty("myapp.pdf.file");
>         String printerName = System.getProperty("myapp.pdf.printer");
>         String tray = System.getProperty("myapp.pdf.printerTray");
>
>         PDFPrint pdfPrint = new PDFPrint();
>         pdfPrint.print(file, printerName, Sides.ONE_SIDED, tray, "test
> PDF");
>     }
>
>     public void print(String file, String printerName, Sides sides, String
> tray, String jobName) {
>         PDDocument document = null;
>         try {
>             document = PDDocument.load(file);
>             PrintRequestAttributeSet pras = new
> HashPrintRequestAttributeSet();
>             pras.add(sides);
>
>             PrinterJob printJob = PrinterJob.getPrinterJob();
>             printJob.setJobName(jobName);
>             printJob.print(pras);
>
>
>             // Getting the PrintServices by Printername
>             PrintService[] services =
> PrintServiceLookup.lookupPrintServices(null, new HashAttributeSet(new
> PrinterName(printerName, Locale.getDefault())));
>             if (services.length > 0) {
>                 try {
>                     printJob.setPrintService(services[0]);
>                     Attribute possibleTray = getMediaTray(services[0],
> tray);
>                     if (possibleTray != null) {
>                         pras.add(possibleTray);
>                     }
>                 } catch (PrinterException pe) {
>                 }
>
>             } else {
>                 System.out.println(printerName + " not found");
>             }
>             document.silentPrint(printJob);
>         } catch (PrinterException ex) {
>             Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null,
> ex);
>         } catch (IOException ex) {
>             Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null,
> ex);
>         }
>         if (document != null) {
>             try {
>                 document.close();
>             } catch (IOException ex) {
>                 Logger.getLogger(Main.class.getName()).log(Level.SEVERE,
> null, ex);
>             }
>         }
>     }
>
>     private Attribute getMediaTray(PrintService ps, String name) {
>         Object attributes = ps.getSupportedAttributeValues(Media.class,
> null, null);
>         if (attributes != null && attributes.getClass().isArray()) {
>             Media[] media = (Media[]) attributes;
>             for (int iMedia = 0; iMedia < media.length; iMedia++) {
>                 if (media[iMedia] instanceof MediaTray) {
>                     if
> (media[iMedia].toString().trim().equalsIgnoreCase(name)) {
>                         return media[iMedia];
>                     }
>                 }
>             }
>         }
>         return null;
>     }
> }
>
>
>

Reply via email to