Java 1.4 Print API is flawed

Status
Not open for further replies.
After spending an afternoon messing with the 1.4 print api I decided to search the Java discussion group only to find numerous posts detailing the same problem I have;

To print you first need to create a Doc object.

Code:
Doc doc = new SimpleDoc(fis, textFlavor, null);

fis = a FileInputStream object relating to a .txt file
textFlavor = a supported DocFlavor object using plain text as the data type

Problem is, me (and everyone else) is told that their printer does not support the plain text DocFlavor.

Here is the output of the getSupportedFlavors method;

Code:
 image/gif; class="[B"
image/gif; class="java.io.InputStream"
image/gif; class="java.net.URL"
image/jpeg; class="[B"
image/jpeg; class="java.io.InputStream"
image/jpeg; class="java.net.URL"
image/png; class="[B"
image/png; class="java.io.InputStream"
image/png; class="java.net.URL"
application/x-java-jvm-local-objectref; class="java.aw
application/x-java-jvm-local-objectref; class="java.aw
application/octet-stream; class="[B"
application/octet-stream; class="java.net.URL"
application/octet-stream; class="java.io.InputStream"

I can print a .gif using the 2nd DocFlavor in the list above.

Any ideas? Will this be fixed?

Thanks

Dom.
 
Read the documentation for SimpleDoc ...
It can handle all of the presently defined "pre-defined" doc flavors defined as static variables in the DocFlavor class.

Those are...
DocFlavor.BYTE_ARRAY, DocFlavor.CHAR_ARRAY, DocFlavor.INPUT_STREAM, DocFlavor.READER, DocFlavor.SERVICE_FORMATTED, DocFlavor.STRING, DocFlavor.URL
http://java.sun.com/j2se/1.4/docs/api/javax/print/DocFlavor.html

What did you define "textFlavor" as? Is it like ...
Code:
DocFlavor textFlavor = new DocFlavor("text/plain", "java.io.InputStream");
as suggested in the above link?
 
Yes, I got the same output as you. It does seem like the current implementation is incomplete. From what I've read so far, only raster style printing is currently implemented. If you want to print text, you're going to have to do it the hard way and implement a Printable object.

Here's some code (slightly altered) I found in the newsgroups that partially works. He's using a component to render the page and then attempting to copy a page full at a time from the component. For me, only the first page actually prints.

For plain text, I think a simple approach can do the same thing. Get the "page size", resize a Cavas type object, clear the canvase, and print to it line by line until no more text can fit.

If I get enought time, I'll try to implement my own.


PrintHTMLPage.java
Code:
import java.io.*;
import java.awt.*;
import java.net.*;
import java.awt.image.*;
import java.awt.print.*;
import javax.print.*;
import javax.print.attribute.*;
import javax.print.attribute.standard.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.text.html.*;

public class PrintHTMLPage implements Printable
  {
  private JFrame frame = new JFrame();
  private JEditorPane htmlPane = new JEditorPane();
  private PrintService service;

  public PrintHTMLPage( int report, PrintService service )
    {
    this.service = service;

    String filePath = "PrintHTMLPage.java";
    File file = new File( filePath );

    URL url = null;
    try
      {
      url = file.toURL();
      }
    catch( MalformedURLException e )
      {
      System.err.println( "bad url" );
      }
	System.out.println(url.toString());

    createDocument();

    try
      {
      htmlPane.setPage( url );
      }
    catch( IOException ioe )
      {
      System.err.println( ioe.toString() );
      }

    htmlPane.setVisible( true );
    htmlPane.setEditable( false );

    frame.setContentPane( htmlPane );
    frame.setVisible( false );
    } // constructor



  public static void main(String[] args) {
	PrintHTMLPage p = new PrintHTMLPage(0, null);
	p.printContent();
  } 

  //----------------------------------------
  // Creates the associated Document
  //----------------------------------------
  public void createDocument()
    {
    HTMLEditorKit kit = new HTMLEditorKit();
    htmlPane.setEditorKit( kit );
    htmlPane.setDocument((HTMLDocument)(kit.createDefaultDocument()));
    htmlPane.setContentType( "text/html" );
    }


  //----------------------------------------
  // Prints the content of htmlPane
  //----------------------------------------
  public void printContent()
    {
    PrinterJob printerJob = PrinterJob.getPrinterJob();
	//
    try
      {
      if( !printerJob.getPrintService().equals( service ) )
        printerJob.setPrintService( service );
      }
    catch( PrinterException e )
      {
      System.err.println( e.getStackTrace() );
      }

    PageFormat pform = printerJob.defaultPage();
    Paper myPaper = pform.getPaper();
    myPaper.setImageableArea( 36.0, 36.0, 540.0, 720.0 );
    pform.setPaper( myPaper );

    PrinterResolution resolution = new PrinterResolution( 300, 300,
ResolutionSyntax.DPI );
    MediaPrintableArea printableArea = new MediaPrintableArea( 0.5f,
0.5f, 7.5f, 10.0f, MediaPrintableArea.INCH );
    HashPrintRequestAttributeSet set = new
HashPrintRequestAttributeSet( resolution );
    set.add( printableArea );

    printerJob.setPrintable( this, pform );

htmlPane.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));

    if( printerJob.printDialog( set ) )
      {
      try
        {
        printerJob.print( set );
        }
      catch( PrinterException pe )
        {
        System.err.println( "PrinterException:" + pe.getMessage() );
        }
      }

    } // printContent


  //----------------------------------------
  // Implements the print method in interface Printable
  //----------------------------------------
  public int print( Graphics g, PageFormat pform, int pageNumber )
    {
    double pageOffset = pageNumber * pform.getImageableHeight();
    View view = htmlPane.getUI().getRootView( htmlPane );

    if( pageOffset > view.getPreferredSpan( View.Y_AXIS ) )
      return Printable.NO_SUCH_PAGE;

    ((Graphics2D)g).translate( 0d, pageOffset );
    g.setFont( new Font( "Monospaced", Font.PLAIN, 8 ) );

    Rectangle rect = new Rectangle();
    rect.setRect( pform.getImageableX(), pform.getImageableY(),
        pform.getImageableWidth(), pform.getImageableHeight() +
        pageOffset );
    view.paint( g, rect );

    return Printable.PAGE_EXISTS;

    } // print 
  } // class PrintHTMLPage
 
Status
Not open for further replies.