开发者

Splitting word file into multiple smaller word files using OLE Automation from java

开发者 https://www.devze.com 2023-01-17 04:03 出处:网络
I have been using OLE automation from java to access methods for word开发者_如何学Python. I managed to do the following using the OLE automation:

I have been using OLE automation from java to access methods for word开发者_如何学Python.

I managed to do the following using the OLE automation:

Open word document template file. Mail merge the word document template with a csv datasource file. Save mail merged file to a new word document file.

What i need to do now is to be able to open the mail merged file and then using OLE programmatically split it into multiple files. Meaning if the original mail merged file has 6000 pages and my max pages per file property is set to 3000 pages i need to create two new word document files and place the 1st 3000 pages in the one and the last 3000 pages into the other one.

On my first attempts i took the amount of rows in the csv file and multiplied it by the number of pages in the template to get the total amount of pages after it will be merged. Then i used the merging to create the multiple files. The problem however is that i cannot exactly calculate how many pages the merged document will be because in some case all say 9 pages of the template will not be used because of the data and the mergefields used. So in some cases one row will only create 3 pages (using the 9 page template) and others might create 9 pages (using the 9 page template) during mail merge time.

So the only solution is to merge all rows into one document and then split it into multiple documents therafter to ensure that the exact amount of pages like the 3000 pages property is indeed in each file until there are no more pages left from the original merged file.

I have tried a few things already by using the msdn site to get methods and their properties etc but have been unable to this.

On my last attempts now i have been trying to use GoTo to get to a specific page number and the remove the page. I was going to try do this one by one for each page until i get to where i want the file to start from and then save it as a new file but have been unable to do so as well.

Please can anyone suggest something that could help me out?

Thanks and Regards Sean

An example to open a word file using the OLE AUTOMATION from jave is included below:

Code sample
OleAutomation documentsAutomation = this.getChildAutomation(this.wordAutomation, "Documents");

int [ ] id = documentsAutomation.getIDsOfNames(new String[]{"Open"});

Variant[] arguments = new Variant[1];

arguments[0] = new Variant(fileName); // where filename is the absolute path to the docx file

Variant invokeResult = documentsAutomation.invoke(id[0], arguments);

private OleAutomation getChildAutomation(OleAutomation automation, String childName) {

int[] id = automation.getIDsOfNames(new String[]{childName});

Variant pVarResult = automation.getProperty(id[0]);

return(pVarResult.getAutomation());

}

Code sample


Sounds like you've pegged it already. Another approach you could take which would avoid building then deleting would be to look at the parts of your template that can make the biggest difference to the number of your template (that is where the data can be multi-line). If you then take these fields and look at the font, line-spacing and line-width type of properties you'll be able to calculate the room your data will take in the template and limit your data at that point. Java FontMetrics can help you with that.

0

精彩评论

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