Skip to content

Instantly share code, notes, and snippets.

@StefanKruk
Last active November 9, 2016 08:32
Show Gist options
  • Select an option

  • Save StefanKruk/e71e74d259aca29accb20665d9acb9e3 to your computer and use it in GitHub Desktop.

Select an option

Save StefanKruk/e71e74d259aca29accb20665d9acb9e3 to your computer and use it in GitHub Desktop.
package com.github.stefankruk.docx4j
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import javax.xml.bind.JAXBException;
import org.apache.commons.io.IOUtils;
import org.docx4j.jaxb.Context;
import org.docx4j.openpackaging.contenttype.ContentType;
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.PartName;
import org.docx4j.openpackaging.parts.WordprocessingML.AlternativeFormatInputPart;
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
import org.docx4j.relationships.Relationship;
import org.docx4j.wml.Br;
import org.docx4j.wml.CTAltChunk;
import org.docx4j.wml.P;
import org.docx4j.wml.R;
import org.docx4j.wml.STBrType;
/**
* Hilfsklasse zum Mergen von zwei .docx Dokumenten
*
*/
public class MergeDocx
{
private static long chunk = 0;
private static final String CONTENT_TYPE = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
/**
* Fügt zwei Dokumente zusammen.
*
* @param b1
* @param b2
* @return content
* @throws Exception
*/
public static byte[] mergeDocx(final byte[] b1, final byte[] b2) throws Exception
{
final ByteArrayInputStream in1 = new ByteArrayInputStream(b1);
final ByteArrayInputStream in2 = new ByteArrayInputStream(b2);
return mergeDocx(in1, in2);
}
/**
* Fügt zwei Dokumente zusammen.
*
* @param s1
* @param s2
* @return content
* @throws Exception
*/
public static byte[] mergeDocx(final InputStream s1, final InputStream s2) throws Exception
{
chunk = 0;
byte[] result = null;
final WordprocessingMLPackage target = WordprocessingMLPackage.load(s1);
addPageBreak(target);
insertDocx(target.getMainDocumentPart(), IOUtils.toByteArray(s2));
try (ByteArrayOutputStream out = new ByteArrayOutputStream())
{
target.save(out);
result = out.toByteArray();
}
return result;
}
/**
* Fügt ein Dokument zu einem anderen hinzu
*
* @param main
* @param bytes
* @throws Exception
*/
private static void insertDocx(final MainDocumentPart main, final byte[] bytes) throws Exception
{
final AlternativeFormatInputPart afiPart = new AlternativeFormatInputPart(
new PartName("/part" + (chunk++) + ".docx"));
afiPart.setContentType(new ContentType(CONTENT_TYPE));
afiPart.setBinaryData(bytes);
final Relationship altChunkRel = main.addTargetPart(afiPart);
final CTAltChunk chunk = Context.getWmlObjectFactory().createCTAltChunk();
chunk.setId(altChunkRel.getId());
main.addObject(chunk);
}
/**
* Fügt einen Seitenumbruch hinzu
*
* @param target
* @throws Docx4JException
* @throws JAXBException
*/
private static void addPageBreak(final WordprocessingMLPackage target) throws Docx4JException, JAXBException
{
final P p = new P();
final R r = new R();
final Br br = new Br();
br.setType(STBrType.PAGE);
r.getContent().add(br);
p.getContent().add(r);
target.getMainDocumentPart().addObject(p);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment