Java PrintFramework druckt nur eine Seite

  • Themenstarter Themenstarter Gelöschtes Mitglied 28562
  • Beginndatum Beginndatum
G

Gelöschtes Mitglied 28562

Gast
Hallo!

Ich bin neu hier. Ich hoffe, ihr könnt mir bei einem Problem helfen, dass ich schon seit Stunden versuche zu lösen:

Ich möchte eine Page-Klasse schreiben, die für mich das Drucken verwaltet. Benutzt werden soll sie, indem man von ihr erbt und die drawCustomizing(Graphics2D g2d, PageFormat pf) überschreibt.
Die Page-Klasse soll dabei automatisch das Graphics-Objekt auf die entsprechenden Seiten verteilen. Dies kann ein sehr langgezogenes Bild sein oder per g2d.drawString() erstellte Texte...

Leider druckt er nur die erste Seite und eine leere zweite Seite wenn ich in der abgeleiteten Klasse 80mal g2d.drawString("Test"...) untereinander schreibe...

Hier der Code:
Java:
package services.printing;

import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
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;

/**
 * A simple page which prints custom graphics on multiple pages
 *
 */
public class Page implements Printable {

    // standart font
    Font font = new Font("Serif", Font.PLAIN, 10);
    // standart margin
    double margin = 20;

	/**
	 * @param font the font to set
	 */
	public void setFont(Font font) {
		this.font = font;
	}

	/**
	 * @param margin the margin to set
	 */
	public void setMargin(double margin) {
		this.margin = margin;
	}

	/**
	 * Generate pages to print
	 */
	public int print(Graphics g, PageFormat pf, int pageIndex)
             throws PrinterException {
		// graphics 2D object
		Graphics2D g2d = (Graphics2D)g;
		
		// set margins
        Paper paper = pf.getPaper();
        paper.setImageableArea(margin, margin, paper.getWidth() - margin * 2, paper.getHeight()
            - margin * 2);
        pf.setPaper(paper);
        
        // get page height
        double pageHeight = pf.getImageableHeight();
        double pageWidth = pf.getImageableWidth();
        
        // push page to imagable area
        g2d.translate(pf.getImageableX(), pf.getImageableY());
        
        // push page orientation upwards with greater pageIndex
        g2d.translate(0, -pageIndex*pageHeight);

        //Draw custom enhancements on top of first page and reset 'y' and font
        if(pageIndex == 0) {
        	drawCustomizing(g2d,pf);
        }
        
        // number of pages
        int numberOfPages = (int) Math.ceil(g2d.getClipBounds().getHeight()/pageHeight);
        
        // if no pages left
        if (pageIndex >= numberOfPages) {
        	return NO_SUCH_PAGE;
          }
        
        // set clipping of this page
        g2d.setClip(0, pageIndex*(int)pageHeight, (int)pageHeight, (int)pageWidth);
        
        /* tell the caller that this page is part of the printed document */
        return PAGE_EXISTS;
    }
    
	/**
	 * Draw custom enhancements
	 * @param g2d
	 */
	public void drawCustomizing(Graphics2D g2d, PageFormat pf) {
		// Do nothing
	}

	/**
	 * Start printing this page
	 */
    public void print() {
        PrinterJob job = PrinterJob.getPrinterJob();
        job.setPrintable(this);
        boolean ok = job.printDialog();
        if (ok) {
            try {
                 job.print();
            } catch (PrinterException ex) {
             /* The job did not successfully complete */
            }
        }
    }

}

Dieser Beitrag hat mir bisher sehr gut weitergeholfen, aber irgendwo hakt es im Moment...
Chapter 6 Continued: Advanced Printing

Das wäre super, wenn mir jemand kurz helfen könnte. Ich hänge schon ewig dran :rtfm:
 
Zuletzt bearbeitet von einem Moderator:
Problem gelöst. Schade, dass das hier keiner bemerkt hat, als ich auf dem Schlauch stand.
Es war wirklich nicht schwer. Das meiste hing an meinem If-statement um die zeichenfunktion, das noch aus einer früheren konzeption stammte.

Falls es jemanden interessiert, hier die Lösung:

Java:
package services.printing;

import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
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;

/**
 * A simple page which prints custom graphics on multiple pages
 * @author DaCapitalist
 *
 */
public class Page implements Printable {

    // standart font
    Font font = new Font("Serif", Font.PLAIN, 10);
    // standart margin
    double margin = 20;

	/**
	 * @param font the font to set
	 */
	public void setFont(Font font) {
		this.font = font;
	}

	/**
	 * @param margin the margin to set
	 */
	public void setMargin(double margin) {
		this.margin = margin;
	}

	/**
	 * Generate pages to print
	 */
	public int print(Graphics g, PageFormat pf, int pageIndex)
             throws PrinterException {
		// graphics 2D object
		Graphics2D g2d = (Graphics2D)g;
		
		// set margins
        Paper paper = pf.getPaper();
        paper.setImageableArea(margin, margin, paper.getWidth() - margin * 2, paper.getHeight()
            - margin * 2);
        pf.setPaper(paper);
        
        // get page height
        double pageHeight = pf.getImageableHeight();
        double pageWidth = pf.getImageableWidth();
        
        // push page to imagable area
        g2d.translate(pf.getImageableX(), pf.getImageableY());
        
        // push page orientation upwards with greater pageIndex
        g2d.translate(0, Math.round(-pageIndex*pageHeight));

        //Draw custom enhancements on top of first page and reset 'y' and font
        drawCustomizing(g2d,pf);
        
        // number of pages
        int numberOfPages = (int) Math.ceil(g2d.getClipBounds().getHeight()/pageHeight);
        
        // if no pages left
        if (pageIndex > numberOfPages) {
        	return NO_SUCH_PAGE;
          }
        
        // set clipping of this page
        g2d.setClip(0, pageIndex*(int)pageHeight, (int)pageHeight, (int)pageWidth);
        
        /* tell the caller that this page is part of the printed document */
        return PAGE_EXISTS;
    }
    
	/**
	 * Draw custom enhancements under the actual page text
	 * @param g2d
	 */
	public void drawCustomizing(Graphics2D g2d, PageFormat pf) {
		// Do nothing
	}

	/**
	 * Start printing this page
	 */
    public void print() {
        PrinterJob job = PrinterJob.getPrinterJob();
        job.setPrintable(this);
        boolean ok = job.printDialog();
        if (ok) {
            try {
                 job.print();
            } catch (PrinterException ex) {
             /* The job did not successfully complete */
            }
        }
    }

}
 
Hallo DaCapitalist,

das ist doch schön, dass du den Fehler selbst gefunden hast! Hier muss man schon mal bis zu einem Tag (selten mehr) auf eine Antwort warten. Es kann ja nicht jeder jederzeit im Forum sein. Darüber hinaus ist die Java Print Api nicht unbedingt jedermanns Sache.

Deine Lösung druckt übrigens jetzt eine zusätzliche Leerseite, vielleicht ist das ja auch gewollt. Wie ich dich einschätze, weißt du jedenfalls schon selbst, warum diese zusätzliche Leerseite gedruckt wird 😀.

Ach ja, herzlich willkommen im Forum 🙂.

Gruß,
André
 

Neue Themen


Zurück
Oben