How to edit MS Word documents using Java?

前端 未结 4 1479
野趣味
野趣味 2020-12-21 20:43

I do have few Word templates, and my requirement is to replace some of the words/place holders in the document based on the user input, using Java. I tried lot of libraries

4条回答
  •  悲&欢浪女
    2020-12-21 21:28

    Stepping back a bit, there are about 4 different approaches for editing words/placeholders:

    • MERGEFIELD or DOCPROPERTY fields (if you are having problems with this in docx4j, then you have probably not set up your input docx correctly)
    • content control databinding
    • variable replacement on the document surface (either at the DOM/SAX level, or using a library)
    • do stuff as XHTML, then import that

    Before choosing one, you should decide whether you also need to be able to handle:

    • repeating data (eg adding table rows)
    • conditional content (eg entire paragraphs which will either be present or absent)
    • adding images

    If you need these, then MERGEFIELD or DOCPROPERTY fields are probably out (though you can also use IF fields, if you can find a library which supports them). And adding images makes DOM/SAX manipulation as advocated in one of the other answers, messier and error prone.

    The other things to consider are:

    • your authors: how technical are they? What does that imply for the authoring UI?
    • the "user input" you mention for variable replacement, is this given, or is obtaining it part of the problem you are solving?

提交回复
热议问题