Click here to Skip to main content
15,867,324 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,
I want to display the page settings dialog like that :

Page dialog snapshot

The values I want to display are :
size : A4
orientation : landscape
margins : left=15, right=15, top=15, bottom=15 (millimeters)

I use a HashPrintRequestAttributeSet instance to pass parameters to the 'getPageFormat' of 'PrinterJob', then I pass the calculated format to the pageDialog function, but the values displayed in the dialog are not correct, particulary the margins.

Could you correct the code below, to get exactly 15mm for each margin in the dialog ?

Thank you.
Chris

What I have tried:

Java
public void actionPerformed(ActionEvent arg0) {
    try {
        PrinterJob pj = PrinterJob.getPrinterJob() ;
        HashPrintRequestAttributeSet printRequestSet = initializePrintingAttributes() ;
        PageFormat pf = pj.getPageFormat(printRequestSet) ;
        pj.pageDialog(pf) ;
    } catch (Throwable e) {
        e.printStackTrace();
    }
}

private HashPrintRequestAttributeSet initializePrintingAttributes() {
    HashPrintRequestAttributeSet printRequestSet = new HashPrintRequestAttributeSet() ;

    printRequestSet.add(OrientationRequested.LANDSCAPE) ;

    float width = MediaSize.ISO.A4.getX(MediaPrintableArea.MM) ;
    float height = MediaSize.ISO.A4.getY(MediaPrintableArea.MM) ;
    printRequestSet.add(new MediaPrintableArea(15f, 15f, (float)width-15*2, (float)height-15*2, MediaPrintableArea.MM)) ;

    printRequestSet.add(MediaSizeName.ISO_A4) ;
    return printRequestSet ;
}
Posted
Comments
Richard MacCutchan 4-Aug-18 12:57pm    
"but the values displayed in the dialog are not correct"
Well we cannot guess what those values are. If you wish for someone to help you then please provide full details of your problem.
chris_brabant 6-Aug-18 4:14am    
Hello,
the values displayed for the margins are :
left = 25,35 mm
right = 25.4 mm
top = 25.4 mm
bottom = 25,51 mm

As you can see it does not correspond to the values I have asked in the "MediaPrintableArea" attribute.
I'm not sure that the attribute I must use to manage the margins is "MediaPrintableArea", but I dont see any other corresponding to the margins.

Thank you

Chris

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900