Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Java Printing

  Asked By: Kent    Date: Dec 05    Category: Java    Views: 1023
  

I am having trouble printing from an applet, I hope some here has
experience with this...

I have an applet that prints HTML pages. This is accomplished by
printing the contents of a jEditorPane set to html (see the following
code). However, when I print an HTML page I only get the upper
lefthand quadrant of the page (as if the page is mis-sized). I can
print the same html page through any other application and get a neat
1 page-full printout...Any insight would be greatfully appreciated..

Marz

(code...don't try to compile what I have here..this is the "good
parts" version, anything print related that is in my code is listed
below...the first section is the setup, and the second section is the
actual printing of the componant)

<sec. 1>

try{
HTMLDocument doc = (HTMLDocument)hk.createDefaultDocument();
StyleSheet doc_style = doc.getStyleSheet();
doc_style.addRule("BODY{ margin : 0;}");
doc_style.addRule("A { text-decoration : none}");
Enumeration rules = doc_style.getStyleNames();
while (rules.hasMoreElements()) {
String name = (String) rules.nextElement();
Style rule = doc_style.getStyle(name);
System.out.println(rule.toString());
}
hk.setStyleSheet(doc_style);
hk.install(jEditorPane2);
jEditorPane2.setEditorKit(hk);
jEditorPane2.setDocument(doc);
jEditorPane2.setPage(URL_to_print);

}
catch (IOException e){
System.out.println("Bad URL: " + url);
jTextField1.setText("Bad URL:" + url);
}


<sec. 2>

PrinterJob printJob = PrinterJob.getPrinterJob();
PageFormat pf = printJob.pageDialog(printJob.defaultPage());
RepaintManager.currentManager
(this.componentToBePrinted).setDoubleBufferingEnabled(false);
printJob.setPrintable(this, pf);
if (printJob.printDialog())
try {
printJob.print();
} catch(PrinterException pe) {
System.out.println("Error printing: " + pe);
}
}

Share: 

 

2 Answers Found

 
Answer #1    Answered By: Calvin Banks     Answered On: Dec 05

Hello could you please help me with java  printing? This is my code...
I used the Java API for printing. The problem is this code  doesnt
find the print  services. Any advice? thanks

import java.io.*;
import javax.print.*;
import javax.print.attribute.*;
import javax.print.attribute.standard.*;
import javax.print.event.*;

public class PrintFoo
{

public static void main(String args[])
{
PrintFoo ps = new PrintFoo();
}
public PrintFoo()
{
try
{
FileInputStream textStream;
textStream=new FileInputStream("tobeprinted.txt");

if(textStream==null)
{
System.out.println("asdfsaf");
return;
}

DocFlavor flavor=DocFlavor.INPUT_STREAM.POSTSCRIPT;
Doc mydoc=new SimpleDoc(textStream,flavor,null);
PrintRequestAttributeSet aset=new HashPrintRequestAttributeSet();
aset.add(new Copies(2));

PrintService[]services=PrintServiceLookup.lookupPrintServices(flavor,aset);
System.out.println(services.length);

if(services.length>0)
{
DocPrintJob job=services[0].createPrintJob();
try
{
job.print(mydoc,aset);
}
catch(PrintException pe)
{
System.err.println(pe);
}
}
}
catch(Exception ee)
{
System.err.println(ee);
}
}
}

 
Answer #2    Answered By: Ralph Murray     Answered On: Dec 05

import java.awt.geom.*;
import java.awt.print.*;

public void actionPerformed(ActionEvent e) {

// Get a PrinterJob
PrinterJob job = PrinterJob.getPrinterJob();
// Create a landscape page  format
PageFormat landscape = job.defaultPage();
landscape.setOrientation(PageFormat.LANDSCAPE);
// set  up a book
Book bk = new Book();
bk.append(new PaintCover(), job.defaultPage());
bk.append(new PaintContent(), landscape);
// Pass the book to the PrinterJob
job.setPageable(bk);
// Put up the dialog box
if (job.printDialog()) {
// print  the job if the user didn't cancel printing
try { job.print(); }
catch (Exception exc) { /* Handle Exception */ }
}





OR TRY THE FULL PROGRAM



import java.awt.event.*;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.*;
import java.awt.geom.*;
import java.awt.print.*;
/* <applet code="SimpleBook" height=200 width=200></applet>*/

public class SimpleBook extends JPanel implements ActionListener{

final static Color bg = Color.white;
final static Color fg = Color.black;
final static Color red = Color.red;
final static Color white = Color.white;

final static BasicStroke stroke = new BasicStroke(2.0f);
final static BasicStroke wideStroke = new BasicStroke(8.0f);

final static float dash1[] = {10.0f};
final static BasicStroke dashed = new BasicStroke(1.0f,

BasicStroke.CAP_BUTT,

BasicStroke.JOIN_MITER,
10.0f, dash1,
0.0f);
final static JButton button = new JButton("Print");

public SimpleBook() {
setBackground(bg);
button.addActionListener(this);
}

public void actionPerformed(ActionEvent e) {


 
Didn't find what you were looking for? Find more on Java Printing Or get search suggestion and latest updates.




Tagged: