Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6917341
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:43:06+00:00 2026-05-27T09:43:06+00:00

I have the problem that when you are in windows and you try to

  • 0

I have the problem that when you are in windows and you try to print through JAVA you can only use the AUTOSENSE property.
However my string that I want to get printed is in greek => UTF-8. When I turn the AUTOSENSE to TEXT_PLAIN_UTF8 i get an: sun.print.PrintJobFlavorException: invalid flavor exception….

Any suggestions? Or other way of printing in Unicode?
thanks!

    String datastr = "UNICODE STRING";
    byte[] databa = null;
    try {
        databa = datastr.getBytes("UTF8");
    } catch (UnsupportedEncodingException e1) {
        e1.printStackTrace();
    }

    DocFlavor docFlavor = DocFlavor.BYTE_ARRAY.TEXT_PLAIN_UTF_16;
    PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
    PrintService service = PrintServiceLookup.lookupDefaultPrintService();

    if (databa != null) {
        DocPrintJob pjob = service.createPrintJob();
        Doc doc = new SimpleDoc(databa, docFlavor, null);
        try {
            pjob.print(doc, aset);
        } catch (PrintException e) {
            e.printStackTrace();
        }

if i try to print it in STRING.TEXT_PLAIN and also in everything else than AUTOSENSE, i get this:

sun.print.PrintJobFlavorException: invalid flavor
    at sun.print.Win32PrintJob.print(Unknown Source)

Finally the supported Flavors are these…

Win32 Printer : HP Deskjet 5440 Series Flavors:
    image/gif; class="[B"
    image/gif; class="java.io.InputStream"
    image/gif; class="java.net.URL"
    image/jpeg; class="[B"
    image/jpeg; class="java.io.InputStream"
    image/jpeg; class="java.net.URL"
    image/png; class="[B"
    image/png; class="java.io.InputStream"
    image/png; class="java.net.URL"
    application/x-java-jvm-local-objectref; class="java.awt.print.Pageable"
    application/x-java-jvm-local-objectref; class="java.awt.print.Printable"
    application/octet-stream; class="[B"
    application/octet-stream; class="java.net.URL"
    application/octet-stream; class="java.io.InputStream"
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T09:43:07+00:00Added an answer on May 27, 2026 at 9:43 am

    Its easier to do it with SWT here is the code…

    package printer;
    
    import org.eclipse.swt.SWT;
    import org.eclipse.swt.graphics.Font;
    import org.eclipse.swt.graphics.GC;
    import org.eclipse.swt.graphics.Point;
    import org.eclipse.swt.graphics.Rectangle;
    import org.eclipse.swt.printing.Printer;
    import org.eclipse.swt.printing.PrinterData;
    
    public class TextPrinter {
    
        Printer printer;
        GC gc;
        int lineHeight = 0;
        int tabWidth = 0;
        int leftMargin;
        int rightMargin;
        int topMargin, bottomMargin;
        int x;
        int y;
        int index;
        int end;
        String tabs;
        StringBuffer wordBuffer;
    
        public TextPrinter() {
        }
    
        public void printString(final String textToPrint) {
    
            PrinterData data = Printer.getDefaultPrinterData();
            printer = new Printer(data);
            Thread printingThread = new Thread("Printing") {
                @Override
                public void run() {
                    print(printer, textToPrint);
                    printer.dispose();
                }
            };
            printingThread.start();
        }
    
        void print(Printer printer, String textToPrint) {
            if (printer.startJob("iassael")) { 
    
                Rectangle clientArea = printer.getClientArea();
                Rectangle trim = printer.computeTrim(0, 0, 0, 0);
                Point dpi = printer.getDPI();
                leftMargin = dpi.x + trim.x; // one inch from left side of paper
                rightMargin = clientArea.width - dpi.x + trim.x + trim.width; 
                topMargin = dpi.y + trim.y; // one inch from top edge of paper
                bottomMargin = clientArea.height - dpi.y + trim.y + trim.height;
    
                /* Create a buffer for computing tab width. */
                int tabSize = 4; // is tab width a user setting in your UI?
                StringBuffer tabBuffer = new StringBuffer(tabSize);
                for (int i = 0; i < tabSize; i++)
                    tabBuffer.append(' ');
                tabs = tabBuffer.toString();
    
                /*
                 * Create printer GC, and create and set the printer font &
                 * foreground color.
                 */
                gc = new GC(printer);
    
                Font font = new Font(null, "Helvetica", 11, SWT.NORMAL);
                gc.setFont(font);
                tabWidth = gc.stringExtent(tabs).x;
                lineHeight = gc.getFontMetrics().getHeight();
    
                /* Print text to current gc using word wrap */
                printText(textToPrint);
                printer.endJob();
    
                /* Cleanup graphics resources used in printing */
                font.dispose();
                gc.dispose();
            }
        }
    
        void printText(String textToPrint) {
            printer.startPage();
            wordBuffer = new StringBuffer();
            x = leftMargin;
            y = topMargin;
            index = 0;
            end = textToPrint.length();
            while (index < end) {
                char c = textToPrint.charAt(index);
                index++;
                if (c != 0) {
                    if (c == 0x0a || c == 0x0d) {
                        if (c == 0x0d && index < end
                                && textToPrint.charAt(index) == 0x0a) {
                            index++; // if this is cr-lf, skip the lf
                        }
                        printWordBuffer();
                        newline();
                    } else {
                        if (c != '\t') {
                            wordBuffer.append(c);
                        }
                        if (Character.isWhitespace(c)) {
                            printWordBuffer();
                            if (c == '\t') {
                                x += tabWidth;
                            }
                        }
                    }
                }
            }
            if (y + lineHeight <= bottomMargin) {
                printer.endPage();
            }
        }
    
        void printWordBuffer() {
            if (wordBuffer.length() > 0) {
                String word = wordBuffer.toString();
                int wordWidth = gc.stringExtent(word).x;
                if (x + wordWidth > rightMargin) {
                    /* word doesn't fit on current line, so wrap */
                    newline();
                }
                gc.drawString(word, x, y, false);
                x += wordWidth;
                wordBuffer = new StringBuffer();
            }
        }
    
        void newline() {
            x = leftMargin;
            y += lineHeight;
            if (y + lineHeight > bottomMargin) {
                printer.endPage();
                if (index + 1 < end) {
                    y = topMargin;
                    printer.startPage();
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the problem that an specific step in Ant can only be executed
On Windows I have a problem I never encountered on Unix. That is how
I have simple WinForms application where modifying Windows Registry. The problem is that in
I have an intermittent problem with some code that writes to a Windows Event
In a couple of scripts that I use I have problem that is intermittent.
guys! I have problem with my .net application. When I try to print in
I'm running into a problem where, I have a Window that contains a child
I have a problem that confuses my users, being that although an item is
I have a problem that I would like have solved via a SQL query.
I have a problem that I feel is best implimented in a stand alone

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.