简体   繁体   English

在首页aspose单词java的末尾修复一些内容

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

I am working with apose words java recently. 我最近正在使用apose单词java。

In my first page I have a table need to merge, which can grow any size, no fixed number of rows and at the end of my first page, I want to keep some content (for example contact details) to be fixed. 在我的第一页中,我需要合并一个表,该表可以增长任何大小,没有固定的行数,并且在我的第一页末尾,我想保留一些要固定的内容(例如,联系方式)。 (Note: I can't keep contact details in Footer or in foot note section because of some formatting I need to ensure which can't maintain in footer or foot note section) (注意:由于某些格式,我无法在页脚或脚注部分中保留详细联系信息,因为我需要确保某些格式无法在页脚或脚注部分中保留)

On growing of table as many rows, My content is going down, But I want to fix it at the end of my first page. 随着表格数量的增加,我的内容正在减少,但是我想在第一页的末尾修复它。 if table grows bigger in size, wanted to skip the content and render table in next page. 如果表格尺寸变大,则希望跳过内容并在下一页中呈现表格。

is there any solution/work around for this? 有什么解决方案/解决方法吗?

My expected results are like below.... 我的预期结果如下。

Page 1 Start Page 1开始

dynamic Table row1 动态表格第1行

dynamic Table row2 动态表格row2

dynamic Table row3 动态表格第3行

Contact Details ,wanted to fix at the end of my first page 联系方式,希望在我的第一页末尾进行修复

Page 1 end 第1页完

Page 2 Start Page 2开始

dynamic table row 4 动态表第4行

dynamic table row 5 动态表格第5行

........ ........

For your scenario, ideally the contact details should be set in a footer. 对于您的情况,理想的联系方式应在页脚中设置。 It is possible, but very risky. 可能,但是风险很大。

First create a new document, either in Aspose.Words or MS Word, it will be used as a template. 首先在Aspose.Words或MS Word中创建一个新文档,它将用作模板。

  1. Add a blank table on top 在顶部添加一个空白表
  2. Add contact details, after the blank table 在空白表格后添加联系方式
  3. Add a bookmark, after the contact details 在联系方式之后添加书签

模板文件

Now, using Aspose.Words, you can check the location of the bookmark , every time you are adding a new row in the table. 现在,使用Aspose.Words可以在每次在表中添加新行时检查书签的位置。 If bookmark is at page 1, add new row to the first table. 如果书签在第1页上,则将新行添加到第一个表中。 If bookmark is at page 2, add new row to the second table. 如果书签在第2页上,则将新行添加到第二个表中。 Below is the sample code that adds rows to the table, keeping the contact details fixed on page 1. 下面是将代码添加到表中的示例代码,使第1页上的联系方式保持不变。

Template document: Google drive link Java source code is given below. 模板文档: Google驱动器链接 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());
}

I work with Aspose as Developer Evangelist. 我与Aspose一起担任开发人员推广人员。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM