Bug 51780 - [PATCH] Feature Request: How to convert a dotx file to docx via poi?
Summary: [PATCH] Feature Request: How to convert a dotx file to docx via poi?
Status: RESOLVED FIXED
Alias: None
Product: POI
Classification: Unclassified
Component: POI Overall (show other bugs)
Version: 3.8-dev
Hardware: PC All
: P2 normal (vote)
Target Milestone: ---
Assignee: POI Developers List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-09-07 09:37 UTC by Sebastian K.
Modified: 2012-02-29 07:55 UTC (History)
1 user (show)



Attachments
With this patch I am able to change the mimetype. (2.05 KB, text/plain)
2011-09-07 09:37 UTC, Sebastian K.
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Sebastian K. 2011-09-07 09:37:08 UTC
Created attachment 27465 [details]
With this patch I am able to change the mimetype.

I can't found a solution with the unmodified poi code for the following problem.

It should be possible to convert the following templates:

".dotx" to ".docx"
".dotm" to ".docm"
".xltx" to "xlsx"
".xltm" to ".xlsm"
".potx" to ".pptx"
".potm" to ".pptm"


I am able to do this after installing the attached patch.

Usage:
OPCPackage pack = new ZipPackage(stream, PackageAccess.READ_WRITE);
if (pack != null) {
  pack.getParts();
  ArrayList<PackagePart> list = pack.getPartsByContentType(oldContentType);
  for (PackagePart packagePart : list) {
    if (packagePart.getContentType().equals(oldContentType)) {
      PackagePartName partName = packagePart.getPartName();
      pack.getContenTypeManager().addContentType(partName, newContentType);
    }
  }
  ByteArrayOutputStream outputStream = new ByteArrayOutputStream(1024);
  pack.save(outputStream);
  //TODO save as file with the new file extention
}

Please include this patch for the next version or implement an other nicer way to change the contenttype.
Comment 1 Yegor Kozlov 2012-02-29 07:55:06 UTC
Replacement of content types supported in r1294998. 

There is a new method  OPCPackage.replaceContentType(String oldType, String newType) that does the job. 

Usage:
 
OPCPackage pkg = OPCPackage.open(new FileInputStream("macro-workbook.xlsm"));
pkg.replaceContentType(
  "application/vnd.ms-excel.sheet.macroEnabled.main+xml",
  "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml");


  FileOutputStream out = new FileOutputStream("workbook.xlsx");
  pkg.save(out);
  out.close();

Yegor