0

I was looking for a way to rearrange several arguments recived as strings and one byte[] image and put them all togther as one image (jpg for example) ready to be printed (immidiatly).

example:

public static void printCard(String name, String LName, Image MainImage)

Basicly this function will be a simple card printer. I was looking for an idea or some one who can guide me, this could be very easy if some one will guide me a bit.

4
  • 1
    Are you looking for printing text on Image. If yes here is the minimal reproducible example[1].. [1]: stackoverflow.com/questions/2658554/… Commented Jan 23, 2013 at 12:08
  • im looking to create the image myself with a fixed size and white background and then add the other arguments above Commented Jan 23, 2013 at 12:15
  • @Adir.el: This is a forum for programming questions and not to ask other people to implement software for you. Commented Jan 23, 2013 at 12:21
  • just read my question again and see that i need some one who can guide me. :) Commented Jan 23, 2013 at 12:38

3 Answers 3

1

This is a simple method that I use to add text onto a pre-existing image.

I'm sure you can work out how to pass a blank image in and add the other lines as you see fit.

private BufferedImage drawText2(BufferedImage bi, String outputText) {
    Graphics2D g2d = bi.createGraphics();
    g2d.setFont(new Font("Helvetica", Font.BOLD, 36));
    FontMetrics fm = g2d.getFontMetrics();
    int textWidth = fm.stringWidth(outputText);
    int imageWidth = bi.getWidth();
    int leftAlignment;
    int topAlignment;

    // Align the text to the middle
    leftAlignment = (imageWidth / 2) - (textWidth / 2);

    // Align the text to the top
    topAlignment = fm.getHeight() - 10;

    // Create the drop shadow
    g2d.setColor(Color.DARK_GRAY);
    g2d.drawString(outputText, leftAlignment + 2, topAlignment + 2);

    // Create the text itself
    g2d.setColor(Color.LIGHT_GRAY);
    g2d.drawString(outputText, leftAlignment, topAlignment);

    g2d.dispose();
    return bi;
}
Sign up to request clarification or add additional context in comments.

1 Comment

i was thinking about this solution and will try to impliment soon . will update you
0

If you want to print directly from your application you can use java.awt.print package.

Try this method

public static void printCard(final String name, final String lName, final Image mainImage){

    Printable contentToPrint = new Printable(){
        @Override
        public int print(Graphics graphics, PageFormat pageFormat, int page) throws PrinterException {
            if (page > 0) {
                return NO_SUCH_PAGE;
            }
            pageFormat.setOrientation(PageFormat.PORTRAIT);
            graphics.drawImage(mainImage, 0, 0, null);
            graphics.drawString(lName, 100, 300);
            graphics.drawString(name, 100, 100);

            return PAGE_EXISTS;
        }
    };

    PrinterJob job = PrinterJob.getPrinterJob();
    job.setPrintable(contentToPrint);
    //You can show a print dialog before printing by job by wrapping the following blocks with a conditional statement if(job.printDialog()){...}
    try {
        job.print();
    } catch (PrinterException e) {
        System.err.println(e.getMessage());
    }

}

You need to import the print related classes from java.awt.print.

2 Comments

any idea of how can fix printing size ?
@Adir.el you can use java.awt.print.PageFormat to set the paper size. Here is some useful documentation. massapi.com/class/java/awt/print/PageFormat.java.html you can google for more samples.
0

i used graphics2d to impliment my function . this method recive 2 images and 6 strings : Bufferdimage bi is my blank image, on this image i add objects (ex: image, string) :

    private static BufferedImage drawText2(BufferedImage logo,BufferedImage small,BufferedImage bi, String Headline,String outputText2,String outputText3,String outputText4,String outputText5,String outputText6) {
    Graphics2D g2d = bi.createGraphics();
    RenderingHints rh = new RenderingHints(
            RenderingHints.KEY_TEXT_ANTIALIASING,
            RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
    g2d.setRenderingHints(rh);
    g2d.setFont(new Font("Arial", Font.BOLD, 50));
    FontMetrics fm = g2d.getFontMetrics();
    int textWidth = fm.stringWidth(Headline);
    int imageWidth = bi.getWidth();
    int leftAlignment;
    int topAlignment;

    // Align the text to the top
    topAlignment = fm.getHeight() - 10;

    // Create the text itself
    //headline
    leftAlignment = (imageWidth / 2) - (textWidth);
    g2d.setColor(Color.blue);
    g2d.drawString(Headline, leftAlignment+290, topAlignment+60);
  //property changed
    g2d.setFont(new Font("Arial", Font.BOLD, 30));
    fm = g2d.getFontMetrics();
    textWidth = fm.stringWidth(Headline);
    //second line
    textWidth = fm.stringWidth(outputText2);
    leftAlignment = (imageWidth / 2) - (textWidth);
    g2d.setColor(Color.black);
    g2d.drawString(outputText2, leftAlignment+290, topAlignment+120);
    //third line
    textWidth = fm.stringWidth(outputText3);
    leftAlignment = (imageWidth / 2) - (textWidth);
    g2d.setColor(Color.black);
    g2d.drawString(outputText3, leftAlignment+290, topAlignment+160);
    //4 line
    textWidth = fm.stringWidth(outputText4);
    leftAlignment = (imageWidth / 2) - (textWidth);
    g2d.setColor(Color.black);
    g2d.drawString(outputText4, leftAlignment+290, topAlignment+200);
  //5 line
    textWidth = fm.stringWidth(outputText5);
    leftAlignment = (imageWidth / 2) - (textWidth);
    g2d.setColor(Color.black);
    g2d.drawString(outputText5, leftAlignment+290, topAlignment+240);

    //property changed
    g2d.setFont(new Font("Arial", Font.getFont("Arial").HANGING_BASELINE, 20));
    fm = g2d.getFontMetrics();
  //security line
    textWidth = fm.stringWidth(outputText6);
    leftAlignment = (textWidth);
    g2d.setColor(Color.red);
    g2d.drawString(outputText6, 10, topAlignment+300);
    //logo
    g2d.drawImage (logo, 44, 44,180,70, null);
    //profile
    g2d.drawImage (small, 60, 120,160,190, null);
    g2d.dispose();
    return bi;
}

bi is the card with all other objects btw i added smothing to the font, without this the text is very unpleasent.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.