2013-02-08 1 views
1

Je suis donc en train d'écrire un programme java pour que mon père puisse imprimer des reçus et d'autres choses. Mon intention initiale était d'imprimer à son Imprimante de reçu quelques informations concernant chaque transaction qu'il a faite. Cependant, l'imprimante a du mal à imprimer ce que j'envoie sans l'écrêter à un point extrême. Ma prochaine idée, qui a très bien fonctionné, était de sauvegarder la "réception" dans un fichier XPS, puis d'imprimer le XPS, ce qui ne le couperait pas et rendrait tout agréable. Maintenant, je peux imprimer dans un fichier XPS en utilisant XPS Document Writer PrintService de Microsoft. Le problème est, quand je le fais, il apparaît toujours une boîte demandant le nom du fichier et l'emplacement pour l'enregistrer dans.JAVA - Impression XPS sans nom de fichier/emplacement Pop-up

Y a-t-il une façon de le régler de manière à ne pas afficher ce pop-up?

Code actuel:

PrinterJob job = PrinterJob.getPrinterJob(); 
job.setPrintable(this); 
try { 
    job.print(); 
} catch (PrinterException ex) { 
    // The job did not successfully complete 
} 

-

@Override 
public int print(Graphics g, PageFormat pf, int page) throws PrinterException { 
    String temp; 

    if (page > 0) { /* We have only one page, and 'page' is zero-based */ 
     return NO_SUCH_PAGE; 
    } 

    Graphics2D g2d = (Graphics2D)g; 
    g2d.translate(pf.getImageableX(), pf.getImageableY()); 
    int lineSize=20; 

    Font testFont=new Font("Lucida Console", 0, 20); 
    g.setFont(testFont); 

    g.drawString("  Fatura/Recibo nº"+nmrRec+"  ", 5, 20); 
    return PAGE_EXISTS; 
} 

Répondre

2

Vous devriez être en mesure de le faire en définissant l'Destination attribut:

static void print(Printable printable, PrintService service) 
throws PrintException, 
     IOException { 

    Path outputFile = Files.createTempFile(
     Paths.get(System.getProperty("user.home")), null, ".xps"); 

    Doc doc = new SimpleDoc(printable, 
     DocFlavor.SERVICE_FORMATTED.PRINTABLE, null); 

    PrintRequestAttributeSet attributes = 
     new HashPrintRequestAttributeSet(); 
    attributes.add(new Destination(outputFile.toUri())); 

    DocPrintJob job = service.createPrintJob(); 
    job.print(doc, attributes); 
} 
+0

Ce code est juste en train de lire « fichier » et de l'écriture dans le nouveau fichier XPS à C: \ Users \ , non? Comment puis-je dessiner des choses? Auparavant, je remplaçais la méthode d'impression de PrinterJob (depuis son résumé) et j'ai obtenu un Graphics2D à partir de là. Puis-je construire "doc" avec cet objet Graphics à la place de l'inputtream? Si oui, comment l'initialiser? – BlueMoon93

+0

Au lieu d'un InputStream, implémentez 'java.awt.print.Printable' et passez cet objet d'implémentation au constructeur SimpleDoc. J'ai mis à jour le code en conséquence. – VGR

+0

A travaillé, merci beaucoup =) – BlueMoon93

1

donc j'ai suivi les conseils de VGR et je me suis ça marche. Ce fut mon code final, au cas où quelqu'un se jette dans le même problème:

Date data = new Date();           //Data 
DateFormat dataform = new SimpleDateFormat("dd-MM-yyyy");  //Data 

PrintService service=getPrinterService("Microsoft XPS Document Writer"); 
if(service!=null){ 
    try{ 
     File outputFile = new File(dataform.format(data)+"-Recibo"+nmrRec+".xps"); 

     Doc doc = new SimpleDoc(new myReceipt(), DocFlavor.SERVICE_FORMATTED.PRINTABLE, null); 

     PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet(); 
     attributes.add(new Destination(outputFile.toURI())); 

     DocPrintJob job = service.createPrintJob(); 
     job.print(doc, attributes); 
    } catch(Exception e){ 
     System.out.println("kaboom"+e); 
    } 
} 
else{ 
    System.out.println("XPS Printer not found"); 
} 

Et il y a ma classe de réception:

class myReceipt implements Printable{ 

    @Override 
    public int print(Graphics g, PageFormat pf, int page) throws PrinterException { 
     String temp; 

     if (page > 0) { /* We have only one page, and 'page' is zero-based */ 
      return NO_SUCH_PAGE; 
     } 

     /* User (0,0) is typically outside the imageable area, so we must 
     * translate by the X and Y values in the PageFormat to avoid clipping 
     */ 
     Graphics2D g2d = (Graphics2D)g; 
     g2d.translate(pf.getImageableX(), pf.getImageableY()); 
     int lineSize=20; 

     Font testFont=new Font("Lucida Console", Font.BOLD, 20); 
     // font name, style (0 for Plain), font size 
     g.setFont(testFont); 
     int line=20; 

     g.drawString("  Fatura/Recibo nº"+nmrRec+"  ", 5, line); 
     return PAGE_EXISTS; 
    } 
} 
Questions connexes