-   Programming (
-   -   Java printing - problem with large strings (

Andy@DP 03-18-2004 08:29 AM

Java printing - problem with large strings
Up until recently I have had zero exposure to Java printing but now I I have to print out a String that could be more than a page of 12pt text with formatting. Just simple new lines, and a page number (x of y) at the bottom.

Can anyone help as my trials with drawstring() don't do new lines and I think it won't take a new page.

How do I dolve this problem?
If it helps the text is taken from a JTextArea and just has to be sans-serif 12pt.

Any help greatly appreciated, even a SIMPLE tutorial.
(I have goolged for it but found nothing that I think helps.)

Andy@DP 03-18-2004 06:48 PM

This is the code im using to try and print:


import javax.print.*;
import javax.print.attribute.*;

public class PrintApp
    public static void main(String args[]) throws Exception
        PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
        DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
        PrintService printService[] = PrintServiceLookup.lookupPrintServices(flavor, pras);
        PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
        PrintService service = ServiceUI.printDialog(null, 200, 200, printService, defaultService, null, pras);
        if (service != null)
            DocPrintJob job = service.createPrintJob();
            DocAttributeSet das = new HashDocAttributeSet();
            String filename = "g:/programs/java/myprinting/data.txt";
            FileInputStream fis = new FileInputStream(filename);
            Doc doc = new SimpleDoc(fis, flavor, das);
                job.print(doc, pras);
            catch(Exception e)
                System.out.println("error printing");

but in suse and winxp the printer is found (named in printDialog) and is accepting jobs but once the job.print() goes the System.out.println() prints and so ends the app.

BUT no paper...its driving me nuts!

linux_ub 08-03-2004 02:23 PM

i am abt 15 min old with java printing ... googled a bit and came here ... i saw another code available online at

the difference is the code in the above listing is for PNG ... i changed tht to AUTODETECT and it did print something (on paper).... havent checked out multiline multipage yet ... hope it helps

edited: was able to print multipage document ... but the line does not wrap ... line longer than the length of the page is not printed

All times are GMT -5. The time now is 12:05 AM.