2
votes

I want to print plain text (probably cp1252 or whatever Windows is using) to a particular printing service. Sounds easy, right? But success seems to depend strongly on the type of printer you're using.

I have three different printers available. One is a HP LaserJet (a regular printer), one is Free PDF (a virtual PDF printer) and one is Tobit Faxware (a virtual faxing service - this is what i want to print to).

According to the supported doc flavors none offers "text/plain" or any other "text" format.

When i print using DocFlavor.URL.AUTOSENSE the HP printer will print the text properly, but the other two will fail silently. Since the "text" DocFlavors are not supported, i cannot give the printers any more hints other than "autosense".

When using Windows' Notepad to print the file, it works fine for all three printers. The difference seems to be that i'm printing "RAW" data, while notepad uses "NT EMF 1.008".

Is there any way for me to make it work using a java PrintService?

2

2 Answers

0
votes

Is there any way for me to make it work using a java PrintService?

Sure. Here's Oracle's write up on the Java Print Service.

I've used the older AWT print service. With the AWT print service, you have to convert the text to an image, and print the image.

0
votes

When using Windows' Notepad [NT EMF 1.008] [...] it works fine for all three printers

The FreeHEP offers EMF output format for Java: http://java.freehep.org/, which is a binary file created according to the Enhanced Metafile Format Driver.

public void write(EMFOutputStream emf) throws IOException {
    emf.writePOINTL(pos);
    emf.writeDWORD(string.length());
    emf.writeDWORD(8 + 28 + 40); // TagHeader + ExtTextOutA + Text
    emf.writeDWORD(options);
    emf.writeRECTL(bounds);
    int pad = (string.length()) % 4;
    if (pad > 0)
        pad = 4 - pad;
    emf.writeDWORD(8 + 28 + 40 + string.length() + pad); // offset to
                                                         // character
                                                     // spacing array
    emf.writeBYTE(string.getBytes());
    for (int i = 0; i < pad; i++)
        emf.writeBYTE(0);
    for (int i = 0; i < string.length(); i++)
        emf.writeDWORD(widths[i]);
}

Sample borrowed from programcreek.com