0

How to convert a ".dotx" Word template to a plain ".docx" using a POI APIs or Docx4j?

JasonPlutext
  • 15,352
  • 4
  • 44
  • 84
youssef elhayani
  • 625
  • 8
  • 28
  • 3
    Add some code that you have tried. so, we can help you. – Java-Dev Jan 26 '19 at 09:50
  • 1
    What happens when you change the file extension from .dotx to .docx? – Emmanuel Rosa Jan 26 '19 at 10:15
  • 1
    For docx4j, please see https://github.com/plutext/docx4j/blob/master/src/samples/docx4j/org/docx4j/samples/TemplateAttach.java#L57 – JasonPlutext Jan 27 '19 at 04:54
  • Because someone voted the comment "What happens when you change the file extension from .dotx to .docx? " as useful: The answer is simply that Word then will not opening that `*.docx` file then. The `*.dotx` and `*.docx` have different content types which are not magically changed when file extension in file name is changed. – Axel Richter Jan 27 '19 at 06:59
  • A typically Stackoverflow closing action here. Four of the five closing voters not having any experience using `apache poi` and/or `docx4j` but vote closing a question about it. Why? Because the question cannot be answered? No, it really could be answered (see my answer and the comment of @JasonPlutext. But the question is short and do not contains some code. But why should it be more verbose? And how should it contain code the questioner don't knows and that's why he asks for? – Axel Richter Jan 27 '19 at 07:50
  • @JasonPlutext actually the methode cloneAs() is not working , i get this message 'The method cloneAs(String) is undefined for the type WordprocessingMLPackage' do you have any suggestions, thank you . – youssef elhayani Jan 28 '19 at 08:35
  • [public OpcPackage cloneAs(String targetContentType)](https://github.com/plutext/docx4j/blob/master/src/main/java/org/docx4j/openpackaging/packages/OpcPackage.java#L960) : "@since 6.1.0". – Axel Richter Jan 28 '19 at 10:04
  • @AxelRichter Thanks a lot ! – youssef elhayani Jan 28 '19 at 10:31

1 Answers1

8

The need is changing the content type of /word/document.xml from application/vnd.openxmlformats-officedocument.wordprocessingml.template.main+xml to application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml.

Example using apache poi 4.0.1:

import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.apache.poi.xwpf.usermodel.*;

public class WordReadDOTXSaveDOCX {

 public static void main(String[] args) throws Exception {

  XWPFDocument document = new XWPFDocument(new FileInputStream("StudentReport.dotx"));
  document.getPackage().replaceContentType(
   "application/vnd.openxmlformats-officedocument.wordprocessingml.template.main+xml",
   "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml");

  FileOutputStream out = new FileOutputStream("TheDocumentFromDOTXTemplate.docx");
  document.write(out);
  out.close();
  document.close();
 }
}
Axel Richter
  • 56,077
  • 6
  • 60
  • 87
  • Hello Axel Richter, I Hope you're fine, sorry for being late , i just try this code today but i get a docx file with a message error 'The file cannot be opened because there are problems with the contents' , thanks for the help. – youssef elhayani Jan 28 '19 at 08:30
  • @youssef elhayani: This code is tested and works for me using `apache poi 4.0.1`. If it does not work for you, please refine your question. Show the exact code which does not wor for you. Tell us `apache poi`version you are using. Provide an example `*.dotx` file using which it does not work. – Axel Richter Jan 28 '19 at 09:10
  • accually i used apache poi 3.13 and i used the same come that you did. there the dependency : org.apache.poi poi 3.13 – youssef elhayani Jan 28 '19 at 09:27
  • I will try apache poi 4.0.1 and see if it work – youssef elhayani Jan 28 '19 at 09:30
  • Thank You !! it's Working , it's all about the version of apache POI ! – youssef elhayani Jan 28 '19 at 09:33
  • Hi guys, I am looking for a .dot file reading example and I stumbled upon this stackoverflow answer. I know it is for .dotx but it doesn't work for .dot, or at least I couldn't figure it out on my own, can you help me? – Máté Tősér Jan 04 '23 at 20:38
  • If you do not use any library this needs to be chnaged inside "[Content_Types].xml" – Huhngut Jan 24 '23 at 11:42