Hi all,
I developed a method to create a pdf file containing a set of tables without any issues. I would like to transform this code to make the same file in the .docx extension. I was consulting the documentation and did not find anything related to colspan.
I would like the title 1 row, title 2 row, and comment to be the same width as attribute 1 and value 1. In aspose.pdf I used setColSpan(2), how could I implement the same in words?
Hi again,
These suggestions worked well for small tables. Now I’m facing a larger table, and the setHorizontalMerge method does not work.
This is my code:
if (indicatorInstance.getAttributeList().size() == 3)
{
Attribute attribute1 = attribute1;
Attribute attribute2 = attribute2;
Table table = builder.startTable();
int colSpan = (attribute1.getValueList().size()) * (attribute2.getValueList().size()) + 1;
Cell interventionAreaRow = builder.insertCell();
interventionAreaRow.getCellFormat().getShading().setBackgroundPatternColor(new Color(45, 163, 151));
interventionAreaRow.getFirstParagraph().getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
Font indicatorFont = builder.getFont();
indicatorFont.setSize(10);
indicatorFont.setColor(new Color(255, 255, 255));
indicatorFont.setBold(true);
builder.getCellFormat().setTopPadding(10);
builder.getCellFormat().setBottomPadding(10);
builder.getCellFormat().setHorizontalMerge(CellMerge.FIRST);
builder.write(indicatorInstance.getIndicator().getCategory().getName());
for (int i = 0; i < colSpan - 1; i++)
{
builder.insertCell();
builder.getCellFormat().setHorizontalMerge(CellMerge.PREVIOUS);
}
builder.endRow();
Cell indicatorNameRow = builder.insertCell();
indicatorNameRow.getCellFormat().getShading().setBackgroundPatternColor(new Color(255, 255, 255));
indicatorNameRow.getFirstParagraph().getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
indicatorFont.setSize(10);
indicatorFont.setColor(new Color(91, 87, 164));
indicatorFont.setBold(true);
builder.getCellFormat().setTopPadding(20);
builder.getCellFormat().setBottomPadding(20);
builder.getCellFormat().setHorizontalMerge(CellMerge.FIRST);
builder.write(indicatorInstance.getIndicator().getName());
for (int i = 0; i < colSpan - 1; i++)
{
builder.insertCell();
builder.getCellFormat().setHorizontalMerge(CellMerge.PREVIOUS);
}
builder.endRow();
builder.insertCell();
for (Value value : attribute1.getValueList())
{
builder.insertCell();
builder.write(value.getValue());
builder.getCellFormat().setHorizontalMerge(CellMerge.FIRST);
for (int j = 0; j < attribute2.getValueList().size() - 1; j++)
{
builder.insertCell();
builder.getCellFormat().setHorizontalMerge(CellMerge.PREVIOUS);
}
}
builder.endRow();
builder.insertCell();
for (int i = 0; i < attribute1.getValueList().size(); i++)
{
for (Value value2 : attribute2.getValueList())
{
builder.insertCell();
builder.write(value2.getValue());
}
}
builder.endRow();
Attribute columnAttribute = indicatorInstance.getAttributeList().get(2);
for (Value value : columnAttribute.getValueList())
{
builder.insertCell();
builder.write(value.getValue());
for (ValueInstance valueInstance : indicatorInstance.getValueInstanceList())
{
for (Value value1 : attribute1.getValueList())
{
for (Value value2 : attribute2.getValueList())
{
if (valueInstance.getValues().contains(value1) && valueInstance.getValues().contains(value2) && valueInstance.getValues().contains(value))
{
builder.insertCell();
builder.write(valueInstance.getValue());
}
}
}
}
builder.endRow();
}
if (indicatorInstance.getNote() != null && !indicatorInstance.getNote().isEmpty())
{
Cell noteCell = builder.insertCell();
noteCell.getCellFormat().getShading().setBackgroundPatternColor(new Color(255, 255, 255));
noteCell.getFirstParagraph().getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
indicatorFont.setSize(10);
indicatorFont.setColor(new Color(30, 32, 25));
indicatorFont.setBold(false);
builder.getCellFormat().setHorizontalMerge(CellMerge.FIRST);
builder.write("NOTE: " + indicatorInstance.getNote());
for (int i = 0; i < colSpan - 1; i++)
{
builder.insertCell();
builder.getCellFormat().setHorizontalMerge(CellMerge.PREVIOUS);
}
builder.endRow();
}
if (indicatorInstance.getAction() != null && !indicatorInstance.getAction().isEmpty())
{
Cell noteCell = builder.insertCell();
noteCell.getCellFormat().getShading().setBackgroundPatternColor(new Color(255, 255, 255));
noteCell.getFirstParagraph().getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
indicatorFont.setSize(10);
indicatorFont.setColor(new Color(30, 32, 25));
indicatorFont.setBold(false);
builder.getCellFormat().setHorizontalMerge(CellMerge.FIRST);
builder.write("ACTION: " + indicatorInstance.getAction());
for (int i = 0; i < colSpan - 1; i++)
{
builder.insertCell();
builder.getCellFormat().setHorizontalMerge(CellMerge.PREVIOUS);
}
builder.endRow();
}
table.setAlignment(HorizontalAlignment.CENTER);
builder.endTable();
}
The image below is the output of my code. How could I change it to make it work properly and obtain the table in the first image?
My next step would be to create a greater table, with 4 attributes (2 on columns and 2 on rows). Is it possible to set a vertical merge?
@irpps Since there is no access to your data objects, I have created a dummy code that produces the required table:
int colSpan = 12;
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Table table = builder.startTable();
// Start the first row
Cell interventionAreaRow = builder.insertCell();
interventionAreaRow.getCellFormat().getShading().setBackgroundPatternColor(new Color(45, 163, 151));
interventionAreaRow.getFirstParagraph().getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
Font indicatorFont = builder.getFont();
indicatorFont.setSize(10);
indicatorFont.setColor(new Color(255, 255, 255));
indicatorFont.setBold(true);
builder.getCellFormat().setTopPadding(10);
builder.getCellFormat().setBottomPadding(10);
// First cell in the row is not merged
builder.getCellFormat().setHorizontalMerge(CellMerge.NONE);
builder.write("TEST");
// The next cell is merged with the next
builder.insertCell();
builder.getCellFormat().setHorizontalMerge(CellMerge.FIRST);
for (int i = 0; i < colSpan - 1; i++)
{
// The rest cells are merged with previous
builder.insertCell();
builder.getCellFormat().setHorizontalMerge(CellMerge.PREVIOUS);
}
builder.endRow();
// The second row is simmilar to the first one.
Cell indicatorNameRow = builder.insertCell();
indicatorNameRow.getCellFormat().getShading().setBackgroundPatternColor(new Color(255, 255, 255));
indicatorNameRow.getFirstParagraph().getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
indicatorFont.setSize(10);
indicatorFont.setColor(new Color(91, 87, 164));
indicatorFont.setBold(true);
builder.getCellFormat().setTopPadding(20);
builder.getCellFormat().setBottomPadding(20);
// It is required to reset HorizontalMerge since it is inherited from the previous cell
builder.getCellFormat().setHorizontalMerge(CellMerge.NONE);
builder.write("TEST");
builder.insertCell();
builder.getCellFormat().setHorizontalMerge(CellMerge.FIRST);
for (int i = 0; i < colSpan; i++)
{
builder.insertCell();
builder.getCellFormat().setHorizontalMerge(CellMerge.PREVIOUS);
}
builder.endRow();
// In the third row the first cell is not merged
builder.insertCell();
builder.getCellFormat().setHorizontalMerge(CellMerge.NONE);
for (int i = 0; i < 3; i++)
{
// Each fourth cell is also not merged
builder.insertCell();
builder.getCellFormat().setHorizontalMerge(CellMerge.NONE);
builder.write("test");
// The next is the start of spanned cell
builder.insertCell();
builder.getCellFormat().setHorizontalMerge(CellMerge.FIRST);
for (int j = 0; j < 2; j++)
{
builder.insertCell();
builder.getCellFormat().setHorizontalMerge(CellMerge.PREVIOUS);
}
}
builder.endRow();
// In the fourth row cells are not merged
for (int i = 0; i < 13; i++)
{
builder.insertCell();
builder.getCellFormat().setHorizontalMerge(CellMerge.NONE);
}
builder.endRow();
builder.endTable();
table.setAlignment(HorizontalAlignment.CENTER);
doc.save("C:\\Temp\\out.docx");