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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:12:06+00:00 2026-06-10T17:12:06+00:00

I need to print images in Java. So I implemented print() method of Printable

  • 0

I need to print images in Java. So I implemented print() method of Printable interface. But printer always prints only some piece of original image. How I can cause image to fit printing area (A4), so printer be able to print whole image, not piece of them? Now my code looks like this:

public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {


    if (pageIndex >= images.size()) {
        return Printable.NO_SUCH_PAGE;
    }
    RenderedImage image;


    Graphics2D graphics2D = (Graphics2D) graphics;

    image = new NullOpImage((RenderedImage) images.get(pageIndex), null, null, OpImage.OP_IO_BOUND);


    double x = (pageFormat.getImageableWidth() - image.getWidth())/2 + pageFormat.getImageableX();
    double y = (pageFormat.getImageableHeight() - image.getHeight())/2 + pageFormat.getImageableY();


    graphics2D.drawRenderedImage(image, AffineTransform.getTranslateInstance(x, y));

    return 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-10T17:12:08+00:00Added an answer on June 10, 2026 at 5:12 pm

    You need to scale the image down to fit the available area.

    private int currentPage = -1;
    private Image cachedScaledImage = null;
    
    public double getScaleFactor(int iMasterSize, int iTargetSize) {
        double dScale = 1;
        if (iMasterSize > iTargetSize) {
            dScale = (double) iTargetSize / (double) iMasterSize;
        } else {
            dScale = (double) iTargetSize / (double) iMasterSize;
        }
        return dScale;
    }
    
    public double getScaleFactorToFit(BufferedImage img, Dimension size) {
        double dScale = 1;
        if (img != null) {
            int imageWidth = img.getWidth();
            int imageHeight = img.getHeight();
            dScale = getScaleFactorToFit(new Dimension(imageWidth, imageHeight), size);
        }
        return dScale;
    }
    
    public double getScaleFactorToFit(Dimension original, Dimension toFit) {
        double dScale = 1d;
        if (original != null && toFit != null) {
            double dScaleWidth = getScaleFactor(original.width, toFit.width);
            double dScaleHeight = getScaleFactor(original.height, toFit.height);
    
            dScale = Math.min(dScaleHeight, dScaleWidth);
        }
        return dScale;
    }
    
    public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
    
        if (pageIndex >= images.size()) {
            return Printable.NO_SUCH_PAGE;
        }
        RenderedImage image;
    
    
        Graphics2D graphics2D = (Graphics2D) graphics;
    
        int width = (int)Math.round(pageFormat.getImageableWidth());
        int height = (int)Math.round(pageFormat.getImageableHeight());
    
        if (currentPage != pageIndex || cachedScaledImage == null) {
            currentPage = pageIndx;    
    
            image = new NullOpImage((RenderedImage) images.get(pageIndex), null, null, OpImage.OP_IO_BOUND);
    
            BufferedImage imageCopy = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);
            Graphics2D g2d = imageCopy.createGraphics();
            g2d.drawImage(imageCopy, 0, 0, null);
            g2d.dispose();
    
            double scaleFactor = getScaleFactorToFit(new Dimension(image.getWidth(), image.getHeight()), new Dimension(width, height));
    
            int imageWidth = (int)Math.round(image.getWidth() * scaleFactor);
            int imageHeight = (int)Math.round(image.getHeight() * scaleFactor);
    
            double x = ((pageFormat.getImageableWidth() - imageWidth) / 2) + pageFormat.getImageableX();
            double y = ((pageFormat.getImageableHeight() - imageHeight) / 2) + pageFormat.getImageableY();
    
            cachedScaledImage = imageCopy.getScaledInstance(imageWidth, imageHeight, Image.SCALE_SMOOTH);
    
        }
    
        graphics2D.drawRenderedImage(cachedScaledImage, AffineTransform.getTranslateInstance(x, y));
    
        return PAGE_EXISTS;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to print on a printer an image and some data, in java
I need to be able to rotate images individually(in java). The only thing I
I need to print report page that has couple of background images on it.
Our print team saves raster images as .eps files. We need to convert about
I need to print real datbase type name from DbParameter.DbType , but when I
I'm drawing images to pdf using Java framework iText. I need to draw lines
I am generating some images using the Graphics2D interface, and occasionally I'd like to
I want to load some images to the cloud, but I want to add
I need print two words: " A " and " B " using java
I make an app that have to printing image. I need to print image

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.