docs-to-pdf-converter icon indicating copy to clipboard operation
docs-to-pdf-converter copied to clipboard

Not all annotations could be added to the document (the document doesn't have enough pages).

Open abhishek0892 opened this issue 7 years ago • 3 comments

org.apache.poi.xwpf.converter.core.XWPFConverterException: java.lang.RuntimeException: Not all annotations could be added to the document (the document doesn't have enough pages). at org.apache.poi.xwpf.converter.pdf.PdfConverter.doConvert(PdfConverter.java:70) at org.apache.poi.xwpf.converter.pdf.PdfConverter.doConvert(PdfConverter.java:38) at org.apache.poi.xwpf.converter.core.AbstractXWPFConverter.convert(AbstractXWPFConverter.java:45) at com.yeokhengmeng.docstopdfconverter.DocxToPDFConverter.convert(DocxToPDFConverter.java:28) at com.yeokhengmeng.docstopdfconverter.MainClass.main(MainClass.java:43) Caused by: java.lang.RuntimeException: Not all annotations could be added to the document (the document doesn't have enough pages). at com.lowagie.text.pdf.PdfDocument.close(Unknown Source) at com.lowagie.text.Document.close(Unknown Source) at org.apache.poi.xwpf.converter.pdf.internal.elements.StylableDocument.close(StylableDocument.java:161) at org.apache.poi.xwpf.converter.pdf.internal.PdfMapper.endVisitDocument(PdfMapper.java:167) at org.apache.poi.xwpf.converter.core.XWPFDocumentVisitor.start(XWPFDocumentVisitor.java:201) at org.apache.poi.xwpf.converter.pdf.PdfConverter.doConvert(PdfConverter.java:56) ... 4 more

abhishek0892 avatar Jul 09 '17 14:07 abhishek0892

Sorry, I'm not maintaining this library anymore as stated in the readme.

yeokm1 avatar Jul 10 '17 13:07 yeokm1

One possible trigger for this bug is to have the hyperlinks in header or footer, — see the solution for a similar issue in xdocreport.

ruv avatar Apr 22 '18 23:04 ruv

I had a similar error.

Caused by: org.apache.poi.xwpf.converter.core.XWPFConverterException: java.lang.RuntimeException: Not all annotations could be added to the document (the document doesn't have enough pages)

My headers/footers contained hyperlinks (not supported by Apache POI 3.12) I fixed the conversion by remove hyperlinks from headers / footers

This is Kotlin code but you can easily convert to java code

   fun removeHeaderFooterHyperLinks(doc: XWPFDocument) {
        val policy = doc.headerFooterPolicy
        val headers = policy.defaultHeader?.paragraphs
        val footers = policy.defaultFooter?.paragraphs

        removeHyperlinks(headers, footers)
    }

    fun removeHyperlinks(vararg paragraphs: Collection<XWPFParagraph>?) {
        if (paragraphs.isNullOrEmpty()) return // early exit

        paragraphs.flatMap { it?.toList() ?: emptyList() }.forEach {

            // Copy hyperlinks list (because iterator.hasNext does not work correctly)
            val hyperlinks = it.ctp.hyperlinkList.toList()

            for (hyperlink in hyperlinks) {
                LOG.debug("Remove hyperlink from headers/footers : {}", hyperlink.getRArray(0).getTArray(0).stringValue)
                it.ctp.removeHyperlink(0)
            }
        }
    }

fayeulle avatar Nov 13 '20 14:11 fayeulle