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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:38:57+00:00 2026-06-12T22:38:57+00:00

I need to print on a printer an image and some data, in java

  • 0

I need to print on a printer an image and some data, in java (Swing) but all in vain. I am just able to do up with the data but not with the image. I have a abc.png file and 6 jTextBoxes, from which the value has to be printed on the printer. I am using the FileWriter and PrinterJob classes to implement the job. The data and the image can be printed separately but not together. Can somebody suggest me something.

Thanks.

Code for printing image:

try {
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
pras.add(new Copies(1));

PrintService pss[] = PrintServiceLookup.lookupPrintServices(DocFlavor.INPUT_STREAM.GIF,     pras);

if (pss.length == 0)
throw new RuntimeException("No printer services available.");

PrintService ps = pss[0];
System.out.println("Printing to " + ps);

DocPrintJob job = ps.createPrintJob();

FileInputStream fin = new FileInputStream("C://a.gif");


Doc doc = new SimpleDoc(fin, DocFlavor.INPUT_STREAM.GIF, null);

//Doc doc1=new SimpleDoc();



job.print(doc, pras);

//fin.close();
} catch (IOException ie) {
ie.printStackTrace();
} catch (PrintException pe) {
pe.printStackTrace();
}

Code for printing text:

Just pass the location of the file on the hard disk, in FileInputStream.

  • 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-12T22:38:59+00:00Added an answer on June 12, 2026 at 10:38 pm

    One solution could be to create a second image that contains your original image and the text.

    Something like this may work? (un-tested):

    public Image addTextToImage(BufferedImage i, String[] text) {
    
        final int VERTICLE_PADDING_PIXELS = 5;
        final int LEFT_MARGIN_PIXELS = 5;
    
        FontMetrics fm = i.createGraphics().getFontMetrics();
    
        int width = i.getWidth();
        int height = i.getHeight()
                + (text.length * (fm.getHeight() + VERTICLE_PADDING_PIXELS));
    
        for (String s : text) {
            width = Math.max(width, fm.stringWidth(s) + LEFT_MARGIN_PIXELS);
        }
    
        BufferedImage result = new BufferedImage(i.getHeight(), width, height);
    
        Graphics2D g = result.createGraphics();
    
        g.drawImage(i, 0, 0, null);
    
        for (int x = 0; x < text.length; x++) {
            g.drawString(text[x], LEFT_MARGIN_PIXELS, i.getHeight() + (x + 1) *VERTICLE_PADDING_PIXELS + x*fm.getHeight());
        }
    
        return result;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have checkbox list printed in one column, but i need it to print
I need to be able to mask an image in my layout to have
I need to create an app (C++) that will use the printer to print
I need to print real datbase type name from DbParameter.DbType , but when I
I need to print a large image collection of pictures. If I use something
I write a simple program to print a image in JSF.... I have one
I have a page with a data grid and when I print it i
I need to print images in Java. So I implemented print() method of Printable
I have some points that I need to classify. Given the collection of these
I need to print an image with my application. I need a phonegap plugin

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.