Boa tarde galera, estou fazendo alguns testes aqui e me deparei com algo muito estranho, gostaria da ajuda de vocês para visualizar o meu erro, e consecutivamente o corrigir.
O que estou fazendo:
Tenho um texto em HTML dentro de um JLabel que esta dentro de um JPanel ( o qual é impresso ).
O que acontece:
Tento fazer a impressão utilizando tanto px quanto pt.
Caso utilize pt o tamanho 9 é impresso corretamente já o 10 e 11 somem espaços.
Caso utilize px o tamanho 9 some o espaço o 10 e o 11 são impressos corretamente.
Isso só acontece se eu estiver utilizando letras maiúsculas e em negrito.
A parte da visualização em tela esta normal mas a impressão não corresponde ao que é visualizado ( somem alguns espaços ).
Os testes foram efetuados em 2 impressoras PDF (Bullzip e Doro) e uma impressora física (LaserJet 1300) em todos os testes a saída é a mesma.
Acredito que o problema esteja na minha forma de impressão, se alguém já passou por isso me de uma luz.
Segue Classes de teste:
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;
public class LabelComHTML extends JFrame{
private static final long serialVersionUID = -8615148794506575325L;
private JLabel label, label1, label2;
private JButton button;
private JPanel panel;
public LabelComHTML(){
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setSize(500, 400);
this.setLocationRelativeTo(null);
this.setLayout(null);
this.panel = new JPanel();
this.panel.setLayout(null);
this.panel.setBorder(new TitledBorder(""));
this.panel.setBounds(10, 10, 465, 325);
this.panel.setBackground(Color.white);
this.add(this.panel);
this.label = new JLabel(labelTexto(0));
this.label.setBounds(10, 10, 445, 100);
this.label.setBorder(new TitledBorder(""));
this.label.setBackground(Color.white);
this.panel.add(this.label);
this.label1 = new JLabel(labelTexto(1));
this.label1.setBounds(10, 110, 445, 100);
this.label1.setBorder(new TitledBorder(""));
this.label1.setBackground(Color.white);
this.panel.add(this.label1);
this.label2 = new JLabel(labelTexto(2));
this.label2.setBounds(10, 210, 445, 100);
this.label2.setBorder(new TitledBorder(""));
this.label2.setBackground(Color.white);
this.panel.add(this.label2);
this.button = new JButton("Clique");
this.button.setBounds(10, 335, 75, 25);
this.button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
PrintService impressoraPadrao = PrintServiceLookup.lookupDefaultPrintService();
new ComponentPrinter(panel, impressoraPadrao);
}
});
this.add(this.button);
}
private String labelTexto(int codigo){
String texto = "<html><p style=\"font-family:Arial;color:#000000;font-size:TAMANHO;text-align:justify;\">" +
"<b>" +
"SAIBAM QUANTOS ESTA PÚBLICA ESCRITURA VIREM QUE, AOS SEIS DIAS DO MÊS DE FEVEREIRO." +
" + SAIBAM QUANTOS ESTA PÚBLICA ESCRITURA VIREM QUE, AOS SEIS DIAS DO MÊS DE FEVEREIRO." +
"</b>" +
"</p></html>";
switch (codigo) {
// em PX
// case 0: return texto.replace("TAMANHO", "9px");
// case 1: return texto.replace("TAMANHO", "10px");
// case 2: return texto.replace("TAMANHO", "11px");
// case 3: return texto.replace("TAMANHO", "12px");
// case 4: return texto.replace("TAMANHO", "13px");
// OU em PT
case 0: return texto.replace("TAMANHO", "10pt");
case 1: return texto.replace("TAMANHO", "11pt");
case 2: return texto.replace("TAMANHO", "12pt");
case 3: return texto.replace("TAMANHO", "13pt");
case 4: return texto.replace("TAMANHO", "14pt");
default:
return null;
}
}
public static void main(String[] args) {
new LabelComHTML().setVisible(true);
}
}
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import javax.print.PrintService;
public class ComponentPrinter implements Printable, ActionListener {
private Component componentToPrint;
private PrintService servico;
/**
* Construtor do ComponentPrinter.
* @param componente (Component) - Componente que vai ser impresso.
* @param service (PrintService) - Impressora que recebera o Job.
*/
public ComponentPrinter(Component componente, PrintService service) {
componentToPrint = componente;
servico = service;
this.actionPerformed(null);
}
//Quando mandamos uma página ser impressa pelo PrinterJob, ele vai executando continuamente este método ate que o retorno seja a constante NO_SUCH_PAGE
public int print(Graphics graphics, PageFormat pf, int page) throws PrinterException {
/** Teremos apenas uma página, que inicia em zero, por isso quando a página for maior que zero retornamos NO_SUCH_PAGE*/
if (page > 0)
return NO_SUCH_PAGE;
Graphics2D g2d = (Graphics2D) graphics;
componentToPrint.print(g2d);
return PAGE_EXISTS;
}
public void actionPerformed(ActionEvent evt) {
PrinterJob printerJob = PrinterJob.getPrinterJob();
try {
// Seta a impressora selecionada.
printerJob.setPrintService(servico);
// Passa a própria classe e as configurações do page format.
printerJob.setPrintable(this, getPageFormat());
// Imprime
printerJob.print();
} catch (Exception e) { e.printStackTrace();}
}
/**
* Método para retornar o PageFormar de uma folha A4.
* @return pageFormar (PageFormat).
*/
private PageFormat getPageFormat(){
PageFormat pageFormat = new PageFormat();
Paper paper = new Paper();
paper.setSize(595, 842);
paper.setImageableArea(0, 0, 595, 842);
pageFormat.setPaper(paper);
return pageFormat;
}