spire.doc for java 设置table不超过页面宽度
时间: 2023-10-05 16:11:26 浏览: 218
要在 Spire.Doc for Java 中设置表格不超过页面宽度,您可以使用以下代码:
```java
// 加载文档
Document doc = new Document("input.docx");
// 获取第一个表格
Table table = doc.getSections().get(0).getTables().get(0);
// 获取页面宽度
float pageWidth = doc.getPageSetup().getPageWidth();
// 获取表格宽度
float tableWidth = table.getTableFormat().getPreferredWidth().getValue();
// 如果表格宽度大于页面宽度,就将表格宽度设置为页面宽度
if (tableWidth > pageWidth) {
table.getTableFormat().setPreferredWidth(new PreferredWidth(pageWidth, WidthType.Point));
}
// 保存文档
doc.saveToFile("output.docx", FileFormat.Docx_2013);
```
这段代码将会获取文档中第一个表格的宽度和页面的宽度,如果表格宽度大于页面宽度,就将表格宽度设置为页面宽度。最后,将修改后的文档保存为一个新的文件。
相关问题
spire.doc for java 设置table文字自动换行
要在 Spire.Doc for Java 中设置表格文字自动换行,您可以使用以下代码:
```java
// 加载文档
Document doc = new Document("input.docx");
// 获取第一个表格
Table table = doc.getSections().get(0).getTables().get(0);
// 获取表格中的所有行
TableRowCollection rows = table.getRows();
// 循环遍历每一行
for (TableRow row : rows) {
// 获取行中的所有单元格
TableCellCollection cells = row.getCells();
for (TableCell cell : cells) {
// 获取单元格中的段落
ParagraphCollection paragraphs = cell.getParagraphs();
for (Paragraph paragraph : paragraphs) {
// 设置段落中的文本自动换行
paragraph.getFormat().setWrapText(true);
}
}
}
// 保存文档
doc.saveToFile("output.docx", FileFormat.Docx_2013);
```
这段代码将会遍历文档中第一个表格的所有单元格,并将单元格中的段落的文本设置为自动换行。最后,将修改后的文档保存为一个新的文件。
spire.doc for java 设置table根据文字内容设置高度
要在 Spire.Doc for Java 中设置表格根据文字内容设置高度,您可以使用以下代码:
```java
// 加载文档
Document doc = new Document("input.docx");
// 获取第一个表格
Table table = doc.getSections().get(0).getTables().get(0);
// 获取表格中的所有行
TableRowCollection rows = table.getRows();
// 循环遍历每一行
for (TableRow row : rows) {
// 获取行中的所有单元格
TableCellCollection cells = row.getCells();
for (TableCell cell : cells) {
// 获取单元格中的段落
ParagraphCollection paragraphs = cell.getParagraphs();
for (Paragraph paragraph : paragraphs) {
// 获取段落中的文本
String text = paragraph.getText();
// 获取文本对应的字体
Font font = paragraph.getFormat().getFont();
// 获取文本对应的字体大小
float fontSize = font.getSize();
// 根据文本和字体大小计算单元格的高度
float cellHeight = (float) (Math.ceil(text.length() * fontSize * 1.2 / cell.getWidth()) * fontSize * 1.2);
// 设置单元格的高度
cell.setHeight(cellHeight);
}
}
}
// 保存文档
doc.saveToFile("output.docx", FileFormat.Docx_2013);
```
这段代码将会遍历文档中第一个表格的所有单元格,并根据单元格中的文本内容和字体大小计算单元格的高度,最后将修改后的文档保存为一个新的文件。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)