LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   word wrap in java (https://www.linuxquestions.org/questions/programming-9/word-wrap-in-java-213323/)

linux_ub 08-04-2004 10:48 AM

word wrap in java
 
i got this code for printing by googling.

Code:

import javax.print.*;
import javax.print.attribute.*;
import java.io.*;

public class Printing {
  public static void print(String args) throws Exception {
    String filename = args;
    PageFormat pageFormat
    PrintRequestAttributeSet pras =
      new HashPrintRequestAttributeSet();
    DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
    //flavor = DocFlavor.CHAR_ARRAY.TEXT_PLAIN;
    PrintService printService[] =
      PrintServiceLookup.lookupPrintServices(flavor, pras);
    PrintService defaultService =
      PrintServiceLookup.lookupDefaultPrintService();
    PrintService service = ServiceUI.printDialog(null, 200, 200,
      printService, defaultService, flavor, pras);
    if (service != null) {
      DocPrintJob job = service.createPrintJob();
      FileInputStream fis = new FileInputStream(filename);
      DocAttributeSet das = new HashDocAttributeSet();
      Doc doc = new SimpleDoc(fis, flavor, das);
      job.print(doc, pras);
    }
  }
}

when i am using this to print a .txt file ... the printer prints but it does not wrap long lines ... can neone suggest wht do i need to do.

thanks

djgerbavore 08-04-2004 02:42 PM

did you try different types of doc flavors? because you can set up different types of documents and print sizes?

what is filename? is it a file of google results or something else, because if its a of google results, you could parse the file, to make it wrap and then stream it into another or same file....just a thought.

linux_ub 08-04-2004 03:17 PM

filename is being supplied to the class. it is the name of the file to be printed. i havent even understood the code ... and am currently reading a very old tutorial on printing in java

http://www.javaworld.com/javaworld/j...020-print.html

how can i change the print sizes .... i am trying to figure tht out ... just came across the tutorial on the link above ... hopefully i will get the answers there

thanks neways

german 08-11-2004 01:16 AM

if you're using a fixed-width font you could just count the characters between "\n"'s and when it gets to 80 or textfield.size().width/8 or whatever insert a "\n"... but that might be slow... an alternative (as previously suggested) is to check which subclass (or implementation... can't remember if it's an interface) of Document the textfield's using and even manually set it (this well may cause it to render html as plain text).

HTH


All times are GMT -5. The time now is 03:02 AM.