【问题标题】:Fixing some content at the end of first page aspose words java修复第一页末尾的一些内容 aspose 单词 java
【发布时间】:2015-04-23 17:25:21
【问题描述】:

我最近正在使用 apose words java。

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

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

是否有任何解决方案/解决方法?

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

第 1 页开始

动态表格第一行

动态表格第 2 行

动态表格第 3 行

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

第 1 页结束

第 2 页开始

动态表格第 4 行

动态表格第 5 行

........

【问题讨论】:

    标签: java aspose.words


    【解决方案1】:

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

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

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

    现在,使用 Aspose.Words,您可以在每次在表格中添加新行时检查 书签 的位置。如果书签位于第 1 页,则将新行添加到第一个表。如果书签位于第 2 页,则将新行添加到第二个表。下面是向表中添加行的示例代码,将联系人详细信息固定在第 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 合作,担任开发人员宣传员。

    【讨论】:

    • @Saquib,感谢您的回复。我们将试用您的解决方案。
    猜你喜欢
    • 1970-01-01
    • 2021-03-16
    • 1970-01-01
    • 2019-11-09
    • 1970-01-01
    • 2021-07-12
    • 1970-01-01
    • 2017-04-12
    • 1970-01-01
    相关资源
    最近更新 更多