Forum Stats

  • 3,838,481 Users
  • 2,262,371 Discussions
  • 7,900,668 Comments

Discussions

Print PDF with mixed orientation of pages

801396
801396 Member Posts: 3
edited Feb 2, 2012 5:55PM in Java Desktop Applications
I have a PDF that I want to print from Java, but it has mixed orientation of pages. For example, the first page is portrait, but the second page is landscape. When printing the PDF from Acrobat Reader, each page is printed correctly according to it's page orientation. I believe this is due to the orientation print option of auto, but that doesn't appear to exist with the Java API.

Here is test code I use to print a PDF. I know that OrientationRequested is used to specify the printing orientation, but I need such an attribute to be page-auto. Any suggestions?
public class PDFPrint {

   public PDFPrint() {
   }

   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);
                   }

                   // need to specify orientation
                   printOrientationAttributes(services[0]);

               } 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) {
                   System.out.print("Name = " + media[iMedia].getName());
                   System.out.print(", Value = " + media[iMedia].toString());
                   System.out.print(", Category = " + media[iMedia].getCategory().getSimpleName());
                   System.out.print(", Class = " + media[iMedia].getClass().getSimpleName());
                   System.out.println(", MediaTray=" + media[iMedia].toString());
                   if (media[iMedia].toString().trim().equalsIgnoreCase(name)) {
                       System.out.println("     ******* Found ******* ");
                       return media[iMedia];
                   }
               }
           }
       }
       return null;
   }
   private void printOrientationAttributes(PrintService ps){
       Object[] attributes = ps.getSupportedAttributeValues(OrientationRequested.class, null, null);

       if (attributes != null && attributes.getClass().isArray()) {
           OrientationRequested[] media = (OrientationRequested[]) attributes;
           System.out.println();
           System.out.println("\t****** OrientationRequested ******");
           for (int iMedia = 0; iMedia < media.length; iMedia++) {
               System.out.println("\t\"" + media[iMedia].toString() + "\"");
           }
       }   }
}
Tagged:

Answers

  • morgalr
    morgalr Member Posts: 457
    I've not ran accross a auto-orientation in Java, but what I've done is print each page--real pain, or in the case of images, I was able to just change the orientation of the image (in a temp file) before printing.
This discussion has been closed.