IllegalStateException: This operation requires the node to be inside the main document

Hi guys,

I am getting this exception when trying to import the attached document. This document was retrieved from the KDE test corpus
https://websvn.kde.org/trunk/tests/calligratests/interoperability/wordprocessing/docx/

Stack trace:

java.lang.IllegalStateException: This operation requires the node to be inside the main document.
at com.aspose.words.Node.zzZLU(Unknown Source) ~[aspose-words-16.1.0.jar:16.1.0.0]
at com.aspose.words.zzEH.zzY(Unknown Source) ~[aspose-words-16.1.0.jar:16.1.0.0]
at com.aspose.words.ShapeRenderer.(Unknown Source) ~[aspose-words-16.1.0.jar:16.1.0.0]
at com.aspose.words.zzX.zzZ(Unknown Source) ~[aspose-words-16.1.0.jar:16.1.0.0]
at com.aspose.words.zzFJ.zzZ(Unknown Source) ~[aspose-words-16.1.0.jar:16.1.0.0]
at com.aspose.words.zzX.zzY(Unknown Source) ~[aspose-words-16.1.0.jar:16.1.0.0]
at com.aspose.words.zzX.zzZ(Unknown Source) ~[aspose-words-16.1.0.jar:16.1.0.0]
at com.aspose.words.zzBJ.zzI(Unknown Source) ~[aspose-words-16.1.0.jar:16.1.0.0]
at com.aspose.words.zzBJ.visitShapeStart(Unknown Source) ~[aspose-words-16.1.0.jar:16.1.0.0]
at com.aspose.words.Shape.zzZ(Unknown Source) ~[aspose-words-16.1.0.jar:16.1.0.0]
at com.aspose.words.CompositeNode.acceptCore(Unknown Source) ~[aspose-words-16.1.0.jar:16.1.0.0]
at com.aspose.words.Shape.accept(Unknown Source) ~[aspose-words-16.1.0.jar:16.1.0.0]
at com.aspose.words.CompositeNode.acceptChildren(Unknown Source) ~[aspose-words-16.1.0.jar:16.1.0.0]
at com.aspose.words.CompositeNode.acceptCore(Unknown Source) ~[aspose-words-16.1.0.jar:16.1.0.0]
at com.aspose.words.Paragraph.accept(Unknown Source) ~[aspose-words-16.1.0.jar:16.1.0.0]
at com.aspose.words.CompositeNode.acceptChildren(Unknown Source) ~[aspose-words-16.1.0.jar:16.1.0.0]
at com.aspose.words.CompositeNode.acceptCore(Unknown Source) ~[aspose-words-16.1.0.jar:16.1.0.0]
at com.aspose.words.Body.accept(Unknown Source) ~[aspose-words-16.1.0.jar:16.1.0.0]
at com.aspose.words.CompositeNode.acceptChildren(Unknown Source) ~[aspose-words-16.1.0.jar:16.1.0.0]
at com.aspose.words.CompositeNode.acceptCore(Unknown Source) ~[aspose-words-16.1.0.jar:16.1.0.0]
at com.aspose.words.Section.accept(Unknown Source) ~[aspose-words-16.1.0.jar:16.1.0.0]
at com.aspose.words.CompositeNode.acceptChildren(Unknown Source) ~[aspose-words-16.1.0.jar:16.1.0.0]
at com.aspose.words.CompositeNode.acceptCore(Unknown Source) ~[aspose-words-16.1.0.jar:16.1.0.0]
at com.aspose.words.BuildingBlock.accept(Unknown Source) ~[aspose-words-16.1.0.jar:16.1.0.0]
at com.aspose.words.CompositeNode.acceptChildren(Unknown Source) ~[aspose-words-16.1.0.jar:16.1.0.0]
at com.aspose.words.CompositeNode.acceptCore(Unknown Source) ~[aspose-words-16.1.0.jar:16.1.0.0]
at com.aspose.words.GlossaryDocument.accept(Unknown Source) ~[aspose-words-16.1.0.jar:16.1.0.0]
at com.aspose.words.zzBJ.zzY(Unknown Source) ~[aspose-words-16.1.0.jar:16.1.0.0]
at com.aspose.words.Document.zzZ(Unknown Source) ~[aspose-words-16.1.0.jar:16.1.0.0]
at com.aspose.words.Document.save(Unknown Source) ~[aspose-words-16.1.0.jar:16.1.0.0]

