开发者

How to print .doc and .docx in java

I just learned how to do some basic .doc and .docx manipulations ussing Apache-POI. And now what i want to do is try to print those documents from within my application. Any idea how can i do so? This is how i create the documents:

public void newWordDoc(String filename) throws FileNotFoundException, IOException {
    XWPFDocument document = new XWPFDocument();
    XWPFParagraph tmpParagraph = document.createParagraph();
    XWPFRun tmpRun = tmpParagraph.createRun();
    tmpRun.setText("Writing to a .doc");
    tmpRun.setFontSize(18);
    document.write(new FileOutputStream(new File(filename + ".doc")));
}

public void newWordDocX(String filename) throws FileNotFoundException, IOException {
    XWPFDocument document = new XWPFDocument();
    XWPFParagraph tmpParagraph = document.createParagraph();
    XWPFRun tmpRun = tmpParagraph.createRun();
    tmpRun.setText("Writing to a .docx");
    tmpRun.setFontSize(18);
    document.write(new FileOutputStream(new File(filename + ".docx")));
}

I suppose now i should create some method that reads them again and prints them, right? What would be the easiest and fastest way to print them i开发者_开发百科n my printer? Do i need to use the java printing API, or is there any freeware API that can make things easier? Also i would like to mention that i rode in a post somewhere else that java cant do such thing as printing word documents and that we have to transform them first to PDF. Is that true? I hope not :(


Apache POI is JUST for reading the objects, NOT for rendering, neither on the screen nor on a Printer.

You will definitely have to script a program that can render doc and docx documents. Microsoft Word comes to mind, but OpenOffice should also do a more or less acceptable job if you are lucky :).

For Word: Use Com4J, which works great.

For OpenOffice: Use their native Java API.


I suppose now i should create some method that reads them again and prints them, right?

right. unless their content is already in memory.

What would be the easiest and fastest way to print them in my printer? Do i need to use the java printing API, or is there any freeware API that can make things easier?

You should separate the concerns. First, you should worry about the reading. The printing would be another object's responsibility :)

About the transform-to-pdf issue. I guess this is not a requirement. As far as you can read the content, you can do pretty much whatever you want with it :)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