We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Text direction (RTL / LTR)

Hi, I am trying to convert text (.txt) files to pdf. RTL langauges (Hebrew and Arabic) and LTR language (English). The text in the converted pdf file is alligned to the left for both RTLand LTR languages which is not the expected result.
I saw a ticket that similar to my issueL Bug in converting Text file (RTL-Hebrew language) to PDF format and I have tried to add it to my code. The RTL text are now alligned to the right but the LTR text was also alligned to the right.

I don’t want to detect the language of each file and then choose the direction. Can you please help me with this issue?

I am using Aspose.Words for Java 19.7 and have 30 day trial license.

Here are my files: files.zip (48.9 KB) and here is my code:

private static void convertDocToPdf(File file) {
String outputPdfFile = new StringBuilder().append(OUTPUT_PDF_FOLDER).append(File.separator).append(FilenameUtils.removeExtension(file.getName())).append(".pdf").toString();
try {
LoadOptions loadOptions = new LoadOptions();
loadOptions.setLoadFormat(LoadFormat.TEXT);
Document doc = new Document(file.getAbsolutePath(), loadOptions);
PageSetup ps = doc.getFirstSection().getPageSetup();
ps.setLeftMargin(0.36 * 72); // 0.5 inch
ps.setRightMargin(0.36 * 72);
for(Object para : doc.getChildNodes(NodeType.PARAGRAPH, true)){
((Paragraph)para).getParagraphFormat().setBidi(true);
}
doc.save(outputPdfFile, new PdfSaveOptions());
} catch (final Exception e) {
System.out.println(e);
}
}

@ghaj17

Please note that Aspose.Words mimics the behavior of MS Word. If you open the TXT file in MS Word, it asks for RTL and LTR direction. So, you are facing the expected behavior. However, we have logged a new feature request as WORDSNET-19067 for your case. You will be notified via this forum thread once this feature is available. We apologize for your inconvenience.

The issues you have found earlier (filed as WORDSNET-19067) have been fixed in this Aspose.Words for .NET 19.10 update and this Aspose.Words for Java 19.10 update.

@tahir.manzoor

I tested it again in Aspose.Words for Java 19.11 update (the latest one) with my code above and the issue still exists.

The results for Hebrew and Arabic conversions are good (because of the setBide(true) method for each paragraph), but the result for English conversion is not good.
If I remove the setBide(true) method, Hebrew and Arabic conversions are not good and English is good.

Can you please check it again from your side with the latest update?

@ghaj17

In the latest version of Aspose.Words, we added DocumentDirection property in TxtLoadOptions class to detect the text direction (RTL/LTR) in the document.

Please use TxtLoadOptions as shown below to get the desired output.

TxtLoadOptions loadOptions = new TxtLoadOptions();
loadOptions.setLoadFormat(LoadFormat.TEXT);
loadOptions.setDocumentDirection(DocumentDirection.AUTO);
Document doc = new Document(MyDir + "hebrew.txt", loadOptions);
PageSetup ps = doc.getFirstSection().getPageSetup();
ps.setLeftMargin(0.36 * 72); // 0.5 inch
ps.setRightMargin(0.36 * 72);

doc.save(MyDir + "out.pdf", new PdfSaveOptions());