开发者

Prinitng text FIle issue?the long lines of a text file get cut from side?

M using the following code for printing but if there are some long lines in my text file ,they get cut from the sides while printing.What am i doing wrong?

  import java.io.FileInputStream;
    import java.io.FileNotFoundException;

    import javax.print.Doc;
    import javax.print.DocFlavor;
    import javax.print.DocPrintJob;
    import javax.print.PrintException;
    import javax.print.PrintService;
    import javax.print.PrintServiceLookup;
    import javax.print.SimpleDoc;
    import javax.print.attribute.HashPrintRequestAttributeSet;
    import javax.print.attribute.PrintRequestAttributeSet;
    import javax.print.attribute.standard.Copies;
    import javax.print.attribute.standard.MediaSizeName;
    import javax.print.attribute.standard.OrientationRequested;

    public class PrintFileWithSpec {

        public static void printFile(String filename,String printerindx){

        FileInputStream psStream=null;
        int Printerinx=Integer.parseInt(printerindx);
        try {
           psStream = new FileInputStream(filename);
        } catch (FileNotFoundException ffne) {} 
        if (psStream == null) {
            return;
        }   
        DocFlavor psInFormat = null;

        int index=filename.lastIndexOf(".");
        String extension=filename.substring(index+1);

        if(extension.equals("txt"))//||extension.equals("log")||extension.equals("xml")||extension.equals("htm")||extension.equals("html"))
        psInFormat = DocFlavor.INPUT_STREAM.AUTOSENSE;

        else if(extension.equals("jpg"))
            psInFormat = DocFlavor.INPUT_STREAM.JPEG;

        else if(extension.equals("png"))
            psInFormat = DocFlavor.INPUT_STREAM.PNG;

        else if(extension.equals("gif"))
            psInFormat = DocFlavor.INPUT_STREAM.GIF; 


        Doc myDoc = new SimpleDoc(psStream, psInFormat, null);  
        PrintRequestAttributeSet aset = 
            new HashPrintRequestAttributeSet();
        aset.add(new Copies(1));
        aset.add(MediaSizeName.ISO_A4); 

        //aset.add(Sides.DUPLEX);
         aset.add(OrientationRequested.PORTRAIT);
        PrintService[] services = 
        PrintServiceLookup.lookupPrintServices(psInFormat, null);
        System.out.println("Printer Selected "+services[Printerinx]);   

        //PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();

        DocFlavor[] docFalvor = services[Printerinx].getSupportedDocFlavors();
        for (int i = 0; i < docFalvor.length; i++) {
            System.out.println(docFalvor[i].getMimeType());
        }   
        if (services.length > 0) {
           DocPrintJob job = services[Printerinx].createPrintJob();
        try 
           {
            job.print(myDoc, aset);     
              System.out.print("Printing Doc");    
           } catch (Print开发者_开发技巧Exception pe)
           {           
               System.out.print(pe);          
           }
        }
        }   
        public static void main(String [] args)
        {       
            printFile("D:/testStream.txt","3");
        }
    }


You have to deal with line wrapping of text files yourself. The javadoc for DocFlavor says:

Furthermore, every Java Print Service instance must fulfill these requirements for processing plain text print data:

  • The character pair carriage return-line feed (CR-LF) means "go to column 1 of the next line."
  • A carriage return (CR) character standing by itself means "go to column 1 of the next line."
  • A line feed (CR) character standing by itself means "go to column 1 of the next line." *

The client must itself perform all plain text print data formatting not addressed by the above requirements.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