修复第一页末尾的一些内容aspose words java

Fixing some content at the end of first page aspose words java

我最近在使用 apose words java。

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

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

周围有solution/work吗?

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

第 1 页开始

动态Table行1

动态Table行2

动态Table行3

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

第1页结束

第 2 页开始

动态 table 第 4 行

动态table第5行

.......

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

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

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

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

模板文档:Google drive link 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 工作,担任开发人员推广员。