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 8007139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T17:43:57+00:00 2026-06-04T17:43:57+00:00

I need to print line by line in a thermal printer. line breaking at

  • 0

I need to print line by line in a thermal printer. line breaking at %n
I would like to persist the string pattern, while printing..
I’m not well versed with printer api and graphic 2d api.. I need to fix this 1 hour time..
Would appreciate a quick answer..
Thanks in advance
My String format is like this:

String printStat = 
                        "              *****                  %n"
                      + "       W*** OF ** AND *****        %n"
                      + "      4/400 kfjkasjfdkas ajdksa        %n"
                      + "  aksdka ajke ajeklaje  kajke ka  a   %n"
                      + " Date: "+now.get(Calendar.DAY_OF_MONTH)+"/"+(now.get(Calendar.MONTH)+1)+"/"+now.get(Calendar.YEAR)+"             Time: "+now.get(Calendar.HOUR_OF_DAY)+":"+now.get(Calendar.MINUTE)+"%n"
                      + "--------------------------------------%n"
                      + "    Name            Qty     Price    %n"
                      + "--------------------------------------%n";

print method:

@Override
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
       /* We'll assume that Jav2D is available. Create a copy
        * of it so that we can pass the original Graphics
        * instance to the PageFormat instance.
        */
        Graphics2D g2d = (Graphics2D) graphics.create();
        /* Move the origin from the corner of the Paper to the corner
        * of the imageable area.
        */
        g2d.translate(format.getImageableX(), format.getImageableY());
        /* Set the text color.
        */
        g2d.setPaint(Color.black);
        g2d.setFont(new Font("Arial", Font.BOLD, 10));
        /* Use a LineBreakMeasurer instance to break our text into
        * lines that fit the imageable area of the page.
        */
        Point2D.Float pen = new Point2D.Float();
        AttributedCharacterIterator charIterator = mStyledText.getIterator();
        //LineBreakMeasurer measurer = new LineBreakMeasurer(charIterator, g2d.getFontRenderContext());
        LineBreakMeasurer measurer = new LineBreakMeasurer(charIterator, g2d.getFontRenderContext());
        float wrappingWidth = (float) format.getImageableWidth();
        while (measurer.getPosition() < charIterator.getEndIndex()) {

        TextLayout layout = measurer.nextLayout(wrappingWidth);
        pen.y += layout.getAscent();
        float dx = layout.isLeftToRight() ? 0 : (wrappingWidth - layout.getAdvance());
        layout.draw(g2d, pen.x + dx, pen.y);
        pen.y += layout.getDescent() + layout.getLeading();
        }
        g2d.dispose();
        g2d = null;
        /* Calling the PageFormat is not part of the printing API, 
        * but it is a useful convention. In this example PageFormat 
        * does not implement Printable and so it is not invoked here. 
        * In later examples, PageFormat will implement Printable. 
        */
        try {
        Printable formatPainter = (Printable) format;
        formatPainter.print(graphics, format, pageIndex);
        /* Nothing to do here. The PageFormat has nothing to print.
        */
        } catch (ClassCastException exception) {
        }
        return Printable.PAGE_EXISTS;
       }   
  • 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-06-04T17:44:00+00:00Added an answer on June 4, 2026 at 5:44 pm

    Use a fixed width font, replace Arial with “Monospaced” or “Courier”.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to print last 20 characters of string, but only whole words. Delimiter
I need to print a string in a message box in specific format for
I am printing to a thermal ticket printer which has no standard drivers. Traditionally
I'm experimenting with a Zebra TTP8200 thermal printer. For my application I need to
I have to print a custom label to a thermal printer. I have everything
I need to print a b c with the vebatim string, I posed another
I use System.out.printf method to format the string and print a line but after
#!/usr/bin/perl open(SARAN,first.txt) or die Can't Open: $!\n; while($line=<SARAN>) { print $line\n; } close SARAN;
I need to print over one line in a loop (Python 3.x). Looking around
I need to get the distance from a lat/lng point to a line. Of

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.