Creating Table using fixed column values causes ALL columns to be sized the same


#1

we are using the following code to add tables to a a word document that is being saved as a pdf:

Table wordTable = builder.startTable();

	builder.getCellFormat().getBorders().setLineStyle(LineStyle.SINGLE);
	builder.getRowFormat().setAllowBreakAcrossPages(false);
	int intTableCols = 4;
	
	double[] dColWidth = {72,144,72,144};
	
	//Write out the header row for the number of configured columns.
	for (int i = 0; i < intTableCols; i++) {
		//System.out.println("Hey we are in the for loop:"+i);
		builder.insertCell();
		
		builder.getCellFormat().setWidth(dColWidth[i]);
		

		}
	//End Table Header Row
	builder.endRow();

	
	wordTable.setAllowAutoFit(true);
	wordTable.autoFit(AutoFitBehavior.FIXED_COLUMN_WIDTHS);
	
	//Write out the content row for the number of configured columns.
	for (int i = 0; i < intTableCols; i++) {
				
	<a class="attachment" 
builder.insertCell();
				
				
				

				}
	//End Table content Row
	builder.endRow();

If the LAST column size is LARGER than the previous column size then the table sets ALL of the columns in the table to the last column size.

If the LAST column size is SMALLER than the previous column size then the table sets the columns to the specified widths. Supplying to pdf out put with this code.

One with the last column set to 144 and on with the last column set to 72.

Smalleraspose_Word_content_documentbuilder.pdf (14.6 KB)
aspose_Word_content_documentbuilder.pdf (13.8 KB)


#2

@paul.calhoun,

You can build logic on the following code to get the desired output:

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

Table wordTable = builder.startTable();

builder.getCellFormat().getBorders().setLineStyle(LineStyle.SINGLE);
builder.getRowFormat().setAllowBreakAcrossPages(false);

int intTableCols = 4;
double[] dColWidth = {72, 144, 72, 144};

for (int x = 0; x < 5; x++) {
    if (x == 0) {
        // header rows
        for (int i = 0; i < intTableCols; i++) {
            builder.insertCell();
            builder.getCellFormat().setWidth(dColWidth[i]);
        }
        builder.endRow();
    } else {
        // other rows
        for (int i = 0; i < intTableCols; i++) {
            builder.insertCell();
            builder.getCellFormat().setWidth(dColWidth[i]);
        }
        builder.endRow();
    }
}

builder.endTable();

wordTable.autoFit(AutoFitBehavior.FIXED_COLUMN_WIDTHS);

doc.save("E:\\Temp\\awjava-19.8.docx");
doc.save("E:\\Temp\\awjava-19.8.pdf");

#3

So basically, reguardless of how we construct the other rows, we have to set the column width property on EVERY column in EVERY row. Correct ?


#4

@paul.calhoun,

Yes, in this case, you need to specify widths for all Cells in Table. In case you have further inquiries or need any help, please let us know.