We have next code (SpireCDoc for Java 11.7.0):
- Code: Select all
public void generateTable() throws IOException {
Document doc = new Document();
Section section = doc.addSection();
Table table = section.addTable();
table.getTableFormat().setLayoutType(LayoutType.Fixed);
table.setPreferredWidth(new PreferredWidth(WidthType.Percentage, (short) 100));
for (int i = 0; i < 5; i++) {
TableRow row = table.addRow(5);
setColumnsWidth(row);
setColumnsBorderWidth(1, row);
setBorderColor(Color.GRAY, row);
setBackgroundColorColor(Color.white, row);
setHeight(i == 3 ? 150 : 40, row);
}
table.getTableFormat().getPaddings().setAll(5);
table.getTableFormat().setCellSpacing(1);
table.applyHorizontalMerge(0, 0, 1);
table.applyHorizontalMerge(0, 3, 4);
fillTable(table);
section.addParagraph();
try (FileOutputStream out = new FileOutputStream("test.docx")) {
doc.saveToStream(out, FileFormat.Docx_2019);
} catch (IOException e) {
throw new RuntimeException(e);
}
byte[] fileContent;
try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
doc.saveToStream(out, FileFormat.PDF);
fileContent = out.toByteArray();
} catch (IOException e) {
throw new RuntimeException(e);
}
try (FileOutputStream outputStream = new FileOutputStream("test.pdf")) {
outputStream.write(fileContent);
}
doc = new Document("test.docx");
try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
doc.saveToStream(out, FileFormat.PDF);
fileContent = out.toByteArray();
} catch (IOException e) {
throw new RuntimeException(e);
}
try (FileOutputStream outputStream = new FileOutputStream("test_after_reload.pdf")) {
outputStream.write(fileContent);
}
}
private void fillTable(Table table) {
TableRow headerRow = table.getFirstRow();
headerRow.getCells().get(0).addParagraph().appendHTML("<p>header1</p>");
headerRow.getCells().get(3).addParagraph().appendHTML("<p>header2</p>");
for (int i = 1; i < 5; i++) {
TableRow row = table.getRows().get(i);
TableCell cell = row.getCells().get(0);
setCellContent(cell, "1 test" + 1);
cell = row.getCells().get(1);
setCellContent(cell, "2 test" + 1);
cell = row.getCells().get(3);
setCellContent(cell, "3 test" + 1);
cell = row.getCells().get(4);
setCellContent(cell, "3 test" + 1);
}
}
private static void setCellContent(TableCell cell, String text) {
TextRange textRange = cell.addParagraph().appendText(text);
textRange.getCharacterFormat().setFontName("Tahoma");
textRange.getCharacterFormat().setFontSize(12);
textRange.getCharacterFormat().setTextColor(Color.BLACK);
}
private void setColumnsWidth(TableRow row) {
row.getCells().get(0).setCellWidth(16, CellWidthType.Percentage);
row.getCells().get(1).setCellWidth(32, CellWidthType.Percentage);
row.getCells().get(2).setCellWidth(4, CellWidthType.Percentage);
row.getCells().get(3).setCellWidth(16, CellWidthType.Percentage);
row.getCells().get(4).setCellWidth(32, CellWidthType.Percentage);
}
private void setBorderColor(Color color, TableRow row) {
row.getCells().get(0).getCellFormat().getBorders().setColor(color);
row.getCells().get(1).getCellFormat().getBorders().setColor(color);
row.getCells().get(3).getCellFormat().getBorders().setColor(color);
row.getCells().get(4).getCellFormat().getBorders().setColor(color);
}
private void setBackgroundColorColor(Color color, TableRow row) {
row.getCells().get(0).getCellFormat().setBackColor(color);
row.getCells().get(1).getCellFormat().setBackColor(color);
row.getCells().get(3).getCellFormat().setBackColor(color);
row.getCells().get(4).getCellFormat().setBackColor(color);
}
private void setColumnsBorderWidth(double lineWidth, TableRow row) {
row.getCells().get(0).getCellFormat().getBorders().setLineWidth((float) lineWidth);
row.getCells().get(1).getCellFormat().getBorders().setLineWidth((float) lineWidth);
row.getCells().get(3).getCellFormat().getBorders().setLineWidth((float) lineWidth);
row.getCells().get(4).getCellFormat().getBorders().setLineWidth((float) lineWidth);
}
private void setHeight(int height, TableRow row) {
row.setHeight(height);
row.setHeightType(TableRowHeightType.Exactly);
}
When we save Document as pdf without reload from disk, tables corrupted.
Please help to resolve this issue.
Thnks,
Andrei Chorin