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

  • Home
  • SEARCH
  • 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 9147277
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:56:57+00:00 2026-06-17T10:56:57+00:00

I just got the printer to work in java how I need it too,

  • 0

I just got the printer to work in java how I need it too, but there’s one last problem I need to solve. When it prints, the font’s width is rather stretched, and not crisp and clear like it should be.

Here is my code my the actual drawing to the paper:

    FontMetrics metrics = graphics.getFontMetrics(font);
    int lineHeight = metrics.getHeight();

    arrangePage(graphics, pageFormat, lineHeight);

    if (page > pageBreaks.length){
        return NO_SUCH_PAGE;
    }

    Graphics2D g = (Graphics2D) graphics;

    g.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
    g.setFont(font);

    int y = 0;
    int begin = 0;

    if (page == 0){
        begin = 0;
    }else begin = pageBreaks[page-1];

    int end = 0;

    if (page == pageBreaks.length){
        end = lines.length;
    }else end = pageBreaks[page];

    for (int line = begin; line < end; line++){
        y += lineHeight;
        g.drawString(lines[line], 0, y);
    }

    string = deepCopy;

    return PAGE_EXISTS;

How do I get rid of the stretching? It can be noted that this is based off this tutorial:
http://docs.oracle.com/javase/tutorial/2d/printing/set.html

Any advice or help is greatly appreciated.

  • 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-17T10:56:58+00:00Added an answer on June 17, 2026 at 10:56 am

    The default DPI is normal 72 DPI (I believe), which, on printed paper, is pretty terrible. You need to prompt the print API to try and find a printer with a better DPI.

    Basically you need to use the print services API.

    Try something like…

    public class PrintTest01 {
    
      public static void main(String[] args) {
    
        PrinterResolution pr = new PrinterResolution(300, 300, PrinterResolution.DPI);
    
        PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
        aset.add(MediaSizeName.ISO_A4);
        aset.add(pr);
        aset.add(OrientationRequested.PORTRAIT);
    
        PrinterJob pj = PrinterJob.getPrinterJob();
        pj.setPrintable(new Page());
        try {
          pj.print(aset);
        } catch (PrinterException ex) {
          ex.printStackTrace();
        }
    
      }
    
      public static class Page implements Printable {
    
        @Override
        public int print(Graphics g, PageFormat pageFormat, int pageIndex) throws PrinterException {
          if (pageIndex > 0) {
            return NO_SUCH_PAGE;
          }
    
          Graphics2D g2d = (Graphics2D) g;
          g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
    
          g.setFont(new Font("Arial", Font.PLAIN, 128));
          FontMetrics fm = g.getFontMetrics();
          int x = (int)(pageFormat.getWidth() - fm.stringWidth("A")) / 2;
          int y = (int)((pageFormat.getHeight() - fm.getHeight()) / 2) + fm.getAscent();
    
          g2d.drawString("A", x, y);
    
          return PAGE_EXISTS;
        }
      }
    }
    

    You might find Working with Print Services and Attributes of some help…

    I should warn you, this is going to print to the first print that it can find that meets the PrintRequestAttributeSet. You could also add in the print dialog to see what’s it doing, but that’s another level of complexity I can live without right now 😉

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

Sidebar

Related Questions

I just got pagination to work on my site, but I wanted to use
I just got a job offer to be a FE Developer, but I've never
I have a query which I eventually got to work fine but what I
I just got myself into weird problem with Luabind. I want to inherit C++
Got kind of a tricky problem. I'm working on a project where we need
Just got the new iPad, and when I touch the microphone and say a
i just got a call from Hosting company, they claimed that my application creates
I just got an email from twitter saying my keys have now been enabled
I just got out of a small project and I've tried to follow a
We just got our first major Perl CGI assignment in my CS class. Our

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.