打印文本FIle问题?文本文件的长行从侧面切割?

时间:2011-06-13 05:33:03

标签: java printing

M使用以下代码进行打印,但如果我的文本文件中有一些长行,则在打印时会从侧面剪切。我做错了什么?

  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 (PrintException pe)
           {           
               System.out.print(pe);          
           }
        }
        }   
        public static void main(String [] args)
        {       
            printFile("D:/testStream.txt","3");
        }
    }

1 个答案:

答案 0 :(得分:1)

您必须自己处理文本文件的换行。 javadoc for DocFlavor说:

  

此外,每个Java Print Service实例都必须满足处理纯文本打印数据的这些要求:

     
      
  • 字符对回车换行符(CR-LF)表示“转到下一行的第1列。”
  •   
  • 回车(CR)字符本身意味着“转到下一行的第1列。”
  •   
  • 换行(CR)字符本身意味着“转到下一行的第1列”。   *
  •   
     

客户端必须自己执行上述要求未解决的所有纯文本打印数据格式。