PageFormat () { PrintSettings printSettings = PrintSettings.getInstance(); PageFormat pageFormat = new PageFormat(); Paper paper = new Paper(); String paperSize = printSettings.PAPER_SIZE; double paperWidth = PageSizes.getWidth(paperSize) * 72; double paperHeight = PageSizes.getHeight(paperSize) * 72; double leftMargin = printSettings.LEFT_MARGIN * 72; double rightMargin = printSettings.RIGHT_MARGIN * 72; double topMargin = printSettings.TOP_MARGIN * 72; double bottomMargin = printSettings.BOTTOM_MARGIN * 72; paper.setSize(paperWidth, paperHeight); if (printSettings.PORTRAIT_LAYOUT) { pageFormat.setOrientation(PageFormat.PORTRAIT); paperWidth -= leftMargin + rightMargin; paperHeight -= topMargin + bottomMargin; paper.setImageableArea(leftMargin, topMargin, paperWidth, paperHeight); } else { pageFormat.setOrientation(PageFormat.LANDSCAPE); paperWidth -= topMargin + bottomMargin; paperHeight -= leftMargin + rightMargin; paper.setImageableArea(rightMargin, topMargin, paperWidth, paperHeight); } pageFormat.setPaper(paper); return pageFormat; }