Problem with printing from file to printer

I've been having problems printing more than one page of text from a file to a specified printer in addition to the fact that the number of pages the print dialog says are to be printed is always 9999! I have tried overriding functions from Pageable too, which gave me problems. I tried making the pageIndex more than one page, but it just prints out blank pages after the first, even if there is more data in the file that should have been printed. Here is my code, and if someone could give me some helpful advice I would very much appreciate it! (Thanks in advance for your advice!)
import java.awt.*;
import java.awt.print.*;
import java.io.*;
import javax.swing.*;
public class MyPrinter implements Printable, Runnable { //, Pageable {
private static Font fnt = new Font("Serif", Font.PLAIN, 11);
protected PrinterJob job;
protected MyDialog dialog = new MyDialog();
protected RandomAccessFile raf;
protected JFrame mainFrame;
protected String fileName;
protected File printFile;
public MyPrinter(JFrame mFrame) { mainFrame = mFrame; } // end constructor
public void printFile(String name) {// gives error message if file doesn't exist!
printFile = new File(name);
fileName = name;
if((printFile.exists()) && (printFile.canRead())) {
Thread myThread = new Thread(this);
myThread.start();
} // end if
else {
dialog.showError("Datei "+fileName+" existert nicht!", "Fehler mit Drucken");
} // end else
} // end printFile
public void run() {
job = PrinterJob.getPrinterJob();
job.setPrintable(this); // Printable is an instance of myPrint
if(job.printDialog()) { // user didn't cancel the printing
try { job.print(); } // end try
catch (PrinterException pe) {dialog.showError("Fehler: "+pe.getMessage(), "Problem");}
} // end if
} // end run
public int print(Graphics g, PageFormat pf, int pageIndex)
throws PrinterException {
// pageIndex # corresponds to page number # + 1
if (pageIndex > 0) { return Printable.NO_SUCH_PAGE; } // end if
else {
g.setFont(fnt);
g.setColor(Color.black);
int x = 75, y = 80;
String oneLine;
try {
raf = new RandomAccessFile(fileName, "r");
int fileLength = (int)printFile.length();
while((((int)raf.getFilePointer()) < fileLength)) {
oneLine = raf.readLine();
g.drawString(oneLine, x, y);
y += 15;
} // end while loop
raf.close();
} // end try
catch(IOException e) { dialog.showError("Fehler: "+e.getMessage(), "Problem");}
return Printable.PAGE_EXISTS;
} // end else
} // end print
/*public Printable getPrintable(int pageIndex) {
return this;
} // end getPrintable
public int getNumberOfPages() {
return 1;
} // end getNumberOfPages
public PageFormat getPageFormat(int pageIndex) {
return job.defaultPage();
} // end getPageFormat
} // end MyPrinter class

Well, I changed some things around, and assuming that in a file around 45 lines is a normal page, I am able to get the right amount of pages to be displayed in the dialog and to print, but still, the first page is printed fine and the rest are blank. I am baffled as to what I'm doing wrong. Printing to a printer should not be this hard to do! I have also noticed that many other people have had the same problem, but with no solutions. I am using the jdk 1.3.1 - is that a problem? Here is my new code and if anyone can propose a solution to the blank pages problem I would be very grateful!
import java.awt.*;
import java.awt.print.*;
import java.io.*;
import javax.swing.*;
public class MyPrinter implements Runnable {
protected PrinterJob job;
protected MyPrintable myPrint = new MyPrintable();
protected MyPageable myPage = new MyPageable();
protected MyDialog dialog = new MyDialog();
protected RandomAccessFile raf;
//protected Startmenu mainMenu;
protected String fileName;
protected File printFile;
protected int numPages = 0;
public MyPrinter() { } // end constructor
public void printFile(String name) {// gives error message if file doesn't exist!
printFile = new File(name);
fileName = name;
if((printFile.exists()) && (printFile.canRead())) {
Thread myThread = new Thread(this);
myThread.start();
} // end if
else {
dialog.showError("Datei "+fileName+" existert nicht!", "Fehler mit Drucken");
} // end else
} // end printFile
public void run() {
numPages = findNumPages();
job = PrinterJob.getPrinterJob();
job.setPageable(myPage); // this is an instance of Pageable
if(job.printDialog()) { // user didn't cancel the printing
try {
job.print();
try { raf.close(); } // end try
catch(IOException e) { dialog.showError("Fehler: "+e.getMessage(), "Problem");}
} // end try
catch (PrinterException pe) {dialog.showError("Fehler: "+pe.getMessage(), "Problem");}
} // end if
} // end run
private int findNumPages() {
try {
numPages = 0;
raf = new RandomAccessFile(fileName, "r");
int fileLength = (int)printFile.length();
int lineNumber = 0;
while((int)raf.getFilePointer() < fileLength) {
lineNumber = 0;
while((((int)raf.getFilePointer()) < fileLength) && (lineNumber < 45)) {
raf.readLine();
lineNumber++;
} // end while loop
numPages++;
} // end while loop
raf.seek(0);
} // end try
catch(IOException e) { dialog.showError("Fehler: "+e.getMessage(), "Problem");}
return numPages;
} // end findNumPages
public class MyPageable implements Pageable {
public Printable getPrintable(int pageIndex) { return myPrint; } // end getPrintable
public PageFormat getPageFormat(int pageIndex) {
PageFormat pf = new PageFormat();
pf.setOrientation(PageFormat.PORTRAIT);
return pf;
} // end getPageFormat
public int getNumberOfPages() {
return numPages; // test
//return ((int)printFile.length()/4000); // assume 4KB per page
} // end getNumberOfPages
} // end MyPageable class
public class MyPrintable implements Printable {
private Font fnt = new Font("Serif", Font.PLAIN, 11);
protected int lastPageIndexChecked = -1;
public int print(Graphics g, PageFormat pf, int pageIndex)
throws PrinterException {
// pageIndex # corresponds to page number # + 1
pf.setOrientation(PageFormat.PORTRAIT);
if (pageIndex >= numPages) { return Printable.NO_SUCH_PAGE; } // end if
else {
g.setFont(fnt);
g.setColor(Color.black);
String oneLine;
int x = 75, y = 80, lineNumber = 0;
try {
int fileLength = (int)printFile.length();
while((((int)raf.getFilePointer()) < fileLength) && (lineNumber < 50)) {
oneLine = raf.readLine();
lineNumber++;
g.drawString(oneLine, x, y);
y += 15;
} // end while loop
} // end try
catch(IOException e) { dialog.showError("Fehler: "+e.getMessage(), "Problem");}
return Printable.PAGE_EXISTS;
} // end else
} // end print
} // end MyPrintable class
} // end MyPrinter class

Similar Messages

  • Printing Error: There is a problem with a font file.

    I get this error when I try to print:
    "Printing Error: There is a problem with a font file."
    I tried exporting to PDF and printing from there. I get this message:
    "An error exist on this page. Acrobat may not display the page correctly. Please contact the person who created the PDF document to correct the problem."
    Has anyone encountered this problem?

    You may have a corrupted font. Try substituting fonts to see which one is the problem.

  • Can't print from safari to hp 4620 appears to print but does not first to print from safari notes print no problem any suggestions?

    Can't print from safari to hp 4620 appears to print but does not first time i have attempted to print from safari , notes print fine no problems. It is like on a desktop my print is sitting in the queue there are error messages only the usual print dialogue. I've turned the printer off and back on with no luck any suggestions?

    That's very strange.  See if clearing the cache in Safari will make it work.  I'll include the steps below.  If that doesn't help, install a different browser, such as Opera, to see if that will print from pages that Safari won't.  If it will, then something is wrong with Safari.  If it doesn't print, then the web pages might have printing blocked.  There are sites that are doing that for copyright reasons.  Let me know what you find.
    To clear the cache on Safari:
    Open Settings.
    Select Safari
    Touch Clear Cache.
    Select Clear.
    Touch Clear Cookies.
    Select Clear.
    Touch Clear History.
    Select Clear.
    I am a printer tech for HP.

  • Printing from file manager in Solaris 10

    Is is possible to print from file manager in Solaris 10, I can print from the command line but not from file manager, when I try there is no output lpstat shows nothing and no error messages. Latest patches are applied as of 14/04/08.
    Edited by: its_mallen on Apr 15, 2008 5:37 AM

    I've found I can print as root but a user can't, wondering if there are any special privilages, I've found an allow and deny command but they can print from the command line so not sure this will fix the problem

  • Hi, I have problem with importing MOV files from SJCAM 4000. MOV files are in supported formats for Adobe Premiere Elements 11. But if I'm importing MOV file, only audio part is imported, video part is not imported. How can I solve this problem?

    Hi, I have problem with importing MOV files from SJCAM 4000. MOV files are in supported formats for Adobe Premiere Elements 11. But if I'm importing MOV file, only audio part is imported, video part is not imported. How can I solve this problem?

    haben
    From looking at the specifications of your camera (SJCam 4000), we know already what video compression your camera is using. It is H.264.
    A H.264.mov file should be supported by Premiere Elements 11. On what computer operating system is your Premiere Elements 11 running?
    Do you have the latest version of QuickTime installed on your computer? And, are you running QuickTime and Premiere Elements 11 from a
    User Account with administrative privileges? Please go to Premiere Elements 11 Publish+Share/Computer/QuickTime to confirm that you find
    presets there for the QuickTime choice there.
    What are the properties of these H.264.mov files - is it 1080p30 (1920 x 1080p30)  or something else? Do you know if this camera is recording with a variable or
    a constant frame rate?
    Please review and consider and then we will decide what next.
    Thank you.
    ATR

  • For the last 2 days whenever I try to upload photos from my iPhoto page to facebook I get an error message: Bad Image There was a problem with the image file.  I haven't knowingly changed any settings on iPhoto or facebook. Can anyone help, please ?

    I need help with uploading photos from iPhoto to facebook. I could do it till 2 days ago. Now any new photo I try to upload gives me an error message:
    Bad Image
    There was a problem with the image file. 
    Please help.

    Can you drag it to the Desktop?

  • HT1338 I have a problem with opening downloaded files from the internet; zip types. Can you tell me an app to open these types of formats of files?

    I have a problem with opening downloaded files from the internet; zip types. Can you tell me an app to open these types of formats of files?

    It should unzip if you double-cick.
    What are you seeing happen when you do that?
    Select a .zip file in the Finder and Get Info (cmd-i). What is set for it to Open With. It should be Archive Utility.

  • When printing from file, print or print preview, must set page size @ 40% to get correct printing. IE is ok only wrong on firefox!

    when printing from file, print or print preview, must set page size @ 40% to get correct printing. IE is ok only wrong on firefox! edit

    What happens when you select a scale higher than 40%?

  • When i try to print from firefox the printer just spits out a blank sheet of paper. all other programs i try to print from work just fine. what can i do to fix this problem.

    when i thy to print from firefox the printer spits out a blank sheet of paper. all other programs i print from work just fine including internet explorer.
    == This happened ==
    Every time Firefox opened
    == when i installed firefox about a month ago

    See this:
    [http://kb.mozillazine.org/Problems_printing_web_pages#Prints_to_a_small_portion_of_the_page]

  • How to print from a remote printer.

    I have a HP computer in my office running windows XP connected by a cable to a Linksys router, also in the office is an HP all-in-one printer connected wirelessly to the router.  In the living room 50 feet away, I have another HP computer, connected wirelessly to the network, running windows XP and a smaller HP all-in-one printer.
    I have been attempting to print from the office printer, using the living room computer, without success.  Every time I try, the living room printer prints the document.
    Does anyone know how to configure these computers and printers to achieve the results I want, mainly printing on the office printer from the living room computer?
    Aloha, Bob

    Go into Control Panel > Printers.  Delete either the problem computer or both computers from the list.
    Re-add them:
    1. Click "Start" button --> Printers and Faxes.
    2. Under Printer Tasks, click "Add Printer".
    3. Click "Next" button on the Add Printer Wizard window.
    4. Select "Local printer attached to this computer". Deselect "Automatically detect and install My Plug and play printer". Click "Next".
    5. Under Select a Printer Port option, select "Create a new port" and select "Standard TCP/IP Port" and click "Next" button.
    6. In the "Welcome to the Add Standard TCP/IP printer port wizard" make sure that the printer is turned on and connected to same network that your computer is connected to. Click "Next" button.
    7. Under "Add Standard TCP/IP Printer Port" enter the printer's IP address. Click "Next" then click "Finish".
    8. Now, select HP from the list of manufacturers, select your printer from the list of Printer models and click 'Next' button.
    9. If you can't find your printer, you'll need to find the install CD and use the "Have Disk" option to select one of the hp*.ini files. Alternatively, you can select another HP printer model from the same type.
    10. Add the print spooler name and click "Next".
    11. Click "Next"
    12. Click "Next" and then "Finish"
    Say thanks by clicking "Kudos" "thumbs up" in the post that helped you.
    I am employed by HP

  • Hi. I have a problem with downloading from app store while last three days. An error did mot allow me to that. It's number is 1009.

    I have a problem with downloading from app store while last three days. An error does not allow me to take any app. The error number is 1009.

    Thats the ugliest code I've seen in a while and may be hampering your abilities to debug it. No offense - you have to start somewhere - think of it as somewhat constructive criticism.
    However, I cannot find any place where this is happening. Its happening on line 193.
    Also, this only happens when you try to move certain pieces to certain squaresThat might be a clue. Try printing out the indexes before line 193 and see what happens. Maybe use a debugger to step through your code.

  • While printing from word appl. printer after each line puts in number of page,

    while printing from word appl. printer after each line puts in number of page, so I do not print 3 pages but 5 instead. What to do?

    What printer models is this intended for?  I assume it is aimed at the lower end inkjets, with the suggestion of loading 10-25 sheets of paper.
    Bob Headrick,  HP Expert
    I am not an employee of HP, I am a volunteer posting here on my own time.
    If your problem is solved please click the "Accept as Solution" button ------------V
    If my answer was helpful please click the "Thumbs Up" to say "Thank You"--V

  • Problem with preview PDF files on Windows 7 32bit

    Hello,
    in my office I have a problem with preview PDF files on my computers. The files are allright, but if I select a file and click on preview mode after ~ 10sec operation system write something "Preview this file is corrupt for PDF Preview Handler wrong". On other 64bit computers with same programs the preview is work. I try uninstall all Adobe soft (Reader, Flash), clean all Adobe data from folders in root and in register, but it is not solve this problem. On Microsoft forums wrote me, that the problem is probably with AR plugin.
    So, can someone help me?
    Config PC:
    Windows 7 32/64bit Pro
    Intel i3, 4GB
    Adobe Reader 11.0.5
    Adobe Flash Player 15 ActiveX
    Adobe Flash Player 15 plugin
    Preview Handler Surrogate Host ver.: 6.1.7601.17514
    Thank you for help.

    I think I figured it out after reading other forums. Thanks!

  • Print a file to printer wirelessly and without need to use CD drive to install driver?

    I have a HP-P1006 printer in my conference room and sometimes my guest want to send print to my printer and I have to connect their laptop with USB cable to my printer and install printer driver thought CD Drive. I want to know if there is a way to print a file to printer wirelessly and without need to use CD drive to install driver? (I don't have a wireless network in my conference room) 

    Is it possible to automate Adobe Reader to load files, then set the staple property and location, then print them out? I've seen in .NET 3.0 that there is a Enumeration for Stapling in the System.Printing Namespace. I need to figure out how to automate the printing in adobe but right before it prints, have .NET assign the printer and whether it should be stapled or not.

  • Problem with uploading a file in Clustered Environment

    Hi,
    I have a problem with uploading a file in a clustered environment. I have an iview component which facilitates an upload action of an xml config file. The problem is that the upload of the modified XML file is reflected only in the central instance of the cluster and not in the dialog instances. The dialog instances hold the old config file.
    Is there any solution to upload the file to all the nodes in the cluster.
    Thanks
    Kiran

    Hi,
    This is a known problem with clustered environment. Remember that your portal component runs on just on dialog instance and it doesn't automatically have access to the other nodes.  However, there are some ways to get around this
    1. Use KM to store files. KM is a common repository for all application servers and therefore you needn't worry more
    2. Use an external batch oriented product (suresync/robocopy) to synch folders on the different DIs. You basically use your existing portal component, but there is a batch job which makes sure the upload folder is identical on all DIs (however, there is a slight delay depending on how often you run the batch job)
    3. Store the files on a shared disk directly from the portal component.
    Cheers
    Dagfinn

Maybe you are looking for

  • Is it possible to hide desktop folders until needed

    MacBookPro screen real estate limited.  Folders and other junk on my desktop can be distracting on top of that. Is it possible to hide them until I need them?

  • Factory Calender - Help Needed

    I am getting this messges when i try and transaction MICN...PID for cycle count... The date 14.07.2009 comes after the end of the validity period of the factory calendar that is assigned to the given plant Could someone tell me how i fix this

  • Wont print get error message

    every time I want to print something i get several different error messages If i got to internet explorer it works fine Warning: Error in parsing value for 'filter'. Declaration dropped. Source File: https://support.mozilla.com/media/css/questions-mi

  • Set font in JTable

    hello i just have an easy question that i am not smart enough to know yet...but how do u set a cell or a whole JTable to a specific font? Thanx Kevin

  • Unload/load proxy clears cache without -cc

    Just as the subject says, when we unload then load proxy the cache volumes get cleared. That without adding the "-cc" switch. New BM39sp2 server on NW65sp8. Is there any way to change this behavior? we'd prefer to be able to load/unload without clear