Imprimindo no Java

0 respostas
palhadecoco

Como faço para essa class imprimi no canto “esquendo”? e como fazer quebra de linha?

Tem como eu por uma imagem no topo da folha?

Queria que a impressão fosse assim:

LOGO

descrição1
descrição1
descrição1

import javax.print.*;
import javax.print.event.*;
import javax.print.attribute.*;
import java.awt.print.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class NewClass {

   static class MyComponent extends JPanel
       implements Printable {

     Font theFont = new Font("Serif", Font.ITALIC, 48);

     public void paint(Graphics g) {
       super.paint(g);
       String msg = "Hello, Printer";

       g.setFont(theFont);
       FontMetrics fm = g.getFontMetrics();

       // Center line
      
       int stringWidth = fm.stringWidth(msg);
       int x = 4;

       int height = getHeight();
       int stringHeight = fm.getHeight();
       int ascent  = fm.getAscent();
       int y = (height - stringHeight)/4 + ascent;

       g.drawString(msg, x, y);
    
       
       g.drawRect(x, y-ascent, stringWidth, stringHeight);

     }

     public int print(Graphics g, PageFormat pageFormat, int pageIndex) {
       int x = (int)pageFormat.getImageableX();
       int y = (int)pageFormat.getImageableY();
       g.translate(x, y);
       if (pageIndex == 0) {
         paint(g);
         return Printable.PAGE_EXISTS;
       } else {
         return Printable.NO_SUCH_PAGE;
       }
     }
   }

   public static void main(String args[]) throws Exception {

     final JFrame frame = new JFrame("Printing Graphics");
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

     Container contentPane = frame.getContentPane();

     final Component printIt = new MyComponent();
     contentPane.add(printIt, BorderLayout.CENTER);

     JButton button = new JButton("Print");
     contentPane.add(button, BorderLayout.SOUTH);

     ActionListener listener = new ActionListener() {
       public void actionPerformed(ActionEvent e) {
         DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
         PrintService printService =
           PrintServiceLookup.lookupDefaultPrintService();
         DocPrintJob job = printService.createPrintJob();
         PrintJobListener pjlistener = new PrintJobAdapter() {
           public void printDataTransferCompleted(PrintJobEvent e) {
             System.out.println("Good-bye");
             System.exit(0);
           }
         };
         job.addPrintJobListener(pjlistener);

         PrintRequestAttributeSet pras =
           new HashPrintRequestAttributeSet();

         DocAttributeSet das = new HashDocAttributeSet();
         Doc doc = new SimpleDoc(printIt, flavor, das);
         try {
           job.print(doc, pras);
         } catch (PrintException pe) {
           pe.printStackTrace();
         }
       }
     };
     button.addActionListener(listener);

     frame.setSize(350, 250);
     frame.show();
   }
}
Criado 8 de outubro de 2010
Respostas 0
Participantes 1