Conversion code (Scala):

val options = new HtmlSaveOptions(SaveFormat.HTML)
options.setImagesFolder(imageDir.getPath)
options.setUseAntiAliasing(true)
options.setUseHighQualityRendering(true)
// Prevent scaling of images.
options.setScaleImageToShapeSize(false)
// Ensure the file names for images are sanitized.
options.setImageSavingCallback(FilenameSanitizingSavingCallback)
// Ensure external images are still linked.
options.setExportOriginalUrlForLinkedImages(true)
// Ensure lists are exported as 

and tags.

options.setExportListLabels(ExportListLabels.BY_HTML_TAGS)

for {
  // Load the document.
  asposeDoc <- Try(new AsposeDocument(file.getAbsolutePath))

  // Convert to HTML.
  _ <- Try(asposeDoc.save(htmlFile.getAbsolutePath, options))
} yield {}

object FilenameSanitizingSavingCallback extends IImageSavingCallback {
  /**
   * Handle an image save callback. This will force the filename to be lowercase.
   *
   * @param imageSavingArgs Information about the image.
   */*
  def imageSaving(imageSavingArgs: ImageSavingArgs): Unit = {
    // Ensure file names are lowercase and do not contain invalid characters.
    imageSavingArgs.setImageFileName(
      imageSavingArgs.getImageFileName.toLowerCase.replaceAll("[^-a-z0-9_\/\.]", "")
    )
  }
}

Hi Jeremy,

Thanks for your inquiry.

While using the latest version of Aspose.Words i.e. 16.1.0, we managed to reproduce this issue on our end. We have logged this issue in our bug tracking system. The ID of this issue is WORDSNET-13186. Your thread has also been linked to this issue and you will be notified as soon as it is resolved. Sorry for the inconvenience.

Best regards,

Thanks for the response. Is there a workaround or a part of the document we can remove to prevent this error?

Thanks,

Jeremy

Hi Jeremy,

Thanks for your inquiry. Please find attached a simplified document which causes the same exception. Hope, this helps.

Best regards,

For future readers, this is the workaround that we settled on. No guarantees that this will work for all cases, but it seems to work for our tests:

Try({
  // Only apply this to structured document tag nodes in the document that contain paragraphs
  // that contain shapes.
  val structuredBlocks = asposeDoc
    .getChildNodes(NodeType.STRUCTURED_DOCUMENT_TAG, true).asScala
    .map(*.asInstanceOf[StructuredDocumentTag])
    // Filter based on a paragraph containing a shape.
    .filter(*.getChildNodes.asScala
      .filter(*.isInstanceOf[Paragraph])
      .map(*.asInstanceOf[Paragraph])
      // Check if the paragraphs children are shapes.
      .exists(*.getChildNodes.asScala.exists(*.isInstanceOf[Shape]))
    )
  structuredBlocks.foreach(structuredBlock => {
    val parentNode = structuredBlock.getParentNode

    // Remove the structured block from the document, moving it’s children into the parent.
    // Using foldLeft to ensure that the children are inserted in order in the correct place
    // in the parent.
    structuredBlock.getChildNodes.asScala.foldLeft(structuredBlock.asInstanceOf[Node])({
      case (previous, node) => {
        node.remove()
        parentNode.insertAfter(node, previous)

        node
      }
    })
    structuredBlock.remove()
  })

  // This refreshes internal state so the changes made above are applied.
  asposeDoc.updatePageLayout()
})

Hi Jeremy,

Regarding WORDSNET-13186, we will inform you via this thread as soon as this issue is resolved. We apologize for any inconvenience.

Best regards,

The issues you have found earlier (filed as WORDSNET-13186) have been fixed in this .NET update and this Java update.


This message was posted using Notification2Forum from Downloads module by aspose.notifier.