• Post Reply Bookmark Topic Watch Topic
  • New Topic

send screen capture to printer  RSS feed

 
Jack Lunn
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Friends
My first contribution.....
I need to send screen capture to printer
I have found code to capture screen.



I have found code to choose printer.and to print.


This uses drawstring which is wrong. I tried using drawimage but got bogged down.

I think I just need a couple of tweeks to solve this but I've been on it too long already

All help greatly appreciated
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Besides strings, the Graphics also allows you to draw images

Also since you already have the screen capture as a file, you can use the Desktop class via the print(File file); method
 
Jack Lunn
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to choose printer.
How can I add a to The code below seems to flash a dialog and then just uses default printing options
 
Paul Clapham
Sheriff
Posts: 22844
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Simple version: Change the default printer (using the Windows tools available). Then print to the default printer.

Complex version: Learn about Java printing (start with this tutorial). That's going to be a lot of work but it might be justified, depending on how often you need to do this and whether other less technically-oriented people are going to use your code, and so on. So it's up to you how nice of a product you want to produce.

 
Jack Lunn
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I really wish it was that easy.
This requirement is to add a print and an email button to several screens in and existing java project - not written by me. Also Java is new to me although I've been programming for over 30 years.
I put the buttons on and got the email working pretty easily using Robot, BufferedImage and write jpg to disc.
Then attach disc file to email.

I've been on the print for longer than I care to admit.
This program may be used on different platforms with different default printers
So changing default printer in Windows is not suitable.

I find it difficult to believe that it's not possible to add something like a printDialog() just before desktop.print!!!
As I said, a dialog does flash for a second using the code above, just before it chooses defaults and disappears.

I've spent hours wading through the Java Tutorials as well as Google pages without getting anything satisfactory yet.

I don't think the page really needs to be written to disc. It just works for the email! and this might be the easiest solution for me.
If you've got an hour to kill, type in Print BufferedImage to Google, It's an absolute maze unless you're a good Java programmer.

I just need to grab the screen, maybe save it to disc if necessary, choose a printer then print it.  In Java!
 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You really need to look at the tutorial Paul Clapham gave you, but here is some code that will pop up a choose printer dialog.  This code is not plug and play; you need to read the tutorial.
 
Jack Lunn
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Firstly I want to thank you for your help.
Secondly.....
I don't know whether you have access to my earlier posts but if you do you will see that I understand how to use a print dialog.

Believe me, I have trawled through the suggested tutorials as well as google for 3 days now, but nowhere have I found a solution.
Twice now I have been advised to check Java Tutorial on Printing but cannot find the answer.
That doesn't mean there isn't a solution, it just means I can't find it.

Again.....
1. Grab the screen (maybe save it if necessary)
2. Choose a printer
3. Print it.
I have used the above facilities to solve all 3 of these individually but cannot find out how to put them all together in one place.
Does anyone know where in the tutorial I will find my answer or does anyone have any code I can use.
Java must be able to do this as I know I've done it in other languages.

We don't just give up guys do we.
.
.
.
 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you post an attempt to put it all together?  (You posted two separate chunks of code previously).  What problems are you running into putting them together?
 
Paul Clapham
Sheriff
Posts: 22844
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, well, just because you think there should be a simple solution, that doesn't mean there is one.

If you just take the image file you have and try to write it to a printer, you aren't going to be happy because it isn't going to fit into the page margins the way you'd like. So you're at least going to have to find out what the page margins are and then modify the image to conform to those margins. I would start by getting the Graphics2D object which you get from the "public int print(Graphics g, PageFormat pf, int page)" method and call one of its drawImage() methods. You'd have to use an AffineTransform to scale the image to the page margins, which I suppose come from the PageFormat object. You might also have to rotate the image if the user chose Landscape mode on the print dialog, or maybe not. It's a very long time since I worked with this sort of code and even then I never had to output images, so my experience is both dated and limited.
 
Paul Clapham
Sheriff
Posts: 22844
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And also, the tutorial which Maneesh linked to contains useful information about how to output an image to a graphics device. Just the sample code from that plus what you've already done with the printer dialog code should at least get image data to paper. From then on you need to write code to scale the image properly, I think.
 
Jack Lunn
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It just seems a long-winded way of doing this considering one of my earlier attempts using
almost solved it with one short line. This line also popped up the print dialog - brilliant!
Of course it just printed the table and I need the page (or maybe the container) which contains the table.
My first attempt was something like but I never got this to work.

Just that simple line printed the table!!! No orientation, magnification or scaling issues, just the table centered in the page, and the dialog.
I admit I was using the Microsoft XPS document writer - print to file which I'm guessing sends to a file what it would send to a printer.

You can understand why I thought that if that simple line is all that is needed to print a table, just a tweek should print what's around it as well.
That was 3 days ago.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!