问题描述
|
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\");
}
}
解决方法
您必须自己处理文本文件的换行。
DocFlavor
的Javadoc说:
此外,每个Java Print Service实例都必须满足以下处理纯文本打印数据的要求:
字符对回车换行符(CR-LF)的意思是“转到下一行的第1列。”
单独的回车(CR)字符表示“转到下一行的第一列。”
换行(CR)字符本身就表示“转到下一行的第1列”。
*
客户必须自己执行上述要求未解决的所有纯文本打印数据格式。