Aspose.words for java 25.6版本 插入带表格的脚注报错

    @Test
    public void wordToTest2() throws Exception {
        String htmlContent = "<table border='1'><tr><td>脚注表格</td></tr></table>";


        com.aspose.words.Document doc = new com.aspose.words.Document();
        DocumentBuilder builder = new DocumentBuilder(doc);

        // Insert footnote reference in the main text
        builder.write("这是正文中的文字");

        // Create a footnote
        Footnote footnote = new Footnote(doc, FootnoteType.FOOTNOTE);
        footnote.getFont().setHidden(true); // Hide default footnote symbol

        // Ensure the footnote has at least one paragraph
        footnote.ensureMinimum();
        Paragraph footnoteParagraph = footnote.getFirstParagraph();

        // Move to the footnote's paragraph
        builder.moveTo(footnoteParagraph);

        // Insert HTML content
        builder.insertHtml(htmlContent);
    }

@qiufeng

Cause

The error you are encountering when inserting a footnote with a table in Aspose.Words for Java version 25.6 may be due to the way the footnote is being constructed or the content being inserted. The library has specific requirements for the types of nodes that can be added to footnotes, and it’s important to ensure that the content is compatible.

Solution

To resolve this issue, you can follow these steps:

  1. Ensure that you are correctly appending a paragraph to the footnote before inserting any content.
  2. Use the ensureMinimum() method on the footnote to ensure it has at least one paragraph before adding the table or HTML content.
  3. Make sure that the HTML content you are inserting is well-formed and does not contain any unsupported elements.

Code

Here’s an updated version of your code that incorporates these suggestions:

@Test
public void wordToTest2() throws Exception {
    String htmlContent = "<div>Your HTML content with a table here</div>"; // Ensure this is well-formed HTML

    com.aspose.words.Document doc = new com.aspose.words.Document();
    DocumentBuilder builder = new DocumentBuilder(doc);

    // Insert footnote reference in the main text
    builder.write("这是正文中的文字");

    // Create a footnote
    Footnote footnote = new Footnote(doc, FootnoteType.FOOTNOTE);
    footnote.getFont().setHidden(true); // Hide default footnote symbol

    // Ensure the footnote has at least one paragraph
    footnote.ensureMinimum();
    Paragraph footnoteParagraph = footnote.getFirstParagraph();

    // Move to the footnote's paragraph
    builder.moveTo(footnoteParagraph);

    // Insert HTML content
    builder.insertHtml(htmlContent);

    // Save the document
    doc.save("output.docx");
}

Next Steps

  1. Test the updated code to see if the error persists.
  2. If you continue to encounter issues, consider simplifying the HTML content to isolate the problem.
  3. Check the Aspose.Words documentation for any additional constraints regarding footnotes and tables.

Please implement these changes and let me know if you need further assistance!

@qiufeng 在添加其他节点之前,您需要在文件中插入该脚注。 您可以将脚注添加到段落中,或者像这样修改代码:

String htmlContent = "<table border=1><tr><td>脚注表格</td></tr></table>";

com.aspose.words.Document doc = new com.aspose.words.Document();
DocumentBuilder builder = new DocumentBuilder(doc);

// Insert footnote reference in the main text
builder.write("这是正文中的文字");

// Create a footnote
Footnote footnote = builder.insertFootnote(FootnoteType.FOOTNOTE, "");
footnote.getFont().setHidden(true); // Hide default footnote symbol

// Move to the footnote's paragraph
builder.moveTo(footnote.getFirstParagraph());

// Insert HTML content
builder.insertHtml(htmlContent);
1 Like