I need to implement the function to support Bidi (bi-directional language) layout, so need to call Aspose for Java API to deal with my input string. But I am not sure that my calling way of Aspose Java Bidi API is correct or not. Please help confirm this**.**
My testing code ( I have attached the testing .txt file ):
import java.io.BufferedReader;
import java.io.InputStreamReader;
import com.aspose.words.DocumentBuilder;
import org.junit.Test;
public class TestAsposeBidiOutput
{
public static int getLCID( String language)
{
if ( language.equalsIgnoreCase( "ar")) //$NON-NLS-1$
{
return 1025;
}
if ( language.equalsIgnoreCase( "iw")) //$NON-NLS-1$
{
return 1037;
}
if ( language.equalsIgnoreCase( "he")) //$NON-NLS-1$
{
return 1037;
}
if ( language.equalsIgnoreCase( "fa")) //$NON-NLS-1$
{
return 1065;
}
if ( language.equalsIgnoreCase( "ur")) //$NON-NLS-1$
{
return 1056;
}
return 0;
}
private void newParagraphWithContent( DocumentBuilder docBuilder, String content, boolean bidi)
{
docBuilder.getParagraphFormat().clearFormatting();
docBuilder.clearRunAttrs();
docBuilder.getFont().setBidi( bidi);
if ( bidi)
{
docBuilder.getFont().setLocaleIdBi( getLCID( "iw")); //$NON-NLS-1$
}
docBuilder.getParagraphFormat().setBidi( bidi);
docBuilder.write( content);
docBuilder.writeln();
}
@SuppressWarnings( "nls")
@Test
public void testBidi() throws Exception
{
DocumentBuilder docBuilder = new DocumentBuilder();
String text = "1 - The quick fox 123 jumps over the lazy dog.";
for ( int i = 0; i < 100; ++i)
{
newParagraphWithContent( docBuilder, text, i%2 == 0);
}
newParagraphWithContent( docBuilder, "\n=============From File=====================\n", false);
BufferedReader reader = null;
boolean isBidi = false;
try
{
reader = new BufferedReader( new InputStreamReader( this.getClass().getResourceAsStream( "TestBidi.txt")));
String tempString = null;
while ( ( tempString = reader.readLine()) != null)
{
newParagraphWithContent( docBuilder, tempString, isBidi);
isBidi = !isBidi;
}
}
catch ( Exception e)
{
System.out.println( e.getMessage());
}
finally
{
if ( reader != null)
{
reader.close();
}
}
docBuilder.getDocument().save( "C:\test.doc");
docBuilder.getDocument().save( "C:\test.pdf");
}
}
Thanks for your query. Please see the following code snippet for your kind reference, the use of BIDI. I have also attached the output document with with post. Let us know, If you have any more queries.
String text = "1 - The quick fox 123 jumps over the lazy dog.";
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.getParagraphFormat().setBidi(true);
builder.writeln(text);
builder.getParagraphFormat().setBidi(false);
builder.writeln(text);
builder.getParagraphFormat().setBidi(true);
builder.writeln(text);
builder.getParagraphFormat().setBidi(false);
builder.writeln(text);
doc.save("D:\\AsposeOut.doc");
Thanks very much for you reply.
I have tested your code, and can’t implement my requirement.
I need to render Bidi content correctly, include two requirements: 1, render right to left, 2, render contents in correct sequence, just as in Hebrew environment.
You can check the correct output in http://www.google.co.il/, in Google, the output sequence is what I want.
I have tested the scenario and have managed to reproduce the same problem at my end. We have already logged this issue in our issue tracking system and you will be notified via this forum thread once this issue is resolved.
Thanks for your reply.
Can you help confirm that my way (Refer to my previous posted codes) calls Aspose API to layout Bidi content correctly or not? This is very important to me, I need to know that it’s my Aspose Lib usage issue or Aspose defect.
Please use the following code snippet for your scenario. Hope this helps you. I have attached the output document with this post.
String text = "1- The quick fox 123 jumps over the lazy dog.";
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.getParagraphFormat().setBidi(true);
builder.writeln(text);
builder.writeln(text);
Node[] runs = doc.getChildNodes(NodeType.RUN, true).toArray();
for (int j = 0; j < runs.length; j++)
{
Run run = (Run)runs[j];
run.getFont().setBidi(true);
}
doc.save("D:\\AsposeOut.doc");
Hi Tahir,
I have tested your code, and have one issue in your code:
1, I want to choose Bidi RTL or LTR layout randomly, but your codes always render RTL, so I think your codes has a little problem. Please advice.
And my codes can implement this, please look the snippet below, but I need your confirmation about my usage of Aspose Bidi API, I don’t know my calling way is correct or not:
I have tested your added codes, but what’s the difference about your codes and mine, I think I have handled the same situation with yours:
Your added codes:
Paragraph para = docBuilder.getCurrentParagraph();
Node[] runs = para.getChildNodes(NodeType.RUN, true).toArray();
for (int j = 0 ; j < runs.length; j++)
{
Run run = (Run)runs[j];
run.getFont().setBidi(bidi);
}
My original in function newParagraphWithContent: docBuilder.getFont().setBidi( bidi);
And I found there are two issues when I use your modified codes:
1, outputs in MS Word 2010 and 2007 are different, output in 2007 is wrong totally. The content with red border in MS word 2010 is rendered correctly.
2, When generated long contents which may contain many lines, all lines are rendered by same way, several lines are rendered wrongly ( the output file is opened by MS Word 2010 ).
There are two issues when output Bidi contents, you can refer to the previous chat history for more detail: 1, outputs in MS Word 2010 and 2007 are different, output in 2007 is wrong totally. The content with red border in MS word 2010 is rendered correctly.
2, When generated long contents which may contain many lines, all lines are rendered by same way, several lines are rendered wrongly ( the output file is opened by MS Word 2010 ).
Do you log the two issues in your bug tracking system?
Could you please explain the steps that I should follow to be able to display/format the text “1 - The quick fox 123 jumps over the lazy dog.” as “.The quick fox 123 jumps over the lazy dog - 1” in MS Word on my side?
Thanks for your reply, I don’t know how to set this in MS Word, because I don’t have Hebrew MS Word environment on my side.
I used Aspose to generate .doc file, and then open .doc file with MS Word 2010, we can find from the .doc file, most of the contents can implement RTL output “.The quick fox 123 jumps over the lazy dog - 1”, but if opened the .doc file with MS Word 2007, the output contents are wrong.
And Why are said the sequence “.The quick fox 123 jumps over the lazy dog - 1” is right, I compared the output with Notepad ( choolse Right to Left render ) and Google ( choose the Hebrew site ). You can check this by the previous history.
Thanks for the additional information. I have passed this info to our development team. As soon as I get any response from them, I will be more than happy to share that with you. Thank you for your patience and sorry for the inconvenience.
In addition, could you please run the following code on your side and view the generated document with Open Office.org Writer and let us know your findings then:
DocumentBuilder builder = new DocumentBuilder();
builder.getFont().setBidi(true);
builder.getParagraphFormat().setBidi(true);
builder.getFont().setLocaleIdBi(1037);
builder.writeln("1 - The quick fox 123 jumps over the lazy dog.");
builder.getDocument().save("c:\temp\outJava.docx");
The output produced by Aspose.Words is just what you’re looking for. Please see the following screen shot:
Moreover, I think, you should also contact Microsoft support, maybe they will be able to help you to resolve this problem.
It is to update you that I have received response from our development team on your issue.
First, please note that this is indeed not a bug in Aspose.Words. In your case, Aspose.Words does the right thing during formatting English text in Hebrew language environment. It may be that MS WORD 2007 and MS WORD 2010 (and possibly some other programs) use different implementations for formatting English text as RTL (Right to Left) and because of that you observe the difference in outputs when viewed with MS WORD 2007 and MS WORD 2010. Also, by OOXML standards, it is invalid to specify Font.setBidi to true for English text. Similarly, if you set Font.setBidi to false for Hebrew text, it will again be considered as invalid.
Secondly, if you still want to format the English text “1 - The quick fox 123 jumps over the lazy dog.” as “.The quick fox 123 jumps over the lazy dog - 1” in RTL fashion, you need to manually specify the correct Font.setBidi setting for LTR and RTL runs as follows:
DocumentBuilder builder = new DocumentBuilder();
builder.getParagraphFormat().setBidi(true);
builder.getFont().setBidi(true);
builder.getFont().setLocaleIdBi(1037);
// RTL run of text
builder.write("1 - ");
builder.getFont().setBidi(false);
// LTR run of text
builder.write("The quick fox 123 jumps over the lazy dog");
// setBidi to true again for rest of your Hebrew text
builder.getFont().setBidi(true);
builder.writeln(".");
builder.getDocument().save("c:\test\out.docx");
I hope, this will help.
Moreover, we’ve marked this issue as fixed and as a resolution we will consider adding more details on Font.setBidi property in the API References. You’ll get these API updates with the next version of Aspose.Words v11.3.0 which is planned to be released by April’ 30, 2012. We well inform you via this forum thread as soon as the new release is published.
If we can help you with anything else, please feel free to ask.
what about mixed content, is Aspose able to render a text with mixed LTR and RTL correctly or the mixed text needs to be manually split and the runs sent individually to Aspose?
An addition to my note above: limited tests using mixed text seem to indicate that Aspose can indeed handle it but I want to make sure that it is on purpose and a supported behavior not just happenstance.
Thanks for your inquiry. Sure, Aspose.Words writes the LTR and RTL text correctly.
Awais:
It may be that MS WORD 2007 and MS WORD 2010 (and possibly some other programs) use different implementations for formatting English text as RTL (Right to Left) and because of that you observe the difference in outputs when viewed with MS WORD 2007 and MS WORD 2010
By MS WORD 2007’s perspective and OOXML standards, it would be great if you manually split the mixed content into separate Runs of text. Otherwise, in your case Open Office Writer 3.3 and MS WORD 2010 displays the output generated by Aspose.Words correctly. Could you please try viewing the generated output with MS WORD 2010/Open Office and let us know your findings then?