Boas
Bom é o seguinte preciso de com o java fazer uma consulta à bases de dados e passar essa consulta para o pdf, até aqui já está resolvi o problema facilmente com o iText. Agora o problema é consegui imprimir o pdf, para isso estou a tentar usar a biblioteca javax.print, já consegui fazer métodos para procurar a impressora com este codigo
Código
PrintService printer = PrintServiceLookup.lookupDefaultPrintService();
Já fiz print do printer.getName e é a impressora correcta até tudo bem, depois de procurar e procurar encontrei algumas soluções nomeadamente isto
import javax.print.*;
import java.io.*;
import javax.print.event.*;
import javax.print.attribute.*;
import javax.print.attribute.standard.*;
//import javax.swing.*;
public class printer {
//JFrame frame;
//JButton btn;
public static void main(String args[]){
new printer("C:\\a.jpg","JPG");
}
private boolean PrintJobDone = false;
printer(String FileToPrint, String pMode) {
try {
InputStream is = new BufferedInputStream(new FileInputStream(FileToPrint));
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
if (pMode != null && pMode.equalsIgnoreCase("TXT")){
}
else if (pMode != null && pMode.equalsIgnoreCase("PS")){
flavor = DocFlavor.INPUT_STREAM.POSTSCRIPT;
}
else if (pMode != null && pMode.equalsIgnoreCase("PDF")){
flavor = DocFlavor.INPUT_STREAM.PDF;
}
else if (pMode != null && pMode.equalsIgnoreCase("JPG")){
flavor = DocFlavor.INPUT_STREAM.JPEG;
}
else if (pMode != null && pMode.equalsIgnoreCase("GIF")){
flavor = DocFlavor.INPUT_STREAM.GIF;
}
else if (pMode != null && pMode.equalsIgnoreCase("PNG")){
flavor = DocFlavor.INPUT_STREAM.PNG;
}
else if (pMode != null && pMode.equalsIgnoreCase("PCL")){
flavor = DocFlavor.INPUT_STREAM.PCL;
}
else if (pMode != null && pMode.equalsIgnoreCase("RAW")){
flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
}
PrintService service = PrintServiceLookup.lookupDefaultPrintService();
PrintService[] services = PrintServiceLookup.lookupPrintServices(flavor, null);
if (services == null || services.length < 1){
services = PrintServiceLookup.lookupPrintServices(null, null);
}
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
aset.add(new Copies(1));
aset.add(OrientationRequested.PORTRAIT);
aset.add(Sides.ONE_SIDED);
aset.add(MediaSizeName.ISO_A4);
//PrintService service = ServiceUI.printDialog( null,60, 60,services,(PrintService) dservice,(DocFlavor) flavor,aset);
if (service != null) {
final DocPrintJob job = service.createPrintJob();
Doc doc = new SimpleDoc(is, flavor, null);
PrintJobWatcher pjDone = new PrintJobWatcher(job);
try {
job.print(doc, (PrintRequestAttributeSet) aset);
}
catch (PrintException e) {
e.printStackTrace();
}
pjDone.waitForDone();
}
is.close();
}
catch (IOException e) {
e.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
}
finally {
try {
synchronized (printer.this) {
PrintJobDone = true;
printer.this.notify();
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
public synchronized void waitForDone() {
try {
while (!PrintJobDone) {
wait();
}
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
class PrintJobWatcher {
boolean done = false;
int lastEvent = 0;
PrintJobWatcher(DocPrintJob job) {
job.addPrintJobListener(new PrintJobAdapter() {
public void printJobRequiresAttention(PrintJobEvent pje) {
lastEvent = pje.getPrintEventType();
System.err.println("* Erro na Impressora:" + pje);
}
public void printDataTransferCompleted(PrintJobEvent pje) {
lastEvent = pje.getPrintEventType();
System.err.println("* Ficheiro enviado para a Impressora ");
}
public void printJobCanceled(PrintJobEvent pje) {
lastEvent = pje.getPrintEventType();
allDone();
}
public void printJobCompleted(PrintJobEvent pje) {
lastEvent = pje.getPrintEventType();
allDone();
}
public void printJobFailed(PrintJobEvent pje) {
lastEvent = pje.getPrintEventType();
System.err.println("* ERRO de impressão" + pje);
}
public void printJobNoMoreEvents(PrintJobEvent pje) {
lastEvent = pje.getPrintEventType();
System.err.println("* Todos os Ficheiros foram enviados para a impressora ");
allDone();
}
void allDone() {
synchronized (PrintJobWatcher.this) {
done = true;
PrintJobWatcher.this.notify();
}
}
});
}
public synchronized void waitForDone() {
try {
while (!done) {
wait();
}
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
Ele faz print de ficheiros jpf, gif, txt mas quando meto PDF ou PS dá um PrintJobFlavorException : Invalid Flavor :S