docs-to-pdf-converter
docs-to-pdf-converter copied to clipboard
Not all annotations could be added to the document (the document doesn't have enough pages).
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
Sorry, I'm not maintaining this library anymore as stated in the readme.
One possible trigger for this bug is to have the hyperlinks in header or footer, — see the solution for a similar issue in xdocreport.
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)
}
}
}