I was able to successfully combine all docx files using alt + chunk method by googling. However when I convert to pdf only the first document is exported.
Any ideas. No errors in the log
Thanks
- Code: Select all
public static InputStream mergeDocx(final List<InputStream> streams) throws Docx4JException, IOException, Exception {
WordprocessingMLPackage target = null;
final File generated = File.createTempFile("generated", ".docx");
int chunkId = 0;
Iterator<InputStream> it = streams.iterator();
while (it.hasNext()) {
InputStream is = it.next();
if (is != null) {
if (target == null) {
// Copy first (master) document
OutputStream os = new FileOutputStream(generated);
os.write(IOUtils.toByteArray(is));
os.close();
target = WordprocessingMLPackage.load(generated);
} else {
// Attach the others (Alternative input parts)
insertDocx(target.getMainDocumentPart(), IOUtils.toByteArray(is), chunkId++);
}
}
}
if (target != null) {
target.save(generated);
return new FileInputStream(generated);
} else {
return null;
}
}
private static void insertDocx(MainDocumentPart main, byte[] bytes, int chunkId) {
try {
AlternativeFormatInputPart afiPart = new AlternativeFormatInputPart(
new PartName("/part" + chunkId + ".docx"));
afiPart.setContentType(new ContentType(CONTENT_TYPE));
afiPart.setBinaryData(bytes);
Relationship altChunkRel = main.addTargetPart(afiPart);
CTAltChunk chunk = Context.getWmlObjectFactory().createCTAltChunk();
chunk.setId(altChunkRel.getId());
main.addObject(chunk);
} catch (Exception e) {
e.printStackTrace();
}
}
private static void generatePdf(File inputFile, String outputFolderPath) throws Exception {
// Font regex (optional)
// Set regex if you want to restrict to some defined subset of fonts
// Here we have to do this before calling createContent,
// since that discovers fonts
String regex = null;
// Windows:
// String
// regex=".*(calibri|camb|cour|arial|symb|times|Times|zapf).*";
// regex=".*(calibri|camb|cour|arial|times|comic|georgia|impact|LSANS|pala|tahoma|trebuc|verdana|symbol|webdings|wingding).*";
// Mac
// String
// regex=".*(Courier New|Arial|Times New Roman|Comic
// Sans|Georgia|Impact|Lucida Console|Lucida Sans Unicode|Palatino
// Linotype|Tahoma|Trebuchet|Verdana|Symbol|Webdings|Wingdings|MS Sans
// Serif|MS Serif).*";
PhysicalFonts.setRegex(regex);
// Document loading (required)
WordprocessingMLPackage wordMLPackage;
// Load .docx or Flat OPC .xml
System.out.println("Loading file from " + inputFile.getName());
wordMLPackage = WordprocessingMLPackage.load(inputFile);
// Refresh the values of DOCPROPERTY fields
FieldUpdater updater = new FieldUpdater(wordMLPackage);
updater.update(true);
String outputfilepath = outputFolderPath + "merged.pdf";
// All methods write to an output stream
OutputStream os = new java.io.FileOutputStream(outputfilepath);
// Since 3.3.0, Plutext's PDF Converter is used by default
System.out.println("Using Plutext's PDF Converter; add docx4j-export-fo if you don't want that");
Docx4J.toPDF(wordMLPackage, os);
System.out.println("Saved: " + outputfilepath);
return;
}