Article View: borland.public.jbuilder.java.language
Article #18991Re: Printing Reports
From: Jennifer Merkel
Date: Fri, 20 Mar 1998 00:00
Date: Fri, 20 Mar 1998 00:00
258 lines
10382 bytes
10382 bytes
Thank you for the code but I am more interested in working with applications
than applets. You said that you can send allyou want to a file, which I have
done. But how do you retrieve it later and send it to the printer? Can it be
done without leaving your application. What I want to able to do is for the uer
to be able to select File and then Print for example and have a report come out
of the printer withoutthem having to do anything else. Do you know how to do
this? If so could you give me some instructions?
Thanks
Jennifer
Conrad Hoesle-Kienzlen wrote:
> Jennifer,
> if you are writing an application, there is no problem. You can write all
> you like to a file and later retrieve that and send it to the printer. If
> you are writing an applet, things don't look so well. You can't just write
> anything to a file, at least not on the target machine, you would have to
> use the server from where the applet was loaded for that.
> Printing, in general, *has* to do with graphics, always, because you don't
> "just send some text to the printer" as you would with any other PC-based
> language. You have to get a graphics context for the target printer (which
> will open the printer dialog, so the user can choose which of her/his
> umpteen different printers to use, and thus giving Java the properties of
> that chosen printer) and then draw your text on the virtual page of that
> printer. The process is made all the more interesting by the buggy way in
> which the printer properties are reported to Java: totally wrong! If I query
> the page dimensions of my (well known) HP 4-printer, I get something like 76
> x 0 (the numbers are assumed to be pixels!). So the thing is to find out by
> trial and error how much you can print on a given page. But the bottom line
> is: you can't get away from the graphics.
> Below is a class that I used to find out about printing.
> Cheers,
> Conrad
> -------------------------------- snip -------------------------------------
>
> //Title: Your Product Name
> //Version:
> //Copyright: Copyright (c) 1997
> //Author: Your Name
> //Company: Your Company
> //Description: Your description
> package printpk;
>
> import java.awt.*;
> import java.awt.event.*;
> import java.applet.*;
> import java.util.Properties;
> import borland.jbcl.layout.*;
> import borland.jbcl.control.*;
>
> public class printap extends Applet {
> XYLayout xYLayout1 = new XYLayout();
> boolean isStandalone = false;
> Label label1 = new Label();
> Button button1 = new Button();
> Frame pfrm = new Frame("PrintFrame");
> Font sansn14 = new Font("SansSerif", 0, 14);
> Font sansb14 = new Font("SansSerif", 1, 14);
> Font sansn20 = new Font("SansSerif", 0, 20);
> Font sansb20 = new Font("SansSerif", 1, 20);
> Font serifn14 = new Font("TimesRoman", 0, 14); // Note: IE4 doesn't know
> "serif"
> Font serifb14 = new Font("TimesRoman", 1, 14); // IE4 is rapidly
> approaching
> Font serifn20 = new Font("TimesRoman", 0, 20); // the status of "best
> hated browser"
> Font serifb20 = new Font("TimesRoman", 1, 20); // with me! Damn Microshit!
>
> Font courn14 = new Font("Courier", 0, 14);
> Font courb14 = new Font("Courier", 1, 14);
> Font courn20 = new Font("Courier", 0, 20);
> Font courb20 = new Font("Courier", 1, 20);
> Label label2 = new Label();
> Label label3 = new Label();
>
> //Construct the applet
> public printap() {
> }
>
> //Initialize the applet
> public void init() {
> try { jbInit(); } catch (Exception e) { e.printStackTrace(); }
> }
>
> //Component initialization
> private void jbInit() throws Exception{
> xYLayout1.setWidth(400);
> xYLayout1.setHeight(300);
> button1.setLabel("Print");
> button1.addActionListener(new printap_button1_actionAdapter(this));
> this.setLayout(xYLayout1);
> this.add(label1, new XYConstraints(17, 24, 364, -1));
> this.add(button1, new XYConstraints(172, 265, -1, -1));
> this.add(label2, new XYConstraints(16, 69, 328, -1));
> this.add(label3, new XYConstraints(15, 110, 354, -1));
> PrintJob prjob = getToolkit().getPrintJob(pfrm, "PrintJob",
> (Properties)null);
> label2.setText("Dimension: "+prjob.getPageDimension());
> label3.setText("Resolution: "+prjob.getPageResolution());
> this.requestFocus();
> }
>
> //Start the applet
> public void start() {
> }
>
> //Stop the applet
> public void stop() {
> }
>
> //Destroy the applet
> public void destroy() {
> }
>
> //Main method
> public static void main(String[] args) {
> printap applet = new printap();
> applet.isStandalone = true;
> DecoratedFrame frame = new DecoratedFrame();
> frame.setTitle("Applet Frame");
> frame.add(applet, BorderLayout.CENTER);
> applet.init();
> applet.start();
> frame.pack();
> Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
> frame.setLocation((d.width - frame.getSize().width) / 2, (d.height -
> frame.getSize().height) / 2);
> frame.setVisible(true);
> }
>
> void button1_actionPerformed(ActionEvent e) {
> PrintJob prjob = getToolkit().getPrintJob(pfrm, "PrintJob",
> (Properties)null);
> Graphics pg = prjob.getGraphics();
> String str;
> FontMetrics fm;
> int h, w, y = 40;
> if (pg != null) {
> label1.setText("Printing....");
> pg.setFont(sansn14);
> fm = pg.getFontMetrics(sansn14);
> str = "This is a string printed in SansSerif, normal, 14 Points;
> overall width=";
> w = fm.stringWidth(str+"999");
> pg.drawString(str+w , 10, y);
> h = fm.getHeight();
> pg.setFont(sansb14);
> fm = pg.getFontMetrics(sansb14);
> str = "This is a string printed in SansSerif, bold, 14 Points; overall
> width=";
> w = fm.stringWidth(str+"999");
> pg.drawString(str+w ,10, y=y+2*h);
> h = fm.getHeight();
> pg.setFont(sansn20);
> fm = pg.getFontMetrics(sansn20);
> str = "This is a string printed in SansSerif, normal, 20 Points;
> overall width=";
> w = fm.stringWidth(str+"999");
> pg.drawString(str+w ,10, y=y+2*h);
> h = fm.getHeight();
> pg.setFont(sansb20);
> fm = pg.getFontMetrics(sansb20);
> str = "This is a string printed in SansSerif, bold, 20 Points; overall
> width=";
> w = fm.stringWidth(str+"999");
> pg.drawString(str+w ,10, y=y+2*h);
> h = fm.getHeight();
> pg.setFont(serifn14);
> fm = pg.getFontMetrics(serifn14);
> str = "This is a string printed in Serif, normal, 14 Points; overall
> width=";
> w = fm.stringWidth(str+"999");
> pg.drawString(str+w , 10, y=y+2*h);
> h = fm.getHeight();
> pg.setFont(serifb14);
> fm = pg.getFontMetrics(serifb14);
> str = "This is a string printed in Serif, bold, 14 Points; overall
> width=";
> w = fm.stringWidth(str+"999");
> pg.drawString(str+w ,10, y=y+2*h);
> h = fm.getHeight();
> pg.setFont(serifn20);
> fm = pg.getFontMetrics(serifn20);
> str = "This is a string printed in Serif, normal, 20 Points; overall
> width=";
> w = fm.stringWidth(str+"999");
> pg.drawString(str+w ,10, y=y+2*h);
> h = fm.getHeight();
> pg.setFont(serifb20);
> fm = pg.getFontMetrics(serifb20);
> str = "This is a string printed in Serif, bold, 20 Points; overall
> width=";
> w = fm.stringWidth(str+"999");
> pg.drawString(str+w ,10, y=y+2*h);
> h = fm.getHeight();
> pg.setFont(courn14);
> fm = pg.getFontMetrics(courn14);
> str = "This is a string printed in Courier, normal, 14 Points; overall
> width=";
> w = fm.stringWidth(str+"999");
> pg.drawString(str+w , 10, y=y+2*h);
> h = fm.getHeight();
> pg.setFont(courb14);
> fm = pg.getFontMetrics(courb14);
> str = "This is a string printed in Courier, bold, 14 Points; overall
> width=";
> w = fm.stringWidth(str+"999");
> pg.drawString(str+w ,10, y=y+2*h);
> h = fm.getHeight();
> pg.setFont(courn20);
> fm = pg.getFontMetrics(courn20);
> str = "This is a string printed in Courier, normal, 20 Points; overall
> width=";
> w = fm.stringWidth(str+"999");
> pg.drawString(str+w ,10, y=y+2*h);
> h = fm.getHeight();
> pg.setFont(courb20);
> fm = pg.getFontMetrics(courb20);
> str = "This is a string printed in Courier, bold, 20 Points; overall
> width=";
> w = fm.stringWidth(str+"999");
> pg.drawString(str+w ,10, y=y+2*h);
> pg.dispose(); // Note: this is a form feed!!!!!!! One could do a
> second page afterwards
> prjob.end(); // now things get sent to the printer - job done.
> }
> }
>
> }
>
> class printap_button1_actionAdapter implements java.awt.event.ActionListener
> {
> printap adaptee;
>
> printap_button1_actionAdapter(printap adaptee) {
> this.adaptee = adaptee;
> }
>
> public void actionPerformed(ActionEvent e) {
> adaptee.button1_actionPerformed(e);
> }
> }
>
> ------------------------------- snap
> ---------------------------------------------
>
> Jennifer Merkel wrote:
>
> > Can anyone give me some help? I am trying to design some reports to be
> > printed out. I can write the report information to a file fine. How do
> > I send it to the printer? I know there are some callses in Java that
> > have to do with printing but most of them look like they deal with
> > graphics, I just want to print out text to the printer.
> >
> > Jennifer
Technical Details
Message-ID:
<35129627.6CEB9CE8@edmundsassoc.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<3511813C.A18DAD08@edmundsassoc.com> <35119157.AFC957DF@csi.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f74b3,7f9a5d5e1be5afb7 X-Google-Attributes: gidf74b3,public From: Jennifer Merkel <JenniferM@edmundsassoc.com> Subject: Re: Printing Reports Date: 1998/03/20 Message-ID: <35129627.6CEB9CE8@edmundsassoc.com> X-Deja-AN: 335982151 Content-Transfer-Encoding: 7bit References: <3511813C.A18DAD08@edmundsassoc.com> <35119157.AFC957DF@csi.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Organization: Another Netscape Collabra Server User Newsgroups: borland.public.jbuilder.java.language