Java 修复第一页末尾的一些内容
我最近正在使用apose Word java 在我的第一页中,我有一个需要合并的表,它可以增长任何大小,没有固定数量的行,在我的第一页末尾,我希望保留一些内容(例如联系人详细信息)以进行固定。(注意:我无法在页脚或脚注部分保留联系人详细信息,因为我需要确保某些格式无法在页脚或脚注部分保留) 随着表格行数的增加,我的内容会下降,但我想在第一页的末尾修复它。如果表的大小变大,则希望跳过内容并在下一页中呈现表 是否有任何解决方案/解决方法 我的预期结果如下 第1页开始 动态表行1 动态表第2行 动态表第3行 联系人详细信息,希望在我的第一页末尾修复 第1页结束 第2页开始 动态表第4行 动态表第5行Java 修复第一页末尾的一些内容,java,aspose.words,Java,Aspose.words,我最近正在使用apose Word java 在我的第一页中,我有一个需要合并的表,它可以增长任何大小,没有固定数量的行,在我的第一页末尾,我希望保留一些内容(例如联系人详细信息)以进行固定。(注意:我无法在页脚或脚注部分保留联系人详细信息,因为我需要确保某些格式无法在页脚或脚注部分保留) 随着表格行数的增加,我的内容会下降,但我想在第一页的末尾修复它。如果表的大小变大,则希望跳过内容并在下一页中呈现表 是否有任何解决方案/解决方法 我的预期结果如下 第1页开始 动态表行1 动态表第2行 动态表
…对于您的场景,理想情况下,联系人详细信息应设置在页脚中。这是可能的,但风险很大 首先在Aspose.Words或MS Word中创建一个新文档,它将用作模板
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());
}
publicstaticvoidmain(字符串[]args)
{
尝试
{
字符串模板=Common.DATA_DIR+“Contact template.docx”;
字符串saveDocument=Common.DATA_DIR+“Contact with tables.docx”;
字符串bookmarkmanamecontact=“ContactEnd”;
//加载模板
com.aspose.words.Document wordDoc=新的com.aspose.words.Document(模板);
DocumentBuilder=新的DocumentBuilder(wordDoc);
//查找联系人书签
com.aspose.words.Bookmark Bookmark contact=wordDoc.getRange().getBookmarks().get(Bookmark name联系人);
//将表设置为null
com.aspose.words.Table Table=null;
//添加一些行
对于(int i=0;i<50;i++)
{
//如果联系人书签位于第一页,请向第一个表中添加新行
如果(getBookmarkPage(wordDoc,bookmarkContact)==1)
{
table=(com.aspose.words.table)wordDoc.getChild(NodeType.table,0,true);
}否则
{
//如果联系人书签位于第二页,请将行添加到第二个表中
table=(com.aspose.words.table)wordDoc.getChild(NodeType.table,1,true);
//如果没有第二个表,请创建它
如果(表==null)
{
table=createNewTable(wordDoc、bookmarkContact);
}
}
//将行动态添加到第一个或第二个表中
addRow(wordDoc,表格,“一些文本”+i);
}
//保存文档
wordDoc.save(保存文档);
}捕获(例外情况除外)
{
System.err.println(例如getMessage());
}
}
私有静态com.aspose.words.Table createNewTable(com.aspose.words.Document wordDoc、com.aspose.words.Bookmark Bookmark联系人)引发异常
{
//获取第一个表并克隆它以创建第二个表
com.aspose.words.Table firstTable=(com.aspose.words.Table)wordDoc.getChild(NodeType.Table,0,true);
com.aspose.words.Table=(com.aspose.words.Table)firstTable.deepClone(true);
//在书签后添加第二个表
bookmarkContact.getBookmarkEnd().getParentNode().getParentNode().appendChild(表);
//删除其所有行
table.getRows().clear();
返回表;
}
//向表中添加新行
私有静态void addRow(com.aspose.words.Document wordDoc、com.aspose.words.Table表格、字符串文本)
{
//创建新行
com.aspose.words.Row行=新建com.aspose.words.Row(wordDoc);
row.getRowFormat().setAllowBreakAcrossPages(true);
//将其添加到表中
表2.追加子项(行);
//将单元格添加到行中
对于(int-iCell=0;iCell<4;iCell++)
{
//创建新单元格并在其中设置文本
com.aspose.words.Cell=新的com.aspose.words.Cell(wordDoc);
cell.appendChild(新的com.aspose.words.paragration(wordDoc));
cell.getFirst段落().appendChild(新运行(wordDoc,text));
cell.getFirstParagraphFormat().getParagraphFormat().setSpaceAfter(0);
子行(单元格);
}
}
私有静态int-getBookmarkPage(com.aspose.words.Document-wordDoc、com.aspose.words.Bookmark-bookmarkContact)引发异常
{
//查找页码,即联系人书签所在的位置
LayoutCollector=新的LayoutCollector(wordDoc);
返回收集器.getStartPageIndex(bookmarkContact.getBookmarkEnd());
}
我与Aspose合作,担任开发人员福音传道者。@Saquib,感谢您的回复。我们将尝试您的解决方案。