• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Printing multiple .png files in the same print job

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:

 
Ranch Hand
Posts: 163
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.



 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 Vashko
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 163
Eclipse IDE Tomcat Server Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 Vashko
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic