Java print barcode labels - java

Can someone point in the right direction for printing barcode labels using Java? I can use the barbecue library (http://barbecue.sourceforge.net/) to generate them bar codes as images, but I need a way to put the image (and human readable caption) into an Avery document template for printing.

The iText library supports pretty much every kind of barcode imaginable. You can generate pdfs and either save them or print them internally.

I suggest using the barcode4j library instead of barbecue for 2 reasons:
Barbecue barcode objects are unnecessarily coupled to Java UI components (e.g. Barcode class extends JComponent). This creates unnecessary dependencies if the Java UI is not being used, e.g. for batch or command line based applications. They should have used aggregation rather than inheritance if they wanted to use their barcode classes with the Java UI.
Barcode4J looks like it is currently supported - version 2.0 released and copyright date is 2012
Then you have the problem of translating the barcode into a format that your printer understands. For this I suggest openlabelprint.org (which I wrote!) - it's another open source project that uses barcode4j and provides:
facilities to define a label layout using SVG (Scalable Vector Graphics - an open w3c standard) and
rasterization to a bitmap of the SVG from barcode4j (and the surrounding label layout in SVG) (openlabelprint applies the excellent Apache SVG Batik Java libraries for rasterization as well as for other SVG tasks)
printing of the bitmap on Zebra printers using their ZPL low level language. openlabelprint has a built in utility to convert png bitmaps to ZPL and send this to the Zebra printer via the standard Java printer system. Also openlabelprint provides APIs to extend it for other printer languages though ZPL is supported by some non-Zebra brands

I'm printing bar codes using java but I'm using a printer which have a pre-programmed function for printing bar codes. So I'm only telling the printer what codes to print and it does the rest. If you willing to pay for a printer it might saves you some time.
This may or may not be useful to you, but I thought i'd mention it.

I think you will have to measure your Avery label page with a ruler and then in your Java code, you will have to create a full Letter/A4/whatever page to print and offset your barcode image on that page to the appropriate location based on your measurements with the ruler.

Have you tried printing the image that you got from "barbecue" ?

You should try JZebra this is an applet and a good start point for you, take a look at the java source code.
http://code.google.com/p/jzebra/
For zebra you this simple guide will help you.
On this Zebra commands
N
q609
Q203,26
B26,26,0,UA0,2,2,152,B,"777777"
A253,56,0,3,1,1,N,"JHON3:16"
A253,26,0,3,1,1,N,"JESUSLOVESYOU"
A253,86,0,3,1,1,N,"TEST TEST TEST"
A253,116,0,3,1,1,N,"ANOTHER TEST"
A253,146,0,3,1,1,N,"SOME LETTERS"
P1,1
on JZebra
var applet = document.jzebra;
if (applet != null) {
applet.append("N\n");
applet.append("q609\n");
applet.append("Q203,26\n");
applet.append("B26,26,0,UA0,2,2,152,B,\"777777\"\n");
applet.append("A253,56,0,3,1,1,N,\"JHON3:16\"\n");
applet.append("A253,26,0,3,1,1,N,\"JESUSLOVESYOU\"\n");
applet.append("A253,86,0,3,1,1,N,\"TEST TEST TEST\"\n");
applet.append("A253,116,0,3,1,1,N,\"ANOTHER TEST\"\n");
applet.append("A253,146,0,3,1,1,N,\"SOME LETTERS\"\n");
applet.append("P1,1\n");}
Having clear this:
EPL is one command per line. A command starts out with a command identifier, typically a letter, followed by a comma-separated list of parameters specific to that command. You can look up each of these commands in the EPL2 programming documentation. Here’s an English-language version of the commands in the above example.
Sending an initial newline guarantees that any previous borked
command is submitted.
[N] Clear the image buffer. This is an important step and
generally should be the first command in any EPL document;
who knows what state the previous job left the printer in.
[q] Set the label width to 609 dots (3 inch label x 203 dpi
= 609 dots wide).
[Q] Set the label height to 203 dots (1 inch label) with a 26
dot gap between the labels. (The printer will probably auto-
sense, but this doesn't hurt.)
[B] Draw a UPC-A barcode with value "777777" at
x = 26 dots (1/8 in), y = 26 dots (1/8 in) with a narrow bar
width of 2 dots and make it 152 dots (3/4 in) high. (The
origin of the label coordinate system is the top left corner
of the label.)
[A] Draw the text "JESUSLOVESYOU" at
x = 253 dots (3/4 in), y = 26 dots (1/8 in) in
printer font "3", normal horizontal and vertical scaling,
and no fancy white-on-black effect.
All tha A starting lines are similar.
10. [P] Print one copy of one label.

Related

LibGDX RTL font rendering?

I am creating an application that should support two languages, English, and Hebrew.
The problem is that Hebrew is Right-To-Left western language and English is Left-To-Right language, and LibGDX does not support RTL fonts.
I have created the bitmap for the font and everything works
But when I write in hebrew, it will write the words reversed. I have a solution for this when I write solely in hebrew, just reverse the words using a StringBuilder, but that's a cheap-fix. But what if I want to implemnet a chat, or character name?
From what I can see the easiest solution is to use Heiro. If you look at this thread Hiero Rendering Arabic fonts Right to Left where there is recent provision to accomodate RTL
From there it becomes increasingly difficult. There are quite a few questions about this issue (one example Showing non-western language from right to left in libgdx (Android)) and fewer solutions.
You have the option of creating a library of glyphs of strings for commonly used words or expression, though this is a painstaking process to set up and there is an overhead in terms of time when using chat, as there is with your string reversal.
This discussion in the libgdx github Support for complex font rendering (Chinese, Arabic, ...). goes into these and more options including work done to support Windows sridharsundaram/complexscriptlayout, which, although that is not Android, may be worth investigating for further development ideas.
On the positive side, there are an increasing number of recent developments in this front, so RTL and bidi formats should become increasingly easier for developers using libgdx.
Of interest is this support issue Right-To-Left Text Rendering Support #787 as there are breadcrumb trails of people with the same issue developing resources.
As of right now, there really isn't a way to render Right to Left text, as shown by this thread about it. So the only way to really do it is to reverse the text with StringBuilder, and then display that. A more efficient way to render the reversed text is to create a method that will display the text accordingly, so you don't have to reverse it every time you try to write Right to Left text. If you create a method, you will be able to implement the RTL text into chats, names, or other graphics that require RTL fonts.
I also recommend converting your Bitmap to a .ttf file so that it is easy to use your custom font while also keeping a good quality. You can then use the FreeTypeFontGenerator to render your font nicely. If you cannot convert your Bitmap to a font you could also use your method of displaying text in the below method. A really good alternative is the Hiero library. You can select the RTL text check box.
Here is an example of a method that you could create to render the text (using the FreeTypeFontGenerator):
// Keep the generator here so that it is not created each time
FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("fontFile.ttf"));
public void renderRTL(float x, float y, int fontSize, String text) {
batch.begin(); // Lets you draw on the screen
// Reverses the text given
StringBuilder builder = new StringBuilder();
builder.append(text);
builder.reverse();
String outputText = builder.toString();
// Creates the font
FreeTypeFontGenerator.FreeTypeFontParameter parameter = new FreeTypeFontGenerator.FreeTypeFontParameter();
parameter.size = fontSize;
parameter.characters = "ALL HEBREW CHARACTERS"; // Put all your Hebrew characters in this String
scoreFont = generator.generateFont(parameter);
scoreFont.setColor(Color.BLACK);
scoreFont.draw(batch, outputText, x, y); // Actually draws the text
generator.dispose(); // Clears memory
batch.end();
}
Make sure to add all of these dependencies into your build.gradle file (in the dependencies section):
compile "com.badlogicgames.gdx:gdx-freetype:$gdxVersion"
natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-armeabi"
natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-armeabi-v7a"
natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-x86"
You could optionally add a color parameter (in RGBA format) to your method for more functionality. Hope it helps!
Use this solution on github :
https://github.com/ultra-deep/libgdx-rtl-support

How to count color pages in a PDF/Word doc using Java

I am looking to develop a desktop application using Java to count the number of colored pages in a PDF or Word file. This will be used as part of an overall system to help calculate the cost of printing a document in terms of how many pages there are (color/B&W).
Ideally, the user of the application would use a file dialog to select the desired PRF/Word file, the application could then count and output the number of colored pages, allowing the system to automatically calculate document cost accordingly.
i.e
if A4 colored pages cost 50c per page to print,
and B&W cost 10c per page,
calculate the total cost of the document per colored/B&W pages.
I am aware of the existing software Rapid PDF Count http://www.traction-software.co.uk/rapidpdfcount/, but would be unsuitable as part on integration into a new system. I have also tried using GhostScript/Python as per this solution: http://root42.blogspot.de/2012/10/counting-color-pages-in-pdf-files.html, however this takes too long (5mins to count a 100 page pdf), and would be difficult to implement into a desktop app.
Is there any method of counting the number of colored pages in a PDF or Word file using Java (or alternative language)
Thanks
Although it might sound easy, the task is rather complicated.
One option would be to use a program such as iText to walk every single token in the PDF, look for tokens that support color and compare that to your definition of "black". However, this will only get you basic text and drawing commands. Images are a completely different beast so you'll probably need to find an image parser or grab a copy of each spec and then walk each of those.
One of the downsides of token walking is you need to properly handle tokens that reference other things and further walk those tokens.
Another downside is that things can overlap each other so you'd probably want be aware of their coordinates, z-index, transparency and such.
There will be many more bumps in the road but that's a good start. What's most interesting is that if you accomplish this, you'll actually have found that you've partially built a PDF renderer!
Next, you'll need to define "black". Off the top of my head there's RGB black, CMYK black, Grey black and maybe Lab black along with some Pantones. That shouldn't be too hard but if I were to build this I'd want to know "blank ink usage" which could also be shades of grey. There's also "rich blank" that you might need to deal with, too!
So, all that said, I think that the GhostScript option you found is really the best bet. It literally renders the PDF and calculates the ink coverage from an RGB standpoint. You still should handle grey's, too, but that shouldn't be too hard, here's a good starting point.
Wanting to know what the click-charge is going to be is a pretty common problem, but it's not easy to solve at all. As already indicated by the answer Chris Haas gave, but I want to put another spin on it.
First of all, you have to wonder whether you really want to support both Word and PDF documents. Analysing Word files is less useful than you might think because that Word file is probably going to be converted into something else before it's going to be printed. And because of the fact that you're starting from Word, the chance that your nice RGB black text in Word gets converted to less-than-perfect 4 color black in PDF is very high. In other words, even though you might count a page of black text in Word as a 'cheap' page, it might turn into an expensive color page after conversion from Word to something that can be printed.
Let's consider the PDF case then. PDF supports a whole host of color spaces (gray, RGB, CMYK, the same with an ICC Profile attached, spot color and a few multi-spot color variants, CalGray and CalRGB and Lab. Besides that there is a whole range of very tricky features such as transparency, overprint, shades, images, masks... that you all have to take into account. The only truly good way to calculate what you need is to do essentially the same work as your printer will do; convert the PDF into one image per page and examine the pixels.
Because of what you want to do, the best way to progress would be to:
1) Convert any word files into PDF
2) Convert any PDF files into CMYK
3) Render each page of that CMYK file into an image.
Once you've done that you can examine the image and see whether you have any colors left. There are a number of potential technologies you can use for this. GhostScript is definitely one, but there are commercial solutions too that would certainly be more expensive but potentially faster.

Dot matrix fast printing

I'd like to know, what practical way of printing on EPSON Dot Matrix printers in Java. I'm having the LX300+II model to play, with USB connectivity.
I searched too much on internet but all the codes available for network and serial(port LPT) printers.
like : https://code.google.com/p/escprinter/, http://devpath.blogspot.in/2008/01/java-como-imprimir-na-epson-lx-300-via.html
I tried with Java print service 2D Graphics, it works but taking time.
I know we have two ways of using this printer:
By directly raw ASCII data to printer
By Graphical printing, with graphical fonts and precise positioning.
How can I use both fast printing fonts (provided by 1) and precise positioning (provided by 2)? Any code or API?
There is a third-party API, RTextPrinter which seems to support the following Dot Matrix printer command sets:
Epson ESCP and ESCP2 or compatible
HP-PCL5 or compatible
HP-PCL 3 (known also as Laset Jet Plus Emulation)
IBM Proprinter or compatible
IBM PPDS
Diablo
Plain (no features)
It supports the following features as listed in its details page:
Font selection (courier ...)
Font style (bold, italic and underlined)
Font size (characters per inch)
Lines (vertical and horizontal) and rectangles
Character set managing and charater value mapping
Other: subscripts and superscripts, double wide, landscape format and interline spacing
However, this is not a free API and you will have to purchase a license to use it. You can try a demo to see if it satisfies your requirement though.
Hope this helps.

Optimizing jar file size (images) when developing java client supporting multiple languages

I am developing the Java client which should support several languages. For translation of the text I use Java ResourceBoundle and it works okay.
Now the problem is with images. The client should load around 50 images which are specific cards for a board game. Each image has a title. So if I have N languages I should prepare 50*N images and put then into the jar file.
Each language support would add around 1 Mb to the size of jar file.
Do you think I should
Generate jar which support all the languages?
Generate many jars which would support English and a local language?
Have one set of image without titles and attach title to the image using Java JLabel?
I advise another option altogether.
Have a set of images without text, and use AWT's Graphics2D to add the text in the necessary language (as opposed to doing so with a JLabel).
Graphics2D g = imageBase.createGraphics();
// color, font, etc settings
g.drawString("title", 0, 0);
g.dispose();
You can modify the parameters of g.drawString as necessary to draw the correct title at the correct coordinates. If you wish to center the title, you can find a nice tutorial here.

iText PDF colors are inconsistent in Acrobat

I'm generating a multipage PDF from Java using iText. Problem: the lines on my charts shift color between certain pages.
Here's a screenshot of the transition between pages:
This was taken from Adobe Reader. The lines are the correct color in OS X Preview.app.
In Reader the top is #73C352, the bottom is #35FF69. In Preview.app the line is #00FE7E.
Any thoughts on what could be causing this discrepancy? I saved the PDF from Preview.app and opened it in Adobe Reader, still has the colors off.
Here is the PDF that is having trouble. Open it in Adobe Reader and look at the transition between pages 11 & 12.
On checking this out further, it appears that the java.awt.print.PrinterJob is calling print() for each pageIndex twice. This might be a clue.
The problem with the pages with darker colors is that they include a pattern object with a transparent image. When transparency is involved, Adobe Acrobat switches automatically to a custom CMYK profile and this causes the darker colors. Only Acrobat does this, other viewers behave just fine. The solution is either to remove the pattern object with the transparent image (it seems to be a drawing artifact of the PDF generator engine, it is not used anywhere on the page) or you can make the page part of a transparency group and specify the transparency group to use RGB colorspace.
Several different possibilities, yes.
Different color matching. If you're using a "calibrated" color space on one page and a "device" color space on another, the same RGB/CMYK values can produce visually different values.
If the graph is inside a Form XObject, the same graph can appear differently depending on the current graphic state when the form is drawn.
If you could post a link to your PDF, I could probably give you a specific answer.
Ouch. That PDF is painful to shclep through. I'd like to have some words with whoever wrote their PDF converter. Harsh ones. Lots of unnecessary clipping ("text" is being clipped hither and yon, page 7 for example), poor use of patters for images, but not using patters when it would actually help, drawing text as paths, and on and on...
EDIT: Which is precisely the sort of stuff you see when rendering Java UI via a PdfGraphics2D object. You CAN keep the text as text though. It's just a matter of how you create the PdfGraphics2D instance.
Okay, so the color of the line itself is identical. 0 1 0.4 RG. HOWEVER, there is some "transparency stuff" going on.
On pages that have images with soft masks or extended graphic states that change the transparency, the green line appears darker. On pages without, it appears brighter.
I suspect that all those other PDF viewers that draw the lines consistently don't support transparency at all, or only poorly.

Categories

Resources