Okey. Cambié un poco el codigo he hice esto:
JFrame v = new JFrame("Recipe");
v.setLayout(null);
v.setBounds(300, 100, 400, 600);
v.setMinimumSize(new Dimension(400, 600));
JButton boton1 = new JButton();
v.setUndecorated(false);
JScrollPane scroll = new JScrollPane(editor);
scroll.setBounds(10, 20, 360, 470);
boton1.setBounds(10, 500, 180, 40);
boton1.setText("Imprimir");
v.getContentPane().add(scroll);
v.getContentPane().add(boton1);
boton1.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
imprimir();
}
});
v.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
// Marcamos el editor para que use HTML
editor.setContentType("text/html");
Ahora se crea una clase cuyo codigo es el siguiente:
public void imprimir(){
PrinterJob job = PrinterJob.getPrinterJob();
job.setPrintable( new imprimir());
try
{
job.print();
}
catch (PrinterException e)
{
}
}
private static class imprimir implements Printable {
protected int currentPage = -1;
protected JEditorPane editor;
protected double pageEndY = 0;
protected double pageStartY = 0;
protected boolean scaleWidthToFit = true;
protected PageFormat pFormat;
protected PrinterJob pJob;
public imprimir() {
pFormat = new PageFormat();
pJob = PrinterJob.getPrinterJob();
}
public Document getDocument() {
if (editor != null) return editor.getDocument();
else return null;
}
public boolean getScaleWidthToFit() {
return scaleWidthToFit;
}
public void pageDialog() {
pFormat = pJob.pageDialog(pFormat);
}
@Override
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) {
double scale = 1.0;
Graphics2D graphics2D;
View rootView;
// I
graphics2D = (Graphics2D) graphics;
// II
editor.setSize((int) pageFormat.getImageableWidth(),Integer.MAX_VALUE);
editor.validate();
// III
rootView = editor.getUI().getRootView(editor);
// IV
if ((scaleWidthToFit) && (editor.getMinimumSize().getWidth() >
pageFormat.getImageableWidth())) {
scale = pageFormat.getImageableWidth()/
editor.getMinimumSize().getWidth();
graphics2D.scale(scale,scale);
}
// V
graphics2D.setClip((int) (pageFormat.getImageableX()/scale),
(int) (pageFormat.getImageableY()/scale),
(int) (pageFormat.getImageableWidth()/scale),
(int) (pageFormat.getImageableHeight()/scale));
// VI
if (pageIndex > currentPage) {
currentPage = pageIndex;
pageStartY += pageEndY;
pageEndY = graphics2D.getClipBounds().getHeight();
}
// VII
graphics2D.translate(graphics2D.getClipBounds().getX(),
graphics2D.getClipBounds().getY());
// VIII
Rectangle allocation = new Rectangle(0,
(int) -pageStartY,
(int) (editor.getMinimumSize().getWidth()),
(int) (editor.getPreferredSize().getHeight()));
// X
if (printView(graphics2D,allocation,rootView)) {
return Printable.PAGE_EXISTS;
}
else {
pageStartY = 0;
pageEndY = 0;
currentPage = -1;
return Printable.NO_SUCH_PAGE;
}
}
public void print(HTMLDocument htmlDocument) {
setDocument(htmlDocument);
printDialog();
}
public void print(JEditorPane jedPane) {
setDocument(jedPane);
printDialog();
}
public void print(PlainDocument plainDocument) {
setDocument(plainDocument);
printDialog();
}
protected void printDialog() {
if (pJob.printDialog()) {
pJob.setPrintable(this,pFormat);
try {
pJob.print();
}
catch (PrinterException printerException) {
pageStartY = 0;
pageEndY = 0;
currentPage = -1;
System.out.println("Error Printing Document");
}
}
}
protected boolean printView(Graphics2D graphics2D, Shape allocation,
View view) {
boolean pageExists = false;
Rectangle clipRectangle = graphics2D.getClipBounds();
Shape childAllocation;
View childView;
if (view.getViewCount() > 0 &&
!view.getElement().getName().equalsIgnoreCase("td")) {
for (int i = 0; i < view.getViewCount(); i++) {
childAllocation = view.getChildAllocation(i,allocation);
if (childAllocation != null) {
childView = view.getView(i);
if (printView(graphics2D,childAllocation,childView)) {
pageExists = true;
}
}
}
} else {
// I
if (allocation.getBounds().getMaxY() >= clipRectangle.getY()) {
pageExists = true;
// II
if ((allocation.getBounds().getHeight() > clipRectangle.getHeight()) &&
(allocation.intersects(clipRectangle))) {
view.paint(graphics2D,allocation);
} else {
// III
if (allocation.getBounds().getY() >= clipRectangle.getY()) {
if (allocation.getBounds().getMaxY() <= clipRectangle.getMaxY()) {
view.paint(graphics2D,allocation);
} else {
// IV
if (allocation.getBounds().getY() < pageEndY) {
pageEndY = allocation.getBounds().getY();
}
}
}
}
}
}
return pageExists;
}
/* Method to set the content type the JEditorPane.
*/
protected void setContentType(String type) {
editor.setContentType(type);
}
public void setDocument(HTMLDocument htmlDocument) {
editor = new JEditorPane();
setDocument("text/html",htmlDocument);
}
public void setDocument(JEditorPane jedPane) {
editor = new JEditorPane();
setDocument(jedPane.getContentType(),jedPane.getDocument());
}
public void setDocument(PlainDocument plainDocument) {
editor = new JEditorPane();
setDocument("text/plain",plainDocument);
}
protected void setDocument(String type, Document document) {
setContentType(type);
editor.setDocument(document);
}
public void setScaleWidthToFit(boolean scaleWidth) {
scaleWidthToFit = scaleWidth;
}
}
Pero aun asi no imprime. Esta clase no la manejo bien y no se que parte tengo que modificar para obtener el tamaño de pagina deseado y la orientacion que quiero.