1

I would like to know, how can i reuse one template (with one page inside and some variables) multiple times a XWPFDocument object.

My idea is:

  • load the template once in a XWPFDocument as an template-object
  • clone/create/copy the template-object with all his styles and headers etc
  • fill the clone with content
  • add this clone to the destination-XWPFDocument

I got this work for one single page only. When i try to clone/create/copy the template-object it will lose all his style informations.

How to copy a paragraph of .docx to another .docx withJava and retain the style

How to copy some content in one .docx to another .docx , using POI without losing format?

Community
  • 1
  • 1
blub
  • 359
  • 6
  • 23

2 Answers2

2

POI probably does not support this out of the box, but I have done a similar thing in my project poi-mail-merge, it works with the underlying XML to repeatedly replace markers in a template Microsoft Word document and combine the results into one resulting document.

So it basically duplicates the template document multiple times into the resulting document.

See here for how I do it there, basically I work on the XML body text and do replacements/changes there and then append it onto the result document.

centic
  • 15,565
  • 9
  • 68
  • 125
  • Thank you. I tried already to append Documents based on this [link](http://stackoverflow.com/questions/2494549/is-there-any-java-library-maybe-poi-which-allows-to-merge-docx-files) but it was very slow. How are your experience with poi-mail-merge ? I need to append 100 pages in one document. – blub Feb 12 '16 at 12:31
  • I only created not too-complex pages with up to 20 pages in the result-document, but it was fairly quick and as I work on the underlying XML, the main work will be to construct the XML and parse it back together at the end. Furthermore there is likely some improvement possible by only parsing the XML at the end. – centic Feb 12 '16 at 14:38
  • With my template i need to use replacing MS Word mergefields and through a table as well. Do you have any experience with that ? – blub Feb 12 '16 at 16:03
  • I receive this exception `org.apache.xmlbeans.impl.values.XmlValueDisconnectedException` after appending something and accessing properties of the `XWPFDocument`. – blub Feb 12 '16 at 17:48
  • You either built an invalid document somehow or you may need to write out and read back the document (possibly into ByteArray streams) before you can use the higher level APIs again – centic Feb 13 '16 at 07:36
  • I've tried to write it out and read it back but it doesn't work when i use the poi-mail-merge – blub Feb 15 '16 at 10:21
0

POI Mail Merge propably helps in other cases but in my case it doesn't work.

My Workaround is to update my Template-XWPFDocument to the needed structure first, save it temporarily and read it back into a XWPFDocument-object.

Here the steps:

  • Read the template-file into a XWPFDocument
  • Read the records from data-file e.g. csv
  • Calculate the numbers of pages related to the data-records
  • Get the Bodyelements-Objects from the Template-XWPFDocument
  • Create new Bodyelements (depending to the numbers of pages) in the Template-XWPFDocument and replace them with the same Objects that we get before
  • Save the updated Template-XWPFDocument temporarily
  • Read the temporarily saved Template into a XWPFDocument
  • Replace all placeholder and fill them with your CSV-Data

Hope this helps somebody

blub
  • 359
  • 6
  • 23