I'm writing an application which utilises tables which can be printed if the user so desires and I wish to print a JTable filled with data, except I haven't been able to find an option to remove the formatting; the printed tables looks like it does in the GUI (based on the system theme) which is making the table less readable and using excess ink.
I wish to print the same data with clear formatting. Is there a way to do this straight from a JTable or is my best option simply to print to a file and have the use printer from there. Currently it functions through a viewer which gives the user some options for printing, and then it goes to the system's printer.
Since 1.5, "Simple new print() methods allow for quick and easy addition of printing support to your application."
Related
I have been working on a java console application and I've got a lot of data to print in a tabular way,
and I am wondering if there is any good way to do it instead of hard coding it which sometimes doesn't work for some data according to the data size.
Need to print a table like this one
enter image description here
I have some string data that needs to be printed inside a table in java.
Simply formatting the text is not enough, I need to actually draw the lines of the table, have a title, header, and footer.
From my searches I found that java.swing.JTable would fulfill my conditions, however I do not need to show the table to the user at any point nor do I need to save the table in a file. I only need to print said table via a printer and JTable seems overkill for such a task, not to mention rather complicated.
Is there a better alternative for this task?
Edit:
Yes, I need to print this to a Printer device.
The table is fairly simple, this is an example of it.
Table to output example
I'm starting to do anything like this, so I need a little bit of help to start with.
I want to print some data and give it a format to print it as an establishment ticket, so what would be the best way?
Using the java api and create the service by myself?
http://docs.oracle.com/javase/tutorial/2d/printing/index.html
Or does it exist any free library to this purpose?
Maybe better creating a PDF with the data before printing?
I've been messing around with PDFbox API but I don't like it very much.
I don't know what printer will be used so, do ticket printers use a standard size or exists differents ticket sizes?
I also need to show a preview before printing it (I'm using Swing).
There's no need to store the formatted data.
Thanks in advance.
If You decide to generate pdf file and then print it, I propose to use Apache FOP. I always use this library to generate PDF. This library is not easy but offers many options.
Second option - You could generate HTML page and then print it.
From a real live example: we're running a project that prints despatch labels to send a package with a parcel service. The label contains barcodes and other textual information as well as a logo.
Usually a ticket printer understands a special printer control language, i.e. if it's a Zebra printer it understands EPL or ZPL-code. To produce a ticket we simply send the whole control code over a simple socket connection to the printer. The control code itself is created through a template engine (Velocity in our case).
Of course there are different printers for different requirements - it may depend on what actually should be printed. So depending on the printer you may need to create a different set of template to produce your label:
https://en.wikipedia.org/wiki/Printer_control_language
By the way:
Usually the printer comes with software to let you design the layout of the paper label (which makes life really easy). We used this software to create the initial layout. The control code itself contains all commands used to configure the printer (label size, etc - no doubt there are plenty of different formats and sizes, as said: it really depends on the use case).
when printing with Java, one can select the media tray (within PrintRequestAttributeSet). Then one can pass this setting to a printjob and have one's document printed to the given tray.
My question is now: can I somehow specify that the first page is printed to one tray and the second one to another tray within one print job?
I'm reluctant to creating two separate print jobs, because my usage scenario is a mass-print, of say 1000 documents. Each document has some pages going to tray 1 and some pages going to tray 2.
If I have to create a new print job on each switch of trays, I would end up with several thousands of separate print jobs and I'm afraid of all sorts of print-spooler overruns and system crashes. Thus my preference to somehow sneak those "switches of tray" into one print job.
I'm pretty sure that it can be sone somehow, but didn't succeed so far.
I thought about creating those thousands of PrinterJobs, but having them print to a StreamPrintService (instead of an actual print service), thus capturing the switches of tray along with the actual printing data. Then I was planning to concatenate the results of those single "virtual" prints and send it all to a real printer in one real print job.
However, with java 1.6 there seems to be only one StreamPrintService, which can only output postscript.
So: is there a way to capture the raw, native output from a native printer driver (using java)? Does it seem practical to you to concatenate that output and send it to the printer, in order to solve my problem?
I would be glad, also about comments regarding only parts of the problem..
By adding a "Destination" attribute to one's print attribute set, the print can be redirected to a local file. That file contains the printjob in whatever language the actual printer's driver uses to talk in.
In my case, I ended up with postscript files.
I created two postscript files, each printing from a different tray and then send their concatenation to the printer. And it worked :-) ! I switched of the printer to verify that there is only one printjob and I wrote some numbers on the paper in the those trays. So I guess I can be sure that it's not only wishful thinking ;-).
However, I think I won't pursue this topic in depth, because
I'm not a printing guru and have my doubts that this approach works every case.
For our customer, the whole thing is a nice-to-have at this time, so there won't be a budget to reasearch further on the circumstances under which this little idea works
I want to list options in drop down menu in HTML form but i need to make some options to be multiline .and the options i need to read them from text file.any ideas how to do that?
In order to have your HTML display values from a text file, you're going to need to use some kind of server-side generation (ASP.NET, JSP, CGI, whatever). The exact solution will depend on what server-side technology you use/choose, but in all cases it should be fairly straightforward as this is a common requirement for all of these technologies.
As for your first part, what exactly do you mean by "some options to be multiline"? Do you mean that some values are quite long and have spaces, so you want them to wrap? Do you want to have some kind of drop-down box that shows multiple values at once? Do you want them not to be a dropdown at all? How will you decide between the values that should be multiline and those that should not? If you can express your intention more clearly (which may involve some additional thinking on your end), it will be possible to help, and you may even end up working out the solution yourself.