Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Java - Printing to DYMO label printer

  • 15-02-2006 5:52pm
    #1
    Registered Users, Registered Users 2 Posts: 841 ✭✭✭


    Hi,

    Anybody any experience of doing this? I can get it to print alright but it just won't print on the first half of the label. It starts printing half way across the label (Doesn't seem to matter what co-ordinates I set for the graphics object or what label size/type I choose from the list in 'printing preferences').
    It can print on the whiole label alright when I use the DYMO program.

    Thanks,
    Brian


Comments

  • Registered Users, Registered Users 2 Posts: 21,264 ✭✭✭✭Hobbes


    Wild guess here. but are you not setting your origin or left border?


  • Registered Users, Registered Users 2 Posts: 841 ✭✭✭Dr Pepper


    Yes I have the left margin set to 10 (which brings me 10 to the right of the centre line of the label). If I set it to 20 it prints 20 right of the centre line. If I set it to -20, it moves the text over to the left of the centre line, however, anything to the left of that line is not printed (so if I was printing 'Brian' for example it would come out as 'ian' starting atthe centre line). If I choose my HP printer (or any other printer), it will print 10mm from the left margin of an A4 page as expected.
    Its like the centre line is the reference point or origin for the x axis and I can't seem to change it.
    Here is the code. I must admit the Printer.java class is something which I found years ago and hacked a little bit (but I still don't fully understand all of it!). It has served me quite well as a simple text printer though.
    // CLASS TO CREATE 'PRINTER' OBJECT
    import java.awt.*;
    
    class testPrinter
    {
    	String test = "Text Line 1\nText Line 2\nText Line 3";
    	Font f = new Font("Courier New", Font.BOLD, 14);
    
    	public testPrinter()
    	{
    		Printer P = new Printer(test,f,10,10,10);
    		System.exit(0);
    	}
    	public static void main(String args[])
    	{
    		testPrinter TP = new testPrinter();
    	}
    }
    
    // PRINTER CLASS
    import java.awt.print.*;
    import java.io.*;
    import java.net.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.Properties;
    
    public class Printer extends JFrame
    {
      	private Properties p = new Properties();
    
    	public Printer( String S, Font f, int topMargin, int leftMargin, int botMargin )
    	{
    		PrintJob pjob = getToolkit().getPrintJob(this, S, p);
    
    		if (pjob != null)
    		{
    			Graphics pg = pjob.getGraphics();
    			if (pg != null)
    			{
    				double start = System.currentTimeMillis();
    				printLongString (pjob, pg, S, f, topMargin, leftMargin, botMargin);
    				double end = System.currentTimeMillis();
    
    				System.out.println("Time taken to create print job was: " + (end - start)/1000 + " seconds.");
    				pg.dispose();
    			}
    
    			pjob.end();
    		}
    	}
    
    	private void printLongString( PrintJob pjob, Graphics pg, String s, Font f, int topMargin, int leftMargin, int botMargin )
    	{
    		int pageNum = 1;
    		int linesForThisPage = 0;
    		int linesForThisJob = 0;
    
    		StringReader sr = new StringReader(s);
    		LineNumberReader lnr = new LineNumberReader(sr);
    		String nextLine;
    
    		int pageHeight = pjob.getPageDimension().height - botMargin;
    
    		pg.setFont(f);
    		pg.setColor( new Color( 0,0,0 ) );
    
    		FontMetrics fm = pg.getFontMetrics(f);
    		int fontHeight = fm.getHeight();
    		int fontDescent = fm.getDescent();
    		int curHeight = topMargin;
    
    		try
    		{
    			do
    			{
    				nextLine = lnr.readLine();
    				if (nextLine != null)
    				{
    					if ((curHeight + fontHeight) > pageHeight)
    					{
    						// New Page
    						System.out.println ("" + linesForThisPage + " lines printed for page " + pageNum);
    						if (linesForThisPage == 0)
    						{
    							System.out.println ("Font is too big for pages of this size; aborting...");
    							break;
    						}
    
    						pageNum++;
    						linesForThisPage = 0;
    						pg.dispose();
    						pg = pjob.getGraphics();
    						if (pg != null)
    						{
    							pg.setFont(f);
    							pg.setColor( new Color( 0,0,0 ) );
    						}
    						curHeight = topMargin;
    					}
    					curHeight += fontHeight;
    					if (pg != null)
    					{
    						pg.drawString (nextLine, leftMargin, curHeight - fontDescent);
    						linesForThisPage++;
    						linesForThisJob++;
    					}
    					else
    					{
    						System.out.println ("pg null");
    					}
    				}
    			}
    			while (nextLine != null);
    		}
    		catch (EOFException eof)
    		{
    		}
    		catch (Throwable t)
    		{
    			System.out.println("THROWABLE THROWN");
    			t.printStackTrace();
    		}
    		System.out.println ("" + linesForThisPage + " lines printed for page " + pageNum);
    		System.out.println ("Pages printed: " + pageNum);
    		System.out.println ("Total lines printed: " + linesForThisJob);
    	}
    }
    


Advertisement