Aspose Java - 在列表值下面添加表

时间:2017-06-20 12:24:36

标签: java aspose.words

以下是我想要生成的格式:

enter image description here

根据我对API的有限经验,我知道如何制作列表

ListFormat listFormat = builder.getListFormat();
listFormat.setList(document.getLists().add(ListTemplate.NUMBER_DEFAULT));
builder.writeln("Component Specification")
listFormat.setListLevelNumber(1);
builder.writeln("Raw Material Specification")

现在我如何从列表转换到下表? 写一个新行会添加数字,即使在表格的单元格中也是如此。

如果我将listformat设置为null,则缩进将丢失。我想维护列表的缩进,但也添加下面的列表值

无论如何要做到这一点?

1 个答案:

答案 0 :(得分:0)

@Furqan,

您需要为您的要求设置Table LeftIndent属性。请查看以下示例代码段。

我是Aspose的开发者传道者Tilal Ahmad。

Document doc= new Document();
DocumentBuilder builder= new DocumentBuilder(doc);

ListFormat listFormat = builder.getListFormat();
listFormat.setList(doc.getLists().add(ListTemplate.NUMBER_DEFAULT));
builder.writeln("Component Specification");
builder.getListFormat().listIndent();
builder.getListFormat().getListLevel().setNumberStyle(NumberStyle.ARABIC);
builder.getListFormat().getListLevel().setNumberFormat("\u0000.\u0001");
builder.writeln("Raw Material Specification");
builder.getListFormat().removeNumbers();

Table table = new Table(doc);

// Add the table to the document.
doc.getFirstSection().getBody().appendChild(table);

Row row1 = new Row(doc);
row1.getRowFormat().setAllowBreakAcrossPages(true);
table.appendChild(row1);


// Create a cell and add it to the row
Cell cell1 = new Cell(doc);
cell1.getCellFormat().getShading()
        .setBackgroundPatternColor(Color.LIGHT_GRAY);
cell1.getCellFormat().setWidth(50);

// Add a paragraph to the cell as well as a new run with some text.
cell1.appendChild(new Paragraph(doc));
cell1.getFirstParagraph().appendChild(new Run(doc, "Col1"));

// Add the cell to the row.
row1.appendChild(cell1);

// We would then repeat the process for the other cells and rows in the
// table.
// We can also speed things up by cloning existing cells and rows.
row1.appendChild(cell1.deepClone(false));
row1.getLastCell().appendChild(new Paragraph(doc));
row1.getLastCell().getFirstParagraph()
        .appendChild(new Run(doc, "Col2"));

row1.appendChild(cell1.deepClone(false));
row1.getLastCell().appendChild(new Paragraph(doc));
row1.getLastCell().getFirstParagraph()
        .appendChild(new Run(doc, "Col3"));


Row row = new Row(doc);
row.getRowFormat().setAllowBreakAcrossPages(true);
table.appendChild(row);

// Create a cell and add it to the row
Cell cell = new Cell(doc);
cell.getCellFormat().setWidth(50);

// Add a paragraph to the cell as well as a new run with some text.
cell.appendChild(new Paragraph(doc));
cell.getFirstParagraph()
        .appendChild(new Run(doc, "Row 1, Cell 1 Text"));

// Add the cell to the row.
row.appendChild(cell);

row.appendChild(cell.deepClone(false));
row.getLastCell().appendChild(new Paragraph(doc));
row.getLastCell().getFirstParagraph()
        .appendChild(new Run(doc, "Row 1, Cell 2 Text"));

row.appendChild(cell.deepClone(false));
row.getLastCell().appendChild(new Paragraph(doc));
row.getLastCell().getFirstParagraph()
        .appendChild(new Run(doc, "Row 1, Cell 3 Text"));
// Add left indent to the table.
table.setLeftIndent(60);


doc.save("E:/Data/Test1.docx");