🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.api
Article #569

Re: Print Preview of HTMLDocument

#569
From: Alan Shiers
Date: Tue, 10 Jun 2003 21:02
150 lines
5129 bytes
Hi there,


I'm not sure if what I have to offer will help you, but I once created a
class that prints the contents of a JTextArea object after having
created a simple text editor project.  Perhaps you will find something
in this class that you can use:

package client;

import java.awt.*;
import javax.swing.*;
import java.awt.print.*;

 

public class JTextAreaPrintUtility implements Printable
{
  private JTextArea myJTextArea;

  /**
   * A static function that will print the contents of a JTextArea
object.
   * @param Requires a JTextArea object to be passed as an arguement.
   */
  public static void printComponent(JTextArea textArea)
  {
    new JTextAreaPrintUtility(textArea).print();
  }

  public JTextAreaPrintUtility(JTextArea textArea)
  {
    this.myJTextArea = textArea;
  }

  /**
   * This function performs the printing.
   */
  public void print()
  {
    PrinterJob pj = PrinterJob.getPrinterJob();
    pj.setPrintable(this);
    try
    {
      if(pj.printDialog())
      {
        pj.print();
      }

    }
    catch(Exception PrintException){}
  }

  /**
   * This function performs the actual calculations necessary for
printing.
   * @param Graphics object, PageFormat object, and an integer
   * @return An integer value indicating the "success" or "no success"
of the print job.
   */
  public int print(Graphics g, PageFormat pageFormat, int pageIndex)
  {
    Graphics2D g2 = (Graphics2D)g;
    g2.setColor(Color.yellow);
    int fontHeight = g2.getFontMetrics().getHeight();
    int fontDesent = g2.getFontMetrics().getDescent();
    double pageHeight = pageFormat.getImageableHeight() - fontHeight;
    double pageWidth = pageFormat.getImageableWidth();
    double headerHeightOnPage = 16.5;
    double oneRowHeight = fontHeight;
    int numRowsOnAPage = (int)((pageHeight -
headerHeightOnPage)/oneRowHeight);
    int totalNumPages =
(int)Math.ceil(((double)(myJTextArea.getLineCount())/numRowsOnAPage));
    double pageHeightForTable=oneRowHeight*numRowsOnAPage;
    double tableWidth = 468;
    double scale=1;

    if (tableWidth >= pageWidth)
    {
       scale = pageWidth / tableWidth;
    }
    double tableWidthOnPage = tableWidth*scale;

    if(pageIndex>=totalNumPages)
    {
        return NO_SUCH_PAGE;
    }


    g2.translate(pageFormat.getImageableX(),
pageFormat.getImageableY());

    g2.drawString("Page: "+(pageIndex+1), (int)pageWidth/2-35,
(int)(pageHeight + fontHeight-fontDesent));

    g2.translate(0f,headerHeightOnPage);
    g2.translate(0f,-pageIndex*pageHeightForTable);

    if (pageIndex + 1 == totalNumPages)
    {
      int lastRowPrinted = numRowsOnAPage * pageIndex;
      int numRowsLeft = totalNumPages*numRowsOnAPage - lastRowPrinted;

      g2.setClip(0,(int)(pageHeightForTable * pageIndex),(int)
Math.ceil(tableWidthOnPage), (int) Math.ceil(oneRowHeight *
numRowsLeft));
    }
    else
    {
      g2.setClip(0,(int)(pageHeightForTable*pageIndex),(int)
Math.ceil(tableWidthOnPage),(int) Math.ceil(pageHeightForTable));
    }

    g2.scale(scale,scale);
    myJTextArea.paint(g2);
    g2.scale(1/scale,1/scale);
    g2.translate(0f,pageIndex*pageHeightForTable);
    g2.translate(0f, -headerHeightOnPage);
    g2.setClip(0, 0,(int)
Math.ceil(tableWidthOnPage),(int)Math.ceil(headerHeightOnPage));
    g2.scale(scale,scale);

     return Printable.PAGE_EXISTS;
   }

   /**
   * A static method.
   * The speed and quality of printing suffers dramatically if
   * any of the containers have double buffering turned on.
   * @see enableDoubleBuffering
   * @param Component object
   */
  public static void disableDoubleBuffering(Component c)
  {
    RepaintManager currentManager = RepaintManager.currentManager(c);
    currentManager.setDoubleBufferingEnabled(false);
  }

  /**
   * Re-enables double buffering globally.
   * @param Component object
   */
  public static void enableDoubleBuffering(Component c)
  {
    RepaintManager currentManager = RepaintManager.currentManager(c);
    currentManager.setDoubleBufferingEnabled(true);
  }

}
Technical Details
Message-ID: <3EE64750.A0B96C6B@hfx.eastlink.ca>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-04!sn-xit-01!sn-xit-09!supernews.com!diablo.theplanet.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <3e99b8fa$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 1089d0,770c0fa2aab7be90
X-Google-Attributes: gid1089d0,public
X-Google-ArrivalTime: 2003-06-10 14:09:55 PST
Message-ID: <3EE64750.A0B96C6B@hfx.eastlink.ca>
Date: Tue, 10 Jun 2003 18:02:08 -0300
From: Alan Shiers <ashiers@hfx.eastlink.ca>
X-Mailer: Mozilla 4.78 [en] (Win98; U)
X-Accept-Language: en
MIME-Version: 1.0
Newsgroups: borland.public.jbuilder.java.api
Subject: Re: Print Preview of HTMLDocument
References: <3e99b8fa$1@newsgroups.borland.com>
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
NNTP-Posting-Host: 24.222.101.2
X-Trace: newsgroups.borland.com 1055279006 24.222.101.2 (10 Jun 2003 14:03:26 -0700)
Lines: 150
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-04!sn-xit-01!sn-xit-09!supernews.com!diablo.theplanet.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail