Selected words are falling on the next line and spacing itself out

INPUT:

OUTPUT:

original file : The Effect of Digital Trade Development on Carbon Emission Reduction in China.docx (415.5 KB)

code snippet:

com.aspose.words.Document document = new com.aspose.words.Document(inputStream);
com.aspose.words.FolderFontSource folderFontSource = new com.aspose.words.FolderFontSource(getFontDirectory(), false, 1);
document.setFontSettings(new FontSettings());
document.getFontSettings().setFontsSources(new FontSourceBase[]{folderFontSource});
com.aspose.words.PdfSaveOptions pdfSaveOptions = new com.aspose.words.PdfSaveOptions();
pdfSaveOptions.setUpdateFields(false);
document.save(directoryPath + PATH_SEPARATOR + outputFileName, pdfSaveOptions);

@TandFSP
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): WORDSNET-25881

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

As a temporary workaround you can reset paragraph alignment like shown in the following code:

Document doc = new Document("C:\\Temp\\in.doc");
Iterable<Paragraph> paragraphs = doc.getChildNodes(NodeType.PARAGRAPH, true);
for (Paragraph p : paragraphs)
{
    if (p.getParagraphFormat().getAlignment() == ParagraphAlignment.DISTRIBUTED)
        p.getParagraphFormat().setAlignment(ParagraphAlignment.JUSTIFY);
}
doc.save("C:\\Temp\\out.pdf");

@TandFSP We have completed analysis of the issue and concluded to close it as not a bug. The problem happened because Kerning is enabled in the document. It is required to enable Text Shaping for correct rendering text.
Aspose.Words.Shaping.Harfbuzz package provides support for OpenType features in Aspose.Words using the HarfBuzz text shaping engine. You should enabling open type features to get the result closer to MS Word’s result. To achieve this you should add reference to Aspose.Words Shaping Harfbuzz plugin:

<dependency>
	<groupId>com.aspose</groupId>
	<artifactId>aspose-words</artifactId>
	<version>23.8</version>
	<classifier>shaping-harfbuzz-plugin</classifier>
</dependency>

and use the following code for conversion:

Document doc = new Document("C:\\Temp\\in.docx");

// Enable open type features
doc.getLayoutOptions().setTextShaperFactory(com.aspose.words.shaping.harfbuzz.HarfBuzzTextShaperFactory.getInstance());

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

out.pdf (257.1 KB)

The issues you have found earlier (filed as WORDSNET-25881) have been fixed in this Aspose.Words for Java 23.10 update.