/* FilePrintHelper.java */import java.util.*;public class FilePrintHelper{ //---Membervariablen---------------------------------- Vector pageinfo; //---Konstruktor-------------------------------------- public FilePrintHelper() { pageinfo = new Vector(); } //---Seitendefinition und -abfrage-------------------- public void createPage(int page) { for (int i = pageinfo.size(); i <= page; ++i) { pageinfo.addElement(new Entry()); } } public boolean knownPage(int page) { return page < pageinfo.size(); } //---Verwaltung der Offsets--------------------------- public long getFileOffset(int page) { Entry entry = (Entry)pageinfo.elementAt(page); return entry.fileoffset; } public void setFileOffset(int page, long fileoffset) { Entry entry = (Entry)pageinfo.elementAt(page); entry.fileoffset = fileoffset; } //---Lokale Klasse Entry------------------------------ static class Entry { public long fileoffset; public Entry() { this.fileoffset = -1; } }}
/* MyFilePrinter.java */import java.awt.*;import java.awt.print.*;import java.io.*;import java.util.*;import java.text.*;public class MyFilePrinter implements Printable{ //---Konstanten-------------------------------------- private static final int RESMUL = 4; //---Membervariablen--------------------------------- private PrinterJob pjob; private PageFormat pageformat; public FilePrintHelper fph; private String fname; private RandomAccessFile in; Date myDate = new Date(); String aktudate = new SimpleDateFormat("dd.MM.yyyy / HH:mm").format(myDate); //---Konstruktoren----------------------------------- public MyFilePrinter(String fname) { this.pjob = PrinterJob.getPrinterJob(); this.fname = fname; } //---Öffentliche Methoden---------------------------- public boolean setupPageFormat() { PageFormat defaultPF = pjob.defaultPage(); this.pageformat = pjob.pageDialog(defaultPF); pjob.setPrintable(this, this.pageformat); return (this.pageformat != defaultPF); } public boolean setupJobOptions() { return pjob.printDialog(); } public void printFile() throws PrinterException, IOException { fph = new FilePrintHelper(); in = new RandomAccessFile(fname, "r"); pjob.print(); in.close(); } //---Implementierung von Printable------------------- public int print(Graphics g, PageFormat pf, int page) throws PrinterException { int ret = PAGE_EXISTS; String line = null; try { if (fph.knownPage(page)) { in.seek(fph.getFileOffset(page)); line = in.readLine(); } else { long offset = in.getFilePointer(); line = in.readLine(); if (line == null) { ret = NO_SUCH_PAGE; } else { fph.createPage(page); fph.setFileOffset(page, offset); } } if (ret == PAGE_EXISTS) { //Seite ausgeben, Grafikkontext vorbereiten Graphics2D g2 = (Graphics2D)g; g2.scale(1.0 / RESMUL, 1.0 / RESMUL); int ypos = (int)pf.getImageableY() * RESMUL; int xpos = ((int)pf.getImageableX() + 2) * RESMUL; int yd = 12 * RESMUL; int ymax = ypos + (int)pf.getImageableHeight() * RESMUL - yd; int fontAdvance = g2.getFontMetrics().stringWidth(aktudate); //Seitentitel ausgeben String title = fname.substring(12); ypos += yd; g2.setColor(Color.black); g2.setFont(new Font("Monospaced", Font.BOLD, 10 * RESMUL)); g.drawString(title, xpos, ypos); g.drawString("Seite " + (page + 1),(int) pf.getImageableWidth() / 2 * RESMUL + fontAdvance, ypos); g.drawString(aktudate + " Uhr", + (int) pf.getImageableWidth() * RESMUL - yd * 3,ypos); // Unterstrich - Linie ausgeben g.drawLine(xpos,ypos + 6 * RESMUL,xpos + (int)pf.getImageableWidth() * RESMUL,ypos + 6 * RESMUL); ypos += 2 * yd; //Zeilen ausgeben g2.setColor(new Color(0, 0, 127)); g2.setFont(new Font("Monospaced", Font.PLAIN, 9 * RESMUL)); while (line != null) { g.drawString(line, xpos, ypos); ypos += yd; if (ypos >= ymax) { break; } line = in.readLine(); } } } catch (IOException e) { throw new PrinterException(e.toString()); } return ret; }}
/*** Methode zum Ausdrucken der Textdateien**/private void printFile(){ if (mfp.setupPageFormat()) { if (mfp.setupJobOptions()) { try { mfp.printFile(); } catch (Exception e) { System.out.println("in printFile ist ein Fehler"); } } }}