Table is not fitting to dynamically converted word document using DocumentBuilder

Hi Team

I am trying to create a table dynamically and saving as word document using Aspose.words 21.4.0 java. But if column data is increased table is not fitting to the page. last columns are overlapped with page and not visible.
Please find the code snippet below.

try {
			doc = new Document();

			DocumentBuilder builder = new DocumentBuilder(doc);
			PdfSaveOptions options = new PdfSaveOptions();
			options.setCompliance(PdfCompliance.PDF_A_1_B);
			SimpleDateFormat DateFor = new SimpleDateFormat("dd/MM/yyyy");

			// We call this method to start building the table.
			com.aspose.words.Table table = builder.startTable();
			table.getAllowAutoFit();
			
			builder.getFont().setSize(12);
			builder.insertCell();
			builder.write("Date Created");
			builder.getCellFormat().setWrapText(true);
			builder.insertCell();
			builder.write("Date LastModified");
			builder.insertCell();
			builder.write("ID");
			builder.insertCell();
			builder.write("Action");
			builder.insertCell();
			builder.write("User");
			builder.insertCell();
			builder.write("User Id");
			builder.insertCell();
			builder.write("Reason");
			builder.insertCell();
			//builder.getCellFormat().setPreferredWidth(PreferredWidth.fromPoints(5));
			builder.write("Comments");
			//builder.getBold();
			builder.endRow();

			while (rs.next()) {
				String CaseID = rs.getString(1);
				// System.out.println("CaseID : "+CaseID);
				String action = rs.getString(5);
				String reason = rs.getString(6);
				String userName = rs.getString(7);
				String userId = rs.getString(8);
				String description = rs.getString(9);
				Date createDate = rs.getDate(11);
				String dateCreated = DateFor.format(createDate);
				String dateTime = rs.getTime(11).toString();
				Date modifyDate = rs.getDate(12);
				String dateModified = DateFor.format(modifyDate);
				String modifyDateTime = rs.getTime(12).toString();
				builder.insertCell();
				builder.writeln(dateCreated);
				builder.write(dateTime);
				builder.insertCell();
				builder.writeln(dateModified);
				builder.write(modifyDateTime);
				builder.insertCell();
				builder.write(CaseID);
				builder.insertCell();
				if (action != null) {
					builder.write(action);
					builder.getCellFormat().setWrapText(true);
				}
				builder.insertCell();
				if (userName != null) {
					builder.write(userName);
				}
				builder.insertCell();
				if (userId != null) {
					builder.write(userId);
				}
				builder.insertCell();
				if (reason != null) {
					builder.write(reason);
				}
				builder.insertCell();
				builder.getCellFormat().setPreferredWidth(PreferredWidth.fromPoints(5));
				if (description != null) {
					builder.getCellFormat().setWrapText(true);
					builder.write(description);
				}
				builder.endRow();

			}
			table.setAllowAutoFit(true);
			table.autoFit(AutoFitBehavior.AUTO_FIT_TO_WINDOW);
			builder.endTable();
// Save the document to disk.

			doc.save(stream, options);

Please do needful.
Thanks & Regards
Sivasankar S

@ssamidis,

Please ZIP and attach the following resources here for testing:

  • Aspose.Words for Java 21.4 generated output DOCX and PDF files showing the undesired behavior
  • Your expected DOCX and PDF files showing the desired output. You can create these documents manually by using MS Word.
  • Please also create a standalone simplified Java Application (source code without compilation errors) that helps us to reproduce your current problem on our end and attach it here for testing. Please do not include Aspose.Words JAR files in it to reduce the ZIP file size.

As soon as you get these pieces of information ready, we will start further investigation into your scenario and provide you more information.

Hi awais.hafeez

As you request i did not included Aspose.Words JAR files.I have added a standalone java Application and you can find “testdocument.pdf” as a sample out put. The last column ‘comments’ is not correctly fitted in pdf.
Please do needful.
TestAsposeConversionProject.zip (69.2 KB)

Thanks

@ssamidis,

We are working on your query and will get back to you soon.

Hi @awais.hafeez

Gentle reminder.
Any update on following query.

Thanks
Sivasankar S

@ssamidis,

This does not seem to be a problem in Aspose.Words API; the problem occurs because of long string of characters “description2345678description12345678description12345678” without any spaces in between them which prevents text from wrapping to next line. One way to workaround is to increase the page width. You can first calculate the actual width of Table and then set page width accordingly.

Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);
PdfSaveOptions options = new PdfSaveOptions();
options.setCompliance(PdfCompliance.PDF_A_1_B);
SimpleDateFormat DateFor = new SimpleDateFormat("dd/MM/yyyy");

