具体步骤如下:
import javaioFile;
import javaioFileInputStream;
import javaioIOException;
import javaioInputStream;
import javaxprintDoc;
import javaxprintDocFlavor;
import javaxprintDocPrintJob;
import javaxprintPrintService;
import javaxprintPrintServiceLookup;
import javaxprintSimpleDoc;
import javaxprintattributeDocAttributeSet;
import javaxprintattributeHashDocAttributeSet;
import javaxprintattributeHashPrintRequestAttributeSet;
import javaxswingJFileChooser;
public class PrintDemo {
public static void main(String[] args) {
JFileChooser fileChooser = new JFileChooser(); // 创建打印作业
File file = new File("f:/111txt"); // 获取选择的文件
// 构建打印请求属性集
HashPrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
// 设置打印格式,因为未确定类型,所以选择autosense
DocFlavor flavor = DocFlavorINPUT_STREAMAUTOSENSE;
// 定位默认的打印服务
PrintService defaultService = PrintServiceLookuplookupDefaultPrintService();
InputStream fis = null;
try {
DocPrintJob job = defaultServicecreatePrintJob(); // 创建打印作业
fis = new FileInputStream(file); // 构造待打印的文件流
DocAttributeSet das = new HashDocAttributeSet();
Doc doc = new SimpleDoc(fis, flavor, das);
jobprint(doc, pras);
} catch (Exception e) {
eprintStackTrace();
} finally {
if (fis != null) {
try {
fisclose();
} catch (IOException e) {
eprintStackTrace();
}
}
}
}
}
肯定是乱码啊那是因为docx 里面的东西其实进行过某些格式整合到一起的文件 你可以把一个word文件的后缀改成zip 解压缩打开看一下就知道
java读取docx里面的东西 可以使用 poi ,具体怎么做可以在网上搜一下
poi读取前程无忧的简历会打不开的,至少我以前读是这样的,因为他有时候是mht文件直接另存为word文档的,所以保险起见建议用jacob来读,如果他是doc或者是docx文档可以转化为html然后用jsoup来读取,效果挺好的
下面是转化的代码:
package comjavadoc;import comjacobactiveXActiveXComponent;
import comjacobcomDispatch;
import comjacobcomVariant;
public class JacobRead {
public static void extractDoc(String inputFIle, String outputFile) {
boolean flag = false;
// 打开Word应用程序
ActiveXComponent app = new ActiveXComponent("WordApplication");
try {
// 设置word不可见
appsetProperty("Visible", new Variant(false));
// 打开word文件
Dispatch doc1 = appgetProperty("Documents")toDispatch();
Dispatch doc2 = Dispatchinvoke(
doc1,
"Open",
DispatchMethod,
new Object[] { inputFIle, new Variant(false),
new Variant(true) }, new int[1])toDispatch();
// 作为txt格式保存到临时文件
Dispatchinvoke(doc2, "SaveAs", DispatchMethod, new Object[] {
outputFile, new Variant(7) }, new int[1]);
// 关闭word
Variant f = new Variant(false);
Dispatchcall(doc2, "Close", f);
flag = true;
} catch (Exception e) {
eprintStackTrace();
} finally {
appinvoke("Quit", new Variant[] {});
}
if (flag == true) {
Systemoutprintln("Transformed Successfully");
} else {
Systemoutprintln("Transform Failed");
}
}
public static void main(String[] args) {
JacobReadextractDoc("D:/xxxx简历doc","D:/etxt");
}
}
当然,也可以转化为txt读取,这部分代码没保存,你可以到网上找找,和转化成html的方法大差不差。
然后下面是我以前写的poi读取的方式:
package TestHanLp;import javaioFileInputStream;
import javaioFileNotFoundException;
import javaioIOException;
import orgapachepoiPOIXMLDocument;
import orgapachepoiPOIXMLTextExtractor;
import orgapachepoihwpfextractorWordExtractor;
import orgapachepoiopenxml4jopcOPCPackage;
import orgapachepoixwpfextractorXWPFWordExtractor;
import orgapachepoixwpfusermodelXWPFDocument;
public class Test {
private static String text = "";
public static String Read(String path) throws Exception{
//解析docx格式的简历
if(pathtoLowerCase()endsWith("docx")){
try {
OPCPackage oPCPackage = POIXMLDocumentopenPackage(path);
XWPFDocument xwpf = new XWPFDocument(oPCPackage);
POIXMLTextExtractor ex = new XWPFWordExtractor(xwpf);
text=exgetText();
oPCPackageclose();
}
catch (FileNotFoundException e)
{
eprintStackTrace();
}
catch (IOException e)
{
eprintStackTrace();
}
}else {
//解析doc格式的简历
if(pathtoLowerCase()endsWith("doc")){
FileInputStream fis = new FileInputStream(path);// 载入文档
WordExtractor wordExtractor = new WordExtractor(fis);
String[] paragraph = wordExtractorgetParagraphText();
StringBuffer stringBuffer = new StringBuffer();
for (int i = 0; i < paragraphlength; i++) {
if (null != paragraph[i] && !""equals(paragraph[i])) {
paragraph[i] = paragraph[i]substring(0, paragraph[i]length() - 1);// 去掉末尾符号
}
stringBufferappend(paragraph[i])append("\n");//将每一小段隔开
}
text = stringBuffertoString();
}
}
return text ;
}
}
望题主采纳
对了,jacob读取word文档的效果比poi号,但运行速度不够,用的时候自己考虑考虑吧WordExtractor的用法错了,你用下面的方法试试: File file = new File(doc); FileInputStream fileInputStream = getFileInputStream(file); WordExtractor wordExtractor = new WordExtractor(fileInputStream); String text = wordExtractorgetText();
可以用过滤器,也可以循环自己判断,如FourQueue 写的那样。
下边是过滤器的例子。
------------------------------------------------------------------------------------
import javaioFile;
import javaioFilenameFilter;
public class FileDoc {
public static void main(String[] args) {
File root = new File("D:\\test");
File[] files = rootlistFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
if (nametoLowerCase()endsWith("doc")) {
// 2003 word
return true;
}
if (nametoLowerCase()endsWith("docx")) {
// 2007 word
return true;
}
return false;
}
});
for (int i = 0; i < fileslength; i++) {
Systemoutprintln(files[i]getPath());
}
}
}
以上就是关于如何用Java调用打印机打印docx,PDF文档全部的内容,包括:如何用Java调用打印机打印docx,PDF文档、为什么java 用IO流读docx文件里的内容打印在控制台是乱码、java:解析word文档(前程无忧简历),最好有代码案例poi或者jacob解析都可以,有jar资源,求急。感谢等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)