0

我最近正在使用 apose words java。

在我的第一页中,我有一个需要合并的表,它可以增长任何大小,没有固定的行数,并且在我的第一页的末尾,我想保留一些内容(例如联系方式)是固定的。(注意:我无法在页脚或脚注部分保留联系方式,因为我需要确保某些格式不能在页脚或脚注部分保留)

随着表格行数的增加,我的内容正在下降,但我想在第一页的末尾修复它。如果表格变大,想跳过内容并在下一页呈现表格。

有什么解决方案/解决方法吗?

我的预期结果如下......

第 1 页 开始

动态表第 1 行

动态表第 2 行

动态表第 3 行

联系方式,想在我的第一页末尾修复

第 1 页结束

第 2 页开始

动态表第 4 行

动态表第 5 行

...........

4

1 回答 1

0

对于您的方案,理想情况下应在页脚中设置联系方式。这是可能的,但风险很大。

首先在 Aspose.Words 或 MS Word 中创建一个新文档,它将用作模板。

  1. 在顶部添加一个空白表格
  2. 在空白表之后添加联系方式
  3. 添加书签,在联系方式之后

模板文件

现在,使用 Aspose.Words,您可以在每次在表格中添加新行时检查书签的位置。如果书签位于第 1 页,则将新行添加到第一个表。如果书签位于第 2 页,则将新行添加到第二个表。下面是向表中添加行的示例代码,将联系人详细信息固定在第 1 页上。

模板文档:谷歌驱动链接 Java源代码如下。

public static void main(String[] args)
{
    try
    {
        String template = Common.DATA_DIR + "Contact Template.docx";
        String saveDocument = Common.DATA_DIR + "Contact with tables.docx";
        String bookmarkNameContact = "ContactEnd";

        // Load the template
        com.aspose.words.Document wordDoc = new com.aspose.words.Document(template);
        DocumentBuilder builder = new DocumentBuilder(wordDoc);

        // Find the contacts bookmark
        com.aspose.words.Bookmark bookmarkContact = wordDoc.getRange().getBookmarks().get(bookmarkNameContact);

        // Set the table with null
        com.aspose.words.Table table = null;

        // Add some rows
        for (int i = 0; i < 50; i++)
        {
            // If contacts bookmark is on 1st page, add new rows to first table
            if (getBookmarkPage(wordDoc, bookmarkContact) == 1)
            {
                table = (com.aspose.words.Table) wordDoc.getChild(NodeType.TABLE, 0, true);
            } else
            {
                // If the contacts bookmark is on second page, add rows to second table
                table = (com.aspose.words.Table) wordDoc.getChild(NodeType.TABLE, 1, true);
                // If there is no second table, create it
                if (table == null)
                {
                    table = createNewTable(wordDoc, bookmarkContact);
                }
            }

            // Add rows dynamically to either first or second table
            addRow(wordDoc, table, "some text " + i);
        }

        // Save the document
        wordDoc.save(saveDocument);

    } catch (Exception ex)
    {
        System.err.println(ex.getMessage());
    }
}

private static com.aspose.words.Table createNewTable(com.aspose.words.Document wordDoc, com.aspose.words.Bookmark bookmarkContact) throws Exception
{
    // Get the first table and clone it to create the second one
    com.aspose.words.Table firstTable = (com.aspose.words.Table) wordDoc.getChild(NodeType.TABLE, 0, true);
    com.aspose.words.Table table = (com.aspose.words.Table) firstTable.deepClone(true);

    // Add the second table after the bookmark
    bookmarkContact.getBookmarkEnd().getParentNode().getParentNode().appendChild(table);

    // Delete all its rows
    table.getRows().clear();

    return table;
}

// Add a new row to the table
private static void addRow(com.aspose.words.Document wordDoc, com.aspose.words.Table table, String text)
{
    // Create a new row
    com.aspose.words.Row row = new com.aspose.words.Row(wordDoc);
    row.getRowFormat().setAllowBreakAcrossPages(true);
    // Add it to the table
    table.appendChild(row);
    // Add cells to the row
    for (int iCell = 0; iCell < 4; iCell++)
    {
        // Create a new cell and set text inside it
        com.aspose.words.Cell cell = new com.aspose.words.Cell(wordDoc);
        cell.appendChild(new com.aspose.words.Paragraph(wordDoc));
        cell.getFirstParagraph().appendChild(new Run(wordDoc, text));
        cell.getFirstParagraph().getParagraphFormat().setSpaceAfter(0);

        row.appendChild(cell);
    }
}

private static int getBookmarkPage(com.aspose.words.Document wordDoc, com.aspose.words.Bookmark bookmarkContact) throws Exception
{
    // Find the page number, where our contacts bookmark is
    LayoutCollector collector = new LayoutCollector(wordDoc);
    return collector.getStartPageIndex(bookmarkContact.getBookmarkEnd());
}

我与 Aspose 一起担任开发人员宣传员。

于 2015-04-23T09:25:21.463 回答