Printing with Attributes(Tray Control, Duplex, etc…) using javax.print library

后端 未结 5 817
一个人的身影
一个人的身影 2020-12-02 20:36

I\'ve been trying for some time to determine a way to use the standard Java Print library to print files - specifically, PDF documents - with certai

相关标签:
5条回答
  • 2020-12-02 21:13

    The problem is that the the Java print API is a bridge between worlds. Printer manufacturers don't release drivers for the JVM. They release drivers for Windows, Macintosh, and maybe someone has a a driver for a given printer that works on one or more *nix platforms.

    Along you come with some Java code running inside a JVM on some host system. When you start querying the printer features, you aren't talking to the printers -- you are talking to a bridge class in java.awt.print that hook into the JVM, which hooks to the host operating system, which hooks into whatever particular driver was installed for a given printer. So there are several places where this can fall apart... The particular JVM you are on may or may not fully implement the API for querying printer features, let alone passing those parameters along for a given job.

    A few suggestions:

    1. look into the javax.print classes as an alternative to java.awt.print -- I've had more luck printing from there.
    2. try using alternative print drivers for your printers -- you can define multiple named connections to a given printer, each with a different driver. If you've got a manufacturer provided driver, try a more generic driver, if you've got a generic driver, try to install a more specific one.
    3. run your code under alternate JVM implementations for your platform
    0 讨论(0)
  • 2020-12-02 21:22

    Here's what it looks like in javafx Tray's may vary and it will also print out all trays that are available just change the tray name

    private void printImage(Node node) {
        PrinterJob job = PrinterJob.createPrinterJob();
        if (job != null) {
            JobSettings js = job.getJobSettings();
            PaperSource papersource = js.getPaperSource();
            System.out.println("PaperSource=" + papersource);
            PrinterAttributes pa = printer.getPrinterAttributes();
            Set<PaperSource> s = pa.getSupportedPaperSources();
            System.out.println("# of papersources=" + s.size());
            if (s != null) {
                for (PaperSource newPaperSource : s) {
                    System.out.println("newpapersource= " + newPaperSource);
                    //Here is where you would put the tray name that is appropriate
                    //in the contains section
                    if(newPaperSource.toString().contains("Tray 2"))
                        js.setPaperSource(newPaperSource);
                }
            }
            job.getJobSettings().setJobName("Whatever");
            ObjectProperty<PaperSource> sources = job.getJobSettings().paperSourceProperty();
            System.out.println(sources.toString());
            boolean success = job.printPage(node);
            if (success) {
                System.out.println("PRINTING FINISHED");
                job.endJob();
                //Stage mainStage = (Stage) root.getScene().getWindow();
                //mainStage.close();
            }
        }
    }
    

    Here's My output:

    PaperSource=Paper source : Automatic
    # of papersources=6
    newpapersource= Paper source :
    newpapersource= Paper source :  Manual Feed in Tray 1
    newpapersource= Paper source :  Printer auto select
    newpapersource= Paper source :  Tray 1
    newpapersource= Paper source :  Tray 2
    newpapersource= Paper source : Form-Source
    ObjectProperty [bean:  Collation = UNCOLLATED
     Copies = 1
     Sides = ONE_SIDED
     JobName = Whatever
     Page ranges = null
     Print color = COLOR
     Print quality = NORMAL
     Print resolution = Feed res=600dpi. Cross Feed res=600dpi.
     Paper source = Paper source :  Tray 2
     Page layout = Paper=Paper: Letter size=8.5x11.0 INCH Orient=PORTRAIT leftMargin=54.0 rightMargin=54.0 topMargin=54.0 bottomMargin=54.0, name: paperSource, value: Paper source :  Tray 2]
    PRINTING FINISHED
    
    0 讨论(0)
  • 2020-12-02 21:24

    I've found the trick for the printer trays is to iterate over the Media.class using getSupportedAttributeValues(...), match the human-readable name, and select that particular value. Tested on Windows, MacOS with several tray configurations.

    String tray = "1";
    
    // Handle human-readable names, see PRINTER_TRAY_ALIASES usage below for context.  Adjust as needed.
    List<String> PRINTER_TRAY_ALIASES = Arrays.asList("", "Tray ", "Paper Cassette ");
    
    // Get default printer
    PrintService printService = PrintServiceLookup.lookupDefaultPrintService();
    
    // Attributes to be provided at print time
    PrintRequestAttributeSet pset = new HashPrintRequestAttributeSet();
    
    Media[] supported = printService.getSupportedAttributeValues(Media.class, null, null);
    for(Media m : supported) {           
        for(String pta : PRINTER_TRAY_ALIASES) {
            // Matches "1", "Tray 1", or "Paper Cassette 1"
            if (m.toString().trim().equalsIgnoreCase(pta + tray)) {
                attributes.add(m);
                break;
            }
        }
    }
    
    // Print, etc
    // printJob.print(pdfDoc, pset);
    
    0 讨论(0)
  • 2020-12-02 21:25

    We had similar requirement to print PDF's and wanted to send some pages to Specific tray and also wanted the document to be stapled. We used Java code + ghost script combination First convert PDF to ghost script and then add PJL (Print job language) commands to ghost script file to select trays and staple the documents. Then send that edited ghost script file to printer.

    Here is complete example written in Java

    http://reddymails.blogspot.com/2014/07/how-to-print-documents-using-java-how.html

    -Ram

    0 讨论(0)
  • 2020-12-02 21:35

    So, we inevitably found a way to print to different trays and with different settings, but not directly. We found it impossible to send attributes via the printJob.print method, and that much hasn't changed. However, we were able to set the name of the print job, then intercept the print job with a low-level Perl script, parse the name, and set the tray and duplex settings there. It's an extreme hack, but it works. It still remains true that Java Printer Attributes do not work, and you will need to find another way if you want to set them.

    0 讨论(0)
提交回复
热议问题