Olá
estou com um grande problema em impressão, tenho um jpanel de 1000 x 1000 e preciso imprimir o conteudo dele, achei os códigos abaixo porem nenhum deles imprime o jpanel completo e com tamanho médio, eles imprimem somente uma página e com uma parte do conteudo ou inteiro em tamanho minusculo.
preciso imprimir o jpanel e várias páginas e com todo o desenho( uma parte em cada página para a imagem não ficar muito pequena)
alguém pode me ajudar? é super inportante.
Obrigado
códigos:
package Model;
import java.awt.*;
import javax.swing.*;
import java.awt.print.*;
public class PrintUtilities implements Printable
{
private Component componentToBePrinted;
public static void printComponent(Component c)
{
new PrintUtilities(c).print();
}
public PrintUtilities(Component componentToBePrinted)
{
this.componentToBePrinted = componentToBePrinted;
}
public void print()
{
PrinterJob printJob = PrinterJob.getPrinterJob();
printJob.setPrintable(this);
if (printJob.printDialog())
try
{
printJob.print();
}
catch(PrinterException pe)
{
System.out.println("Error printing: " + pe);
}
}
public int print(Graphics g, PageFormat pageFormat, int pageIndex)
{
if (pageIndex > 0)
{
return(NO_SUCH_PAGE);
}
Graphics2D g2d = (Graphics2D)g;
g2d.scale(0.5,0.5);
g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
disableDoubleBuffering(componentToBePrinted);
componentToBePrinted.paint(g2d);
enableDoubleBuffering(componentToBePrinted);
return(PAGE_EXISTS);
}
/** The speed and quality of printing suffers dramatically if
* any of the containers have double buffering turned on.
* So this turns if off globally.
* @see enableDoubleBuffering
*/
public static void disableDoubleBuffering(Component c)
{
RepaintManager currentManager = RepaintManager.currentManager(c);
currentManager.setDoubleBufferingEnabled(false);
}
/** Re-enables double buffering globally. */
public static void enableDoubleBuffering(Component c)
{
RepaintManager currentManager = RepaintManager.currentManager(c);
currentManager.setDoubleBufferingEnabled(true);
}
}
imprime em um tamanho legal, porem só imprime uma parte do código, e se eu mudar o if (pageIndex > 0) para if (pageIndex > 1 ou 2) ele repete a imagem
package Model;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.awt.print.*;
class PrintableJPanel extends JPanel implements Printable
{
public PrintableJPanel(){
}
public int print(Graphics g, PageFormat pf, int pageIndex) throws PrinterException
{
Graphics2D g2 = (Graphics2D)g;
g2.setColor(Color.black); //set default foreground color to black
RepaintManager.currentManager(this).setDoubleBufferingEnabled(false);
Dimension d = this.getSize(); //get size of document
double panelWidth = d.width; //width in pixels
double panelHeight = d.height; //height in pixels
double pageHeight = pf.getImageableHeight(); //height of printer page
double pageWidth = pf.getImageableWidth(); //width of printer page
double scale = (pageWidth/panelWidth);
int totalNumPages = (int)Math.ceil(scale * panelHeight / pageHeight);
if(pageIndex >= totalNumPages)
{
return Printable.NO_SUCH_PAGE;
}
g2.translate(pf.getImageableX(), pf.getImageableY());
g2.translate(0f, -pageIndex*pageHeight);
g2.scale(scale, scale);
this.paint(g2);
return Printable.PAGE_EXISTS;
}
public void doPrintActions()
{
PrinterJob pj=PrinterJob.getPrinterJob();
pj.setPrintable(this);
pj.printDialog();
try
{
//pj.setPrintable()
pj.print();
}
catch (Exception PrintException)
{
}
}
}
imprime o desenho inteiro em uma unica página, porem muito pequeno
.