Java Printing - DocPrintJob

1

I have this method to do printing:

public void imprimir(String texto) {
    PrintService[] printService = PrintServiceLookup.lookupPrintServices(DocFlavor.INPUT_STREAM.AUTOSENSE, null);
    PrintService impressoraPadrao = PrintServiceLookup.lookupDefaultPrintService();
    DocFlavor docFlavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
    HashDocAttributeSet hashDocAttributeSet = new HashDocAttributeSet();

    InputStream stream = new ByteArrayInputStream((texto + "\n").getBytes());
    Doc doc = new SimpleDoc(stream, docFlavor, hashDocAttributeSet);
    PrintRequestAttributeSet printRequestAttributeSet = new HashPrintRequestAttributeSet();
    PrintService printServico = ServiceUI.printDialog(null, 300, 200, printService, impressoraPadrao, docFlavor, printRequestAttributeSet);
    if (printServico != null) {
        DocPrintJob docPrintJob = printServico.createPrintJob();
        try {
            docPrintJob.print(doc, printRequestAttributeSet);
        } catch (PrintException e) {
            JOptionPane.showMessageDialog(null, "Erro: " + e.getMessage());
        }
    }
}

No error occurs.

But it also does not print, and when I print to PDFCreator the page goes blank.

But if I send an image like this it prints normal:

public void imprimir(String texto) {
    PrintService[] printService = PrintServiceLookup.lookupPrintServices(DocFlavor.INPUT_STREAM.AUTOSENSE, null);
    PrintService impressoraPadrao = PrintServiceLookup.lookupDefaultPrintService();
    DocFlavor docFlavor = DocFlavor.INPUT_STREAM.PNG;
    HashDocAttributeSet hashDocAttributeSet = new HashDocAttributeSet();

    try {
         FileInputStream stream = new FileInputStream("C:\Users\Rodrigo\Desktop\Trab4_Luc\teste.png");
         Doc doc = new SimpleDoc(stream, docFlavor, hashDocAttributeSet);
         PrintRequestAttributeSet printRequestAttributeSet = new HashPrintRequestAttributeSet();
         PrintService printServico = ServiceUI.printDialog(null, 300, 200, printService, impressoraPadrao, docFlavor, printRequestAttributeSet);
         if (printServico != null) {
         DocPrintJob docPrintJob = printServico.createPrintJob();
         try {
             docPrintJob.print(doc, printRequestAttributeSet);
         } catch (PrintException e) {
             JOptionPane.showMessageDialog(null, "Erro: " + e.getMessage());
         }
    } catch (FileNotFoundException ex) {
         JOptionPane.showMessageDialog(null, "Erro: " + e.getMessage());
    }
}

What am I doing wrong?

    
asked by anonymous 23.05.2016 / 19:34

1 answer

4

The difference between the code that works is

DocFlavor docFlavor = DocFlavor.INPUT_STREAM.AUTOSENSE;

for

DocFlavor docFlavor = DocFlavor.INPUT_STREAM.PNG;

I suggest switching to:

DocFlavor docFlavor = DocFlavor.INPUT_STREAM.TEXT_PLAIN_UTF_8;
HashDocAttributeSet hashDocAttributeSet = new HashDocAttributeSet();

InputStream stream = new ByteArrayInputStream((texto + 
"\n").getBytes(StandardCharsets.UTF_8));

Using a hexadecimal editor, I am sure it is sending this to the printer. I can not say that the printer can print what I am sending.

If this does not work for you, then the only way, seems to be to render the text in image, and to follow with the alternative path that you quoted.     

27.05.2016 / 20:16