Convert RTF Word Document containing Tables to PDF File using Java & Preserve Overall Layout

sample.zip (22.1 KB)

We are facing some issues while converting some RTF files, when I open the file in any word processing software I see that the content is rendered correctly, 3 pages in total and the content is aligned correctly and all tables printed correctly.

we don’t use any option for the aspose conversion practically we only use the following lines:

	try {
		Document rtfDocument = new Document(rtfFile.getPath());
		rtfDocument.save(pdfFilePath);
	} catch (Exception exception) {

		throw new PdfConverterException.AsposeConversionException(
				String.format("Unable to convert file %s to PDF format",
						rtfFile.getName()),
				conversionRequestEvent, exception);
	}

but the output that aspose is offering is not rendered correctly, it created another page and also created some jump lines and the content of the tables are not printed correctly.

Attached you can find the RTF source file and the PDF output with all our different issues.

Please let us know what is happening with this specific conversion.

@estebal,

We have logged this problem in our issue tracking system with ID WORDSNET-20896. We will further look into the details of this problem and will keep you updated on the status of correction. We apologize for your inconvenience.

@estebal,

You can use the following code as a workaround:

Document doc = new Document("C:\\Temp\\sample\\sample.rtf");

for (Section section : doc.getSections())
    for (Table table : section.getBody().getTables())
        for (Row row : table.getRows())
            for (Cell cell : row.getCells())
                if (cell.getText().startsWith("       ")) {
                    Run run = cell.getFirstParagraph().getRuns().get(0);
                    run.setText(trimStart(run.getText()));
                }

doc.save("C:\\Temp\\sample\\out.pdf"); 

public static String trimStart(String value) {
    // Remove leading spaces.
    return value.replaceFirst("^\\s+", "");
}

Please note that this workaround can be applied when the Aspose.Words for Java 20.9 (September release) becomes available.