Diminuir margens impressão

0 respostas
antoniosales

ola a todos,

Tenho que diminuir a margem esqueda na impressão que esta muito grande, alguem poderia me ajudar a fazer isso isso usando o código abaixo

segue o código:

package acesso;


import java.awt.*;
import java.awt.print.*;
import java.awt.font.TextLayout;
import java.io.*;
import java.net.*;
import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.Level;
import java.util.logging.Logger;

class Acesso implements Printable {

    //cria as variaveis de fontes
   public static final int DEFAULT_FONT_SIZE = 12;
   public static final String DEFAULT_FONT_NAME = "Serif";

   //impressora
   private PrinterJob job;
   private String typeName;
   private int typeSize;
   private Font typeFont;
   private Font typeFontBold;
   private String [] header;
   private String [] body;

     public Acesso() {
       this(DEFAULT_FONT_NAME, DEFAULT_FONT_SIZE);
   }

   public Acesso(String name, int size)
   {
       if (size < 3 || size > 127) {
           throw new IllegalArgumentException("Type size out of range");
       }
       typeName = name;
       typeSize = size;
       typeFont = new Font(typeName, Font.PLAIN, typeSize);
       typeFontBold = new Font(typeName, Font.BOLD, typeSize);
       job = null;
   }

        public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException
        {
           if (pageIndex != 0) {
           return NO_SUCH_PAGE;
       }
       FontMetrics fm;
       graphics.setFont(typeFont);
       graphics.setColor(Color.black);

       fm = graphics.getFontMetrics();

       int i;
       double x, y;
       x = pageFormat.getImageableX();
       y = pageFormat.getImageableY() + fm.getMaxAscent();
       
       // Cria cabeçalho
       if (header != null) {
           graphics.setFont(typeFontBold);
           for(i = 0; i < header.length; i++) {

               graphics.drawString(header[i],(int)x, (int)y);
               
               y += fm.getHeight();
           }
       }
        
       // parte da impressão
       if (body != null) {
           graphics.setFont(typeFont);

           for(i = 0; i < body.length; i++) {
               graphics.drawString(body[i],(int)x,(int)y);
               y += fm.getHeight();
           }
       }

       return PAGE_EXISTS;
   }

   protected void init() {
       job = PrinterJob.getPrinterJob();
   }

      public int getCharsPerLine() {
       if (job == null) {
           init();
       }
       PageFormat pf;
       pf = job.defaultPage();
       double width = pf.getImageableWidth();
       double ptsize = typeFont.getSize();
       double ptwid = ptsize * 3 / 4;
       double cnt = (width / ptwid);
       return (int)(Math.round(cnt));
   }
  public void Apaga() throws IOException{
        URL url = new URL("http://192.168.0.1/academico/desenvolve/fotos/delete_dados.php");
        URLConnection amp_apaga = url.openConnection();
        InputStream stream = amp_apaga.getInputStream();
        amp_apaga.connect();
  }

        public boolean doPrint(String [] header, String [] body, boolean interactive)
       throws PrinterException
   {
       if (job == null) {
           init();
       }
       if (interactive) try {
           if (job.printDialog()) {

           } else {
               return false;
           }
       } catch (Exception pe) {
           System.err.println("Não abriu a a janela de dialogo da impressora");
       }

       job.setPrintable(this);
       this.header = header;
       this.body = body;
       job.print();
       job = null;
       return true;
   }

public static final long TEMPO = (100 * 60);
    public static void main(String[] args) throws IOException {
       final java.util.List lines;
       final Acesso tp = new Acesso();
       final Acesso ap = new Acesso();
       lines = new java.util.ArrayList();
       Timer timer = null;
         if (timer == null) {
             timer = new Timer();
             TimerTask tarefa = new TimerTask() {
             public void run() {

try{
    URL amplus = new URL("http://192.168.0.1/academico/desenvolve/fotos/dados.txt");
    URLConnection amp = amplus.openConnection();
    amp.setConnectTimeout(7000);
    amp.connect();
    BufferedReader in = new BufferedReader(new InputStreamReader(amp.getInputStream()));

           String line;
           for(line = in.readLine(); line != null;
               line = in.readLine())
               {
                   lines.add(line);
               }
           in.close();

           String [] headers = new String[1];
           headers[0] = "Impressão Biblioteca";
           String [] body = new String[lines.size()];
           for(int ix = 0; ix < lines.size(); ix++) {
               body[ix] = (String)(lines.get(ix));
           }

           try {
           
           if(lines.isEmpty()){

           }else{
           boolean didit = tp.doPrint(headers, body, false);
           lines.clear();
           ap.Apaga();
            }
           } catch (PrinterException ex) {
                Logger.getLogger(Acesso.class.getName()).log(Level.SEVERE, null, ex);
           }

            } catch (MalformedURLException me) {
           // System.out.println("MalformedURLException: " + me);
            } catch (IOException ioe) {

            //System.out.println("IOException:1" + ioe);
            }
            }  //fechou run

             };  // fechou TimerTask
             timer.scheduleAtFixedRate(tarefa, TEMPO, TEMPO);

            }
         }
    }

Agradeço qualquer ajuda.

Antonio

Criado 2 de setembro de 2010
Respostas 0
Participantes 1