简体   繁体   中英

Java - set printing page format

I try to print a pdf using DocPrintJob but it won't fit to page and so, I have to manually print the page. My pdf is already in Letter format.

Here's what I've done so far:

            PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();

            DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
            PrintService printService[] = PrintServiceLookup.lookupPrintServices(flavor, pras);
            PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
            PrintService service = ServiceUI.printDialog(GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration(), 200, 200,
                                  printService, defaultService, flavor, pras);

            if (service != null) {
                pras.add(MediaSizeName.NA_LETTER);
                PageFormat page = new PageFormat();
/*              MediaSizeName media = MediaSize.findMedia(
                        (float) (page.getWidth() / 72.0),
                        (float) (page.getHeight() / 72.0), 
                        Size2DSyntax.INCH);
    */          
        //      pras.add(media);
                DocPrintJob job = service.createPrintJob();
                FileInputStream fis=null;
                File file = new File("test.pdf");
                try {
                    fis = new FileInputStream("test.pdf");

                } catch (FileNotFoundException e2) {
                    // TODO Auto-generated catch block
                    e2.printStackTrace();
                }
                DocAttributeSet das = new HashDocAttributeSet();

               Doc document = null;
                document = new SimpleDoc(fis, flavor, null);


                try {
                    job.print(document, pras);
                } catch (PrintException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }


            }

As you can see, I've tried to use MediaSizeName.NA_LETTER but it just doesn't change anything. Is there a way, also, to let the user select the Media size, there's no option in the print dialog? Thanks in advance!

似乎打印机正在根据您设置的首选项来获取默认纸张,因此,如果您将首选项设置为使用Letter尺寸,它将要求使用Letter尺寸的页面,默认情况下它将在A4尺寸的纸张上打印。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM