Change specific lines in a word file with Apache POI - java

I am using Apache POI lib in order to change automatically word files. I want to create a software which will have as an input some text, and put it in specific lines of the word file. How is it possible to have access in specific lines fo a word file? I mean how is it possible to create "variable areas" inside a word file.

You can use bookmarks inside Word documents using Microsoft Word. I have done it one week ago. I inserted bookmarks in the document by selecting the text that will be replaced then insert -> bookmark and type a new bookmark name. After that in your code you should implement something like this in your util classes :
Original code found here : http://apache-poi.1045710.n5.nabble.com/Replacing-the-value-of-the-bookmarks-td5710052.html

Related

overwrite existing table rows using DOCX4j

I am facing a problem in writing in MS Word file using Docx4j. The scenario is something like:
First user writes into the word file and supposedly he takes about 10 rows of the table and saves the file.
Second user comes and writes about 5 rows and saves the file again. But after sometime he again opens the file and modify something through java application.
Modified text is being added into the file instead of replacing the old text.
Now I want to replace his old text and want to save new paragraphs in table.
In simple words I want to replace old table rows with new one in Docx4j.
How can I reach to my desired rows to replace them?
Can anybody help me in this? Thanks
Use Replace to what you try do is just replace the String
Check this
https://www.docx4java.org/forums/docx-java-f6/replace-text-into-docx-within-a-table-t6.html

Writing to a specific csv column

I have made a little javaFX app that reads from the database and writes to a file using openCSV.Everything is fine now.In the file i have a column and its values contain the path of an image(Example : /home/user/folder1/image.jpg).What i have to do is replace all the values of this column ,with another value that contains only the last folder name and the image name(Example : folder1/image.jpg).I read about this and i realised that i had to write a lot of code.What i am asking is if there is another way of doing this? For example can i replace all of this values directly from the query or can i use another library that lets me write to a specific column?
Thanks :)

Insert PDF file into MSWord using Java POI API

I am using apache-poi api to create a word document.
I want to insert a PDF document into MS Word (normally we use Insert -> Object -> Create from file option in MS word to do this).
Is is possible to insert the PDF as an object into MS Word using Java ?
Regards,
Suthershan
To solve this you have to use OLE. After a quick research I found no example code for WORD but I found some code for EXCEL and an example for PPTX. Maybe it is helpfull to write some corresponding code for WORD.

Read text files and write it to excel in java

I have to read a text file and write it to an already existing excel file. The excel file is a customized excel sheet with different items in different columns. The items has different values for each of them... These items with there value can be found in a text file. But i dont have much idea as to how to do this.
E.g- example.txt
Name: John
Age=24
Sex=M
Graduate=M.S
example.xlsx
Age: Sex:
Name: Graduate:
Thanks in advance :)
Just as for so many other problems that need solved, there's an Apache library for that! In this case, it's the POI library. I've only used it for very basic spreadsheet manipulation, but managed that by just following a few tutorials. I'd link to one, but I can't now remember where it was.
Please see Apache POI-HSSF library for reading and writing Excel files with Java. There are some quick guides to get you started.
This post How to read and write excel file in java might help you.
You can also create a *.csv (comma separated value) file in Java. Just create a simple text file with CSV extension and put your values in there like that :
Age:,24,Sex:,M,
So you just separate your values with commas (or other delimiters like ';').
Every line in this file is a row, and every delimiter separates two columns. You won't be able to add colours/styles/formatting this way, but it gives you a file that is openable and understandable even without Excel (or other spreadsheet software).

Updating values of custom properties in word doc using java

I am not able to update value of a custom property in a word document using java.
I have a word document which contains a custom property with value 'stack'. Using java I want to change that value to 'overflow'. I used two approaches.
1) Using Apache POI
I set the org.apache.poi.hpsf.CustomProperties in org.apache.poi.hpsf.DocumentSummaryInformation and written it to POIFSFileSystem.
It does updates the value in word's customproperties table. But doesn't update the value for that respective property in the document. After the document is opened, I need to manually refresh the document to get that value updated.
2) Parse the document char by char and use the field codes DC3, DC4 and NAK to identify the location of custom properties. Replace the existing value with the new value.
Now the generated document contains the new values. But the problem here is, if the length of the old value and new value is different the document gets corrupted. I made sure the logic is good enough.
Any help would be appreciated.
To update the approach I took to solve the issue:
Using Office 2007 (docx) document as a template became easier. It is nothing but a zip document. You can open it using your zip application (winzip/7zip) and you can find many xml files inside it. document.xml contains the content, styles.xml contains formatting information and so on.
At runtime, I unzipped the document and parsed the document.xml, then used dom and updated with dynamic content. Custom properties are available in a separate xml file.
For 2003 users, they have to prepare the template using word application and save the document as XML then provide it as input.
No use of apache-poi now.

Categories