# Seitenränder mmit awt.PrintJob



## pvflo (4. Feb 2011)

Hallo Allerseits,

Ich mühe mich ab mit Drucken aus Java, daher eine Frage:

Ich habe eine Applikation, welche auf einen Quittungsdrucker druckt. Funktionierte soweit einwandfrei. Mit der Einschränkung, dass immer der Dialog eingeblendet wurde, um den Drucker zu selektionieren.

Nachforschungen im WWW haben mir dann geholfen und ich kann nun den Drucker angeben:
        JobAttributes ja=new JobAttributes();
        ja.setDestination(JobAttributes.DestinationType.PRINTER);
        ja.setPrinter("DRUCKERNAME"); 

aber nun kann ich die Seitenränder nicht mehr anpassen:

Weiterer Code:
        PageAttributes pa=new PageAttributes();
        pa.setOrigin(PageAttributes.OriginType.PRINTABLE);

        Toolkit toolkit = Toolkit.getDefaultToolkit();
        PrintJob pjob = toolkit.getPrintJob(null, "Application Printjob", ja, pa);

        Dimension dim = pjob.getPageDimension();


        g = pjob.getGraphics();
        // Druckausgabe auf graphics

        g.dispose();
        pjob.end();

Kann mir jemand helfen? Wie kann ich die Seitenränder dieses Druckjobs anpassen?
Oder allgemein ausgedrückt: Wie kann ich drucken - ohne Drucker-Dialog - aber trotzdem die Seitenränder bestimmen...

Vielen Dank


----------



## André Uhres (4. Feb 2011)

Hallo pvflo,

versuch's mal hiermit:


```
import java.awt.*;
import java.awt.print.*;
import java.util.logging.*;
import javax.print.*;
import javax.print.attribute.*;
import javax.print.attribute.standard.*;

public class PrintDemo {

    public PrintDemo() {
        Printable printable = new MyPrintable();
        boolean portrait = true;
        Insets insets = new Insets(10, 10, 10, 10);
        boolean shouldDisplayDialog = false;
        String printerName = "pdfcreator";
        PrintUtility.print(printable, portrait, insets, shouldDisplayDialog, printerName);
    }

    public static void main(final String... args) {
        PrintDemo printDemo = new PrintDemo();
    }
}

class MyPrintable implements Printable {

    private final Rectangle available = new Rectangle(0, 0, 0, 0);

    private void myPage(Graphics2D g2d) {
        g2d.setColor(Color.LIGHT_GRAY);
        g2d.fillRect(0, 0, available.width, available.height);
        g2d.setColor(Color.BLACK);
        g2d.drawRect(0, 0, available.width, available.height);
        g2d.drawString("Test page Test page Test page Test page Test page Test page Test page Test page", 30, 30);
    }

    @Override
    public int print(final Graphics graphics, final PageFormat pageFormat, final int pageIndex)
            throws PrinterException {
        final int imgWidth = (int) pageFormat.getImageableWidth();
        final int imgHeight = (int) pageFormat.getImageableHeight();
        if (imgWidth <= 0) {
            throw new PrinterException("Width of printable area is too small.");
        }
        if (imgHeight <= 0) {
            throw new PrinterException("Height of printable area is too small.");
        }
        if (pageIndex != 0) {// if we are finished with all pages
            return NO_SUCH_PAGE;
        }
        // translate into the coordinate system of the pageFormat
        Graphics2D g2d = (Graphics2D) graphics;
        g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
        // constrain the output to the available space
        available.width = imgWidth;
        available.height = imgHeight;
        g2d.clip(available);
        //draw something:
        myPage(g2d);
        return PAGE_EXISTS;
    }
}

class PrintUtility {

    static private PrintRequestAttributeSet attr;

    static public void print(final Printable printable, final boolean portrait,
            final Insets insets, boolean shouldDisplayDialog, String printerName) {
        PrinterJob pjob = PrinterJob.getPrinterJob();
        pjob.setPrintable(printable);
        // create an attribute set to store attributes from the print dialog
        if (attr == null) {
            attr = new HashPrintRequestAttributeSet();
            float leftMargin = insets.left;
            float rightMargin = insets.right;
            float topMargin = insets.top;
            float bottomMargin = insets.bottom;
            if (portrait) {
                attr.add(OrientationRequested.PORTRAIT);
            } else {
                attr.add(OrientationRequested.LANDSCAPE);
                leftMargin = insets.top;
                rightMargin = insets.bottom;
                topMargin = insets.right;
                bottomMargin = insets.left;
            }
            attr.add(MediaSizeName.ISO_A4);
            MediaSize mediaSize = MediaSize.ISO.A4;
            float mediaWidth = mediaSize.getX(Size2DSyntax.MM);
            float mediaHeight = mediaSize.getY(Size2DSyntax.MM);
            attr.add(new MediaPrintableArea(
                    leftMargin, topMargin,
                    (mediaWidth - leftMargin - rightMargin),
                    (mediaHeight - topMargin - bottomMargin), Size2DSyntax.MM));

        }
        PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);
        PrintService service = null;
        if (printerName != null) {
            for (PrintService printService : services) {
                String name = printService.getName();
                if (name.toLowerCase().contains(printerName.toLowerCase())) {
                    service = printService;
                    break;
                }
            }
        }
        if (shouldDisplayDialog) {
            if (pjob.printDialog(attr)) {
                try {
                    pjob.print(attr);
                } catch (PrinterException ex) {
                    Logger.getLogger(ContainerPrintable.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        } else {
            try {
                if (service != null) {
                    pjob.setPrintService(service);
                }
                pjob.print(attr);
            } catch (PrinterException ex) {
                Logger.getLogger(ContainerPrintable.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}
```

Gruß,
André


----------



## pvflo (4. Feb 2011)

super, André!
Vielen Dank!!!
Grüsse


----------

