We use your enterprise extension for procesing special Word documents.
One task ist to merge another document in the main document. This worked so far very well. Now i got a new main document and this time a get an exception when I include the same other document. The main document has many content controls (SdtBlock elements) in it, and one of these content controls is the marker where to insert the other document.
I have no idea, what the problem could be
Here is the exception:
13:28:25.917 [main] INFO com.plutext.merge.DocumentBuilder -
source document 1 (false
13:28:26.088 [main] ERROR com.plutext.merge.DocumentBuilder - org.docx4j.wml.SdtBlock cannot be cast to org.docx4j.wml.ContentAccessor
java.lang.ClassCastException: org.docx4j.wml.SdtBlock cannot be cast to org.docx4j.wml.ContentAccessor
at com.plutext.merge.DocumentBuilder.fixRange(DocumentBuilder.java:1394) [docx4j-enterprise-3.2.0.5.jar:na]
at com.plutext.merge.DocumentBuilder.fixRanges(DocumentBuilder.java:1302) [docx4j-enterprise-3.2.0.5.jar:na]
at com.plutext.merge.DocumentBuilder.appendDocument(DocumentBuilder.java:722) [docx4j-enterprise-3.2.0.5.jar:na]
at com.plutext.merge.DocumentBuilder.buildDocument(DocumentBuilder.java:469) [docx4j-enterprise-3.2.0.5.jar:na]
at com.plutext.merge.DocumentBuilder.buildOpenDocument(DocumentBuilder.java:262) [docx4j-enterprise-3.2.0.5.jar:na]
at at.rcm.reporting.rechenschaftsbericht.docx4j.DocumentProcessor.insertDocument(DocumentProcessor.java:131) [classes/:na]
This is my source where the exception occours:
- Code: Select all
public void insertDocument(String alias, String value) {
MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
Map mappings = null;//documentData.getData();
List<BlockRange> blockRanges = new ArrayList<BlockRange>();
try {
// Lade Dokument
WordprocessingMLPackage fbMLPackage = Docx4J.load(new File(value));
// Page Break entfernen
Body fbBody = fbMLPackage.getMainDocumentPart().getContents().getBody();
List<Object> fbContent = fbBody.getContent();
for (Object o : fbContent) {
if (o instanceof P) {
removePageBreak(((P)o).getContent());
}
}
Body body = documentPart.getContents().getBody();
int index = getIndexOfSdt(body, alias);
if (index == -1) {
LOG.error("insertDocument", "Couldn't find index");
return;
}
BlockRange preFondebestimmungen = new BlockRange(wordMLPackage, 0, index);
BlockRange fondsbestimmungen = new BlockRange(fbMLPackage);
fondsbestimmungen.setFooterBehaviour(BlockRange.HfBehaviour.INHERIT);
fondsbestimmungen.setHeaderBehaviour(BlockRange.HfBehaviour.INHERIT);
fondsbestimmungen.setSectionBreakBefore(BlockRange.SectionBreakBefore.CONTINUOUS);
fondsbestimmungen.setRestartPageNumbering(false);
BlockRange postFondebestimmungen = new BlockRange(wordMLPackage, index + 1);
postFondebestimmungen.setSectionBreakBefore(BlockRange.SectionBreakBefore.CONTINUOUS);
blockRanges.add(preFondebestimmungen);
blockRanges.add(fondsbestimmungen);
blockRanges.add(postFondebestimmungen);
} catch (Docx4JException e) {
e.printStackTrace();
}
DocumentBuilder documentBuilder = new DocumentBuilder();
try {
wordMLPackage = documentBuilder.buildOpenDocument(blockRanges);
} catch (InvalidFormatException e) {
e.printStackTrace();
}
}
private int getIndexOfSdt(Body body, String alias) {
int index = 0;
List<Object> all = body.getContent();
for (Object o : all ) {
if (o instanceof SdtBlock) {
SdtPr sdtPr= ((SdtBlock)o).getSdtPr();
if (sdtPr!=null) {
for (Object pr : sdtPr.getRPrOrAliasOrLock() ) {
// Annoyingly, it may be wrapped in javax.xml.bind.JAXBElement
pr = XmlUtils.unwrap(pr);
if (pr instanceof SdtPr.Alias && ((SdtPr.Alias)pr).getVal().equals(alias)) {
return index;
}
}
}
}
index++;
}
return -1;
}
Thanks for you help. Do you need sample documents ?
Regards
Harald