// We call this method to start building the table.
com.aspose.words.Table table = builder.startTable();
table.getAllowAutoFit();

builder.getFont().setSize(12);
builder.insertCell();
builder.write("Date Created");
builder.insertCell();
builder.write("Date LastModified");
builder.insertCell();
builder.write("ID");
builder.insertCell();
builder.write("Action");
builder.insertCell();
builder.write("User");
builder.insertCell();
builder.write("User Id");
builder.insertCell();
builder.write("Reason");
builder.insertCell();
//builder.getCellFormat().setPreferredWidth(PreferredWidth.fromPoints(5));
builder.write("Comments");
//builder.getBold();
builder.endRow();

for (int i = 0; i <= 2; i++) {
    String CaseID = "case 12345678";
    // System.out.println("CaseID : "+CaseID);
    String action = "Action 12345678";
    String reason = "Reason 12345678";
    String userName = "User name 12345678";
    String userId = "user id 12345678";
    String description = "description2345678description12345678description12345678 description 12345678";
    Date createDate = new Date(0);
    String dateCreated = DateFor.format(createDate);
    String dateTime = new Time(0).toString();
    Date modifyDate = new Date(0);
    String dateModified = DateFor.format(modifyDate);
    String modifyDateTime = new Time(0).toString().toString();
    builder.insertCell();
    builder.getCellFormat().setWrapText(true);
    builder.writeln(dateCreated);
    builder.write(dateTime);
    builder.insertCell();
    builder.getCellFormat().setWrapText(true);
    builder.writeln(dateModified);
    builder.write(modifyDateTime);
    builder.insertCell();
    builder.getCellFormat().setWrapText(true);
    builder.write(CaseID);
    builder.insertCell();
    if (action != null) {
        builder.getCellFormat().setWrapText(true);
        builder.write(action);
    }
    builder.insertCell();
    if (userName != null) {
        builder.getCellFormat().setWrapText(true);
        builder.write(userName);
    }
    builder.insertCell();
    if (userId != null) {
        builder.getCellFormat().setWrapText(true);
        builder.write(userId);
    }
    builder.insertCell();
    if (reason != null) {
        builder.getCellFormat().setWrapText(true);
        builder.write(reason);
    }
    builder.insertCell();
    if (description != null) {
        builder.getCellFormat().setWrapText(true);
        builder.write(description);
    }
    builder.endRow();

}

builder.endTable();

table.setAllowAutoFit(true);
table.autoFit(AutoFitBehavior.AUTO_FIT_TO_WINDOW);


LayoutCollector collector = new LayoutCollector(doc);
LayoutEnumerator enumerator = new LayoutEnumerator(doc);

double maxWidth = 0;
for (Row row : (Iterable<Row>) table.getRows()) {
    enumerator.setCurrent(collector.getEntity(row.getLastCell().getFirstParagraph()));

    while (enumerator.moveParent())
        if (enumerator.getType() == LayoutEntityType.ROW)
            break;

    // Get the width of Row
    double rowWidth = enumerator.getRectangle().getX() + enumerator.getRectangle().getWidth();

    if ((rowWidth > maxWidth))
        maxWidth = rowWidth;
}

Section section = ((Section) table.getAncestor(NodeType.SECTION));
section.getPageSetup().setPageWidth(maxWidth + section.getPageSetup().getRightMargin());

doc.save("C:\\Temp\\awjava-21.5.pdf", options);