We are converting a C++ project to Java where we generate reports in ".doc" extension. The problem is we don't use any third party library to generate MS Word document, rather a file with .doc extension. Everything works fine except that we can't seem to find a way to add a Header at the beginning of every page. Using line numbers is not an option. Any other way it can be done?
Thank you.
The Apache POI library might be of some help.
It has facilities to read and modify Microsoft proprietary file formats like MS-Word .doc and MS-Excel .xls
Related
How can I open a Microsoft Word docx file in Java? furthermore, how can I open it if it is password protected?
For instance,
File f = new File("hello.docx");
Please try to avoid responding with things such as "you shouldn't do this." I have a good reason for this, so please stick to the question when you answer. thanks a lot!
There is Apache POI project for working with MS Office files. DOCX file is just a zip file with series of XML files inside, so you can unzip the file and work with XML. The XML spec (Open XML) is known.
I haven't personally used it, but it looks like Apache POI will work for you: http://poi.apache.org/
You can use docx4j too. http://www.docx4java.org/trac/docx4j
I have used both docx4j and Apache's POI libraries, if you are working with .docx I would recommend .docx4j. Automated alot of the process of creating a .docx.
There is a great exmaple here : http://java.dzone.com/articles/create-complex-word-docx
on how to create a .docx using the docx4j package.
If the docx is password protected, it won't be a zip file. It will be a compound file. See Overview of Protected Office Open XML Documents
To read a compound file in Java, use POIFS. POIFS is part of POI (docx4j uses it as well, so if you download the docx4j distribution, you'll be able to use the POIFS API)
Once you have decrypted the encrypted package, you can read it using docx4j or POI.
Edit: OK, now docx4j can handle password-protected docx automatically.
Have you tried to open it using the Open Office api? It can work with a lot of documents types.
I used it with MS Excel files .xls ( old version ) format.
Hope this can help you.
I want to generate PDF file from RTF file.
I have tried following.
Itext
It's already outdated and new version doesn't support rtf.
JDocConverter
It uses OpenOffice on the background. it is working fine, there is only one problem. Open office doesn't support drawing object in RTF.
Any other possible and reliable solutions?
Note: It would be fine don't use any commercial software.
Windows has native convert RTF to PDF using command line, however it will to a degree be limited, so it will use direct convert text and images, but it will depend on rtf syntax as to which drawn objects are supported. WORD ART drawing objects need MS Word to print
The output looks reasonable but here is the source in MSWord where the art was clearly not handled by the non-word printout.
Under Windows you could print to CutePDF Writer. This freeware uses Ghostscript as a back end.
You may try Aspose.Words for Java to convert RTF file to PDF format. You can load a file in RTF format into Aspose.Words for Java and then save it to PDF format. Please note that while loading specify RTF as LoadFormat value and pass PDF as SaveFormat value while saving the document. This doesn't require OpenOffice or any other software to be installed for the conversion to work.
Disclosure: I work as developer evangelist at Aspose.
Best way to do it is use MS Office. And Ms Office is able to save file in PDF format (you need install some addons I think).
I need to create a little desktop app in Java that creates for me a .doc file and writes a bit of text into the file. I found an interesting tool called Aspose, but i saw it is not free at all.
Do yoy know what kind of, java API can i use for doing that(for free)?
Is it possible to do that only with the java SE libraries?
What do you think would be the easiest and fastest way to achive this goal?
I suggest you have a look at the Apache POI framework, specifically the HWPF - Java API to Handle Microsoft Word Files:
HWPF is the name of our port of the Microsoft Word 97(-2007) file format to pure Java. It also provides limited read only support for the older Word 6 and Word 95 file formats.
if you are going with .doc then as a learning excercise, open a Word document with some content (ideally similar to what you want to create) then save that as XML, and review the contents.
you will need to do some basic DOM parsing and management in your code to insert the right stuff.
By .doc file, I assume you mean Microsoft Office? Reading and writing Office file formats is something of a black art. Does it have to be a .doc format file specifically? A lot easier would be to write out a Rich Text Format file (.rtf) that Word could load.
And if you don't need to use .doc specifically I would suggest you use .odt, http://www.jopendocument.org/.
I've got a customer who managed it to paste WordprocessingML content into our application. As far as I know it was a direct copy&paste from Word 2000 to our Java application. I tried every Word and Java Version combination, but I can't reproduce this behavior - especially, since our application filters for HTML and text/plain.
I'm pretty sure that the older Office version had there own clipboards and exported only the formats, which should be available to other programms. Every office version I know(except maybe 2007) exports HTML, RTF and Plain.
Is there any way to get a WordprocesingML content into the clipboard and maybe to get Java to mix-up the data flavours
Apache POI is a Java API To Access Microsoft Format Files. HWPF is its part for reading and writing MS Word files. Apache TIKA is a toolkit for detecting and extracting metadata and structured text content from various documents using existing parser libraries. It also gives some support for MS Word documents. I suggest you see if they fit your use case.
how to Parse a PDF file and write the content in word file using Java?
For parsing a PDF file in Java, you can use Apache PDFBox: http://incubator.apache.org/pdfbox/
For reading/writing Word (or other Office) file formats in Java, try POI: http://poi.apache.org/
Both are free.
Try the iText java library:
iText is an ideal library for developers looking to enhance web- and other applications with dynamic PDF document generation and/or manipulation.
It can be used for your parsing step.
As for generating word documents - the OpenOffice Java API might be able to generate Word compatible docs (no personal experience with this API).
You might want to try any of these:
http://incubator.apache.org/pdfbox/
https://pdf-renderer.dev.java.net/
Once you are reading the contents of the PDF file, you can as well store them in a ODT file or a text file. For ODT file, try http://odftoolkit.openoffice.org.
Best!
You could use iText if the source PDF is mostly text. Images and such are quite hard to handle while parsing. If it's text only, it's as easy as 10 lines of code. See the iText manual for examples.
For writing word files there's only Apache POI. It can be a little tricky to figure out, but for such a simple task it shouldn't be any problem.