aspose file tools*
The moose likes Java in General and the fly likes Printing multiple .png files in the same print job Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Printing multiple .png files in the same print job" Watch "Printing multiple .png files in the same print job" New topic
Author

Printing multiple .png files in the same print job

Jesse Kelm
Greenhorn

Joined: Sep 06, 2011
Posts: 11
I am trying to take a list of .png files in a directory and print them all in the same print job. I am able to do this correctly for jobs that have around 80 pages in them by loading each image into a BufferedImage and placing them into an ArrayList, and then sending the ArrayList to the print method I will paste below. The problem is when there are more then 80 pages, when trying to load all of the images into this ArrayList, we start to receive Out Of Memory errors due to the amount of memory the images take up when loading them. Does anyone have any suggestions on a better way to do this? Thanks in advance!!

Here is the code that loads the images into an ArrayList and provides out of memory error messages once we load too many .png files in to the images ArrayList



Here is the print method:

Bill Clar
Ranch Hand

Joined: Sep 21, 2006
Posts: 140

I ran into this very problem. I built a GUI to print any number of TIF images and it would run out of memory around the 100 image mark.

I did two things to correct the problem:

1) I increased the maximum heap size to 1 GB.

2) I created a separate thread for every batch of 50 images. Each thread would make an independent call to PrintJob. This allowed me to garbage collect some of the BufferedImage objects
and reclaim some memory.



Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3435
    
  47

I haven't ever tried printing from Java, so I may be missing something basic, but why don't you render every image only when it is about to be printed and then throw it away?
Jesse Kelm
Greenhorn

Joined: Sep 06, 2011
Posts: 11
Thanks Bill..I will give that a shot. You couldn't find any way to send the images one by one through the print routine either instead of loading them all into an arraylist first?

Martin..

That is ideally what I am trying to do, I just can't seem to determine how to print the multiple images one by one in the same print job. It doesn't appear that Java lets you do that. You have to supply the print routine all of the image data up front for it to work through all of the objects.
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3435
    
  47

There is this line (number 85) in your code which takes a rendered image from your list:

Why couldn't you store just filenames in the list, and load the image at this very point (and throw it away as soon as it is printed)?
Jesse Kelm
Greenhorn

Joined: Sep 06, 2011
Posts: 11
I tried to do exactly that in the method, but when it runs, the pageIndex variable is automatically set somewhere and is used extensively. It is reading how many pages is sent to it from the collection and loops through it. It it only has the first rendered image, it treats the pageIndex as 1 and provides an out of index error message.

I am still trying to resolve this if you have any other suggestions or can suggest a way to do exactly this.


public int print(Graphics g, PageFormat f, int pageIndex)
Bill Clar
Ranch Hand

Joined: Sep 21, 2006
Posts: 140

You can render one image at a time as it's to be printed, but the downside is that you're sending an individual PrintJob for each image.

Depending on your printer speed, this may cause a slowdown. For my GUI, it resulted in a 2-3 second gap between each page print. The users were not happy.

Give it a try and see what happens.

Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3435
    
  47

Jesse, do not put images in the collection. Rather, put the file names there, and in the print method render the image which should go to the given page. The filenames are known beforehand and the collection will thus contain all of them right from the start, hence the page will always be found (unless there is an error rendering the image, but that is another concern).

This can certainly introduce gaps in processing Bill mentioned. If that was a problem, you might perhaps create a bounded queue of, say, 5 items for rendered images and render the images in a separate thread. Some coordination between the printing thread and the rendering thread might be needed to satisfy the java.awt.print.Printable page ordering contract but - at the first blush - it should be doable.
Jesse Kelm
Greenhorn

Joined: Sep 06, 2011
Posts: 11
This worked out perfectly and without the delay that Bill described.

What I ended up doing was passing the ArrayList of file names to the print routine, thus the pageIndex had the correct amount of pages required. I then loaded each file into a BufferedImage, ran that through the RenderedImage process and everything is working correctly.

Thanks very much to Bill and Martin!!!

Jesse
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Printing multiple .png files in the same print job
 
Similar Threads
Help!!! Image print problem
Help!!! Image print problem
Help!!! Image print problem
Help!!! Image print problem
How to print an RenderedImage?