Page 1 of 1

Exception when merging documents

PostPosted: Thu Jul 23, 2015 10:36 pm
by HarryVienna
Hello!

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

Re: Exception when merging documents

PostPosted: Thu Jul 23, 2015 10:57 pm
by HarryVienna
Perhaps this can be the reason:

This is the XML part of the document that works:

Code: Select all
<w:bookmarkStart w:id="27" w:name="_GoBack" w:displacedByCustomXml="next"/>
      <w:sdt>
         <w:sdtPr>
            <w:alias w:val="Text_Fondsbestimmungen"/>
            <w:tag w:val="include"/>
            <w:id w:val="-84386685"/>
            <w:placeholder>
               <w:docPart w:val="DefaultPlaceholder_1082065158"/>
            </w:placeholder>
         </w:sdtPr>
         <w:sdtEndPr/>
         <w:sdtContent>
            <w:p w:rsidR="00EA6113" w:rsidRDefault="00C562E7">
               <w:pPr>
                  <w:spacing w:line="240" w:lineRule="auto"/>
               </w:pPr>
               <w:r>
                  <w:t>Fondsbestimmungen</w:t>
               </w:r>
            </w:p>
         </w:sdtContent>
      </w:sdt>
      <w:bookmarkEnd w:id="27" w:displacedByCustomXml="prev"/>


And here the document that does not work:

Code: Select all
<w:bookmarkStart w:id="70" w:name="_Toc418591205" w:displacedByCustomXml="next"/>
      <w:sdt>
         <w:sdtPr>
            <w:alias w:val="Überschrift_Anhang"/>
            <w:tag w:val="Text"/>
            <w:id w:val="-147053134"/>
            <w:placeholder>
               <w:docPart w:val="DFFE9A984BDE4D3080AA6E6611A0DFE8"/>
            </w:placeholder>
         </w:sdtPr>
         <w:sdtContent>
            <w:p w:rsidR="007D5DAD" w:rsidRDefault="007D5DAD" w:rsidP="007D5DAD">
               <w:pPr>
                  <w:pStyle w:val="berschrift1RBHJB"/>
               </w:pPr>
               <w:r>
                  <w:t>Anhang</w:t>
               </w:r>
            </w:p>
            <w:bookmarkEnd w:id="70" w:displacedByCustomXml="next"/>
         </w:sdtContent>
      </w:sdt>


You can see, that the bookmarkEnd is INSIDE the sdt block!? I don't know why word does this, but it's the original document...

Harald

Re: Exception when merging documents

PostPosted: Fri Jul 24, 2015 3:51 pm
by jason
Hi Harald

I just had a look at this, and I think I've fixed it. But if you could email me your docx, I'll verify the fix works before sending you the update.

thanks .. Jason

Re: Exception when merging documents

PostPosted: Fri Jul 24, 2015 10:18 pm
by HarryVienna
Hello Jason,

I sent you an email!

Thanks a lot,
Harald