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

Free Support Forum - aspose.com

Problem with (RTL-LTR) mixed report

Hi Alexey,

Thanks for your service & quick replies.

We are using Aspose.Word for .net version 9.0.0.0

We are facing one issue while printing mixed report containing Arabic(RTL) & English(LTR) text.

This issue was also discussed on Aspose forum & as per your suggestion we have to make changes into Font.Bidi property only for supporting mixed mode text for e.g.

Font.bidi = true { for Arabic text}

Font.bidi = false {for English text}

After using above suggestion we found that the text is getting printed properly but position of the text is getting changed. which result into an wrong output.

Please refer the Attached “sample.doc” for further reference.

In attached report the text “Hi Vinesh” is not getting displayed on proper location.The order of the text is getting changed.

Also refer the attached image “error.png” encircling the problem area in RED color.

The sample code is as follows.

Document doc = new Document();
DocumentBuilder documentBuilder = new DocumentBuilder(doc);
XmlDocument XmlDoc = new XmlDocument();
string path = Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath);
Aspose.Words.License lic = new Aspose.Words.License();
lic.SetLicense(path + @"\Aspose.Total.lic");
Aspose.Pdf.License lic1 = new Aspose.Pdf.License();
lic1.SetLicense(path + @"\Aspose.Total.lic");
documentBuilder.ParagraphFormat.ClearFormatting();
// Specify the locale so Microsoft Word recognizes this text as Arabic - Saudi Arabia.
// For the list of locale identifiers see https://docs.microsoft.com/en-us/openspecs/office_standards/ms-oe376/6c085406-a698-4e12-9d4d-c3b0ee3dbc4a
documentBuilder.Font.LocaleIdBi = 1025;
documentBuilder.ParagraphFormat.Bidi = true;
documentBuilder.Font.NameBi = "Arial unicode MS";
documentBuilder.Font.SizeBi = 10;
documentBuilder.ParagraphFormat.Alignment = ParagraphAlignment.Left;
documentBuilder.Font.Bidi = true;
documentBuilder.InsertHtml("مرحبا هناك ما هو اسمك وماذا تفعل في هذا");
documentBuilder.Font.Bidi = false;
documentBuilder.InsertHtml("Microsoft 1998");
documentBuilder.InsertHtml("Hi vinesh");
// Save output document.
// builder.Document.Save(@"Test001\out.doc");
doc.Save("OK.doc");
StartWord("OK.doc");

Will you please verify this issue against Aspose.Word version 9.0.0.0

Will you please let us know how to resolve this issue?

Waiting for your reply…

Thanks & Regards,

Dwarika

Hi Dwarika,

Thanks for your request. I tested your code using the latest version of Aspose.Words and the output document looks correct. So you can try using the latest version of Aspose.Words to resolve the issue.

Also, you can try specifying alignment of the paragraph instead of specifying bidi property. Please see the following code:

Document doc = new Document();
DocumentBuilder documentBuilder = new DocumentBuilder(doc);
documentBuilder.ParagraphFormat.ClearFormatting();
// Specify the locale so Microsoft Word recognizes this text as Arabic - Saudi Arabia.
// For the list of locale identifiers see https://docs.microsoft.com/en-us/openspecs/office_standards/ms-oe376/6c085406-a698-4e12-9d4d-c3b0ee3dbc4a
documentBuilder.Font.LocaleIdBi = 1025;
documentBuilder.ParagraphFormat.Alignment = ParagraphAlignment.Right;
documentBuilder.Font.NameBi = "Arial unicode MS";
documentBuilder.Font.SizeBi = 10;
documentBuilder.Font.Bidi = true;
documentBuilder.InsertHtml("مرحبا هناك ما هو اسمك وماذا تفعل في هذا");
documentBuilder.Font.Bidi = false;
documentBuilder.InsertHtml("Microsoft 1998");
documentBuilder.InsertHtml("Hi vinesh");
// Save output document.
doc.Save(@"Test001\OK.doc");

Hope this helps.

Best regards,

Hi Alexey,

Thanks for your quick guidance.

We have downloaded Aspose latest version as Aspose.Word version 9.5.0.0

We have tested the sample code against it one more time & we found following problem while printing Arabic report.

1> The text printing operation flows in left-right direction rather than to flowing from right to left direction.

2> the document language property is set to “English” rather than “Arabic” indicating that the MS word is not recognizing this text as “Arabic” text.

Please refer the sample document & image in PNG format, the image contain the problem area encircled by RED color.

If you observe the sample code & document, you can easily come to know that the flow of printing the document should start from right hand side & should go on increasing towards left hand side for e.g.

1> Arabic text { in most right hand place}

2>“Microsoft 1998” ( after Arabic text but in left hand side }

3>“Hi Vinesh” (after above text in left side side & should appear as last one}

But in current document flow is first Arabic text then 2nd English text towards right hand direction & “hi Vinesh” towards right hand direction.

The sample code is as follows.

private void button1_Click(object sender, EventArgs e)
{
    Document doc = new Document();
    DocumentBuilder documentBuilder = new DocumentBuilder(doc);
    XmlDocument XmlDoc = new XmlDocument();
    string path = Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath);
    Aspose.Words.License lic = new Aspose.Words.License();
    lic.SetLicense(path + @"\Aspose.Total.lic");
    Aspose.Pdf.License lic1 = new Aspose.Pdf.License();
    lic1.SetLicense(path + @"\Aspose.Total.lic");
    documentBuilder.ParagraphFormat.ClearFormatting();
    // Specify the locale so Microsoft Word recognizes this text as Arabic - Saudi Arabia.
    // For the list of locale identifiers see https://docs.microsoft.com/en-us/openspecs/office_standards/ms-oe376/6c085406-a698-4e12-9d4d-c3b0ee3dbc4a
    documentBuilder.Font.LocaleIdBi = 1025;
    documentBuilder.ParagraphFormat.Bidi = true;
    documentBuilder.Font.NameBi = "Arial unicode MS";
    documentBuilder.Font.SizeBi = 10;
    documentBuilder.ParagraphFormat.Alignment = ParagraphAlignment.Left;
    documentBuilder.Font.Bidi = true;
    documentBuilder.InsertHtml("مرحبا هناك ما هو اسمك وماذا تفعل في هذا");
    documentBuilder.Font.Bidi = false;
    documentBuilder.InsertHtml("Microsoft 1998");
    documentBuilder.InsertHtml("Hi vinesh");
    // Save output document.
    //builder.Document.Save(@"Test001\out.doc");
    doc.Save("OK.doc");
    StartWord("OK.doc");
}

Notre:- ParagraphAlignment.Left; will align the text towards right in RTL & vice versa.

1> Will you please retest this issue with 9.5.0.0.

2> How to resolve this problem.

Waiting for your reply…

Thanks & Regards,

Dwarika

Hi Dwarika,

Thank you for additional information. The problem occurs because in 9.5.0 version behavior of InsertHtml was changed. Now content inserted by Insert HTML does not inherit formatting specified in DocumentBuilder options. Whole formatting is taken from HTML snippet. Since your snippet contains only text and no formatting, default formatting is used. So as a simple solution use Write method to insert simple text instead of using InsertHtml.

Also could you please also attach the document that will show us how the expected output should look? This will really help us to better understand your requirements.

Best regards,

Hi Alexey,

Thanks for your quick reply & suggestion.

With reference to your above comment will you please solve our following queries.

1>In Aspose.word version 9.0.0.0 the InsertHTML method is used to print all HTML & NON-HTML text but now this will not work in Aspose.Word version 9.5.0.0 as you have change the behaviour of this API

2> InsertHTML will only print HTML text & will not print the normal text in proper format.

3>In Aspose word version 9.0.0.0 documentBuilder.InsertHTML() was supporting all text formatting coming from HTML text as well as the formatting associated with documentbuilder object.for e.g

1>documentbuilder.font.color = color.blue

2>documentbuilder.insertHTML(“Hello All”);

The final Outcome is a combination of blue color text along with bold & italic formatting on text.

This functionality will no more working with Aspose Word version 9.5.0.0

4> documentBuilder.Write used to print simple text in Aspose word version 9.5.0.0 { This functionality was not in picture in version 9.0.0.0}

5> for printing HTML text for RTL report {for e.g. Arabic}we have to explicitly use in case of Aspose word version 9.5.0.0 & This was not required in 9.0.0.0 as bidi property implementation of documentBuilder already take care of RTL settings.

6> for printing HTML text for LTR we don’t have to use span property as the default span property setting is always “ltr”

We are waiting for your reply…!!!

Thanks & Regards,

Dwarika

Hi Dwarika,

Thanks for your request.

  1. Yes you are right and this is correct behavior. In case of using HTML strings you should use Insert Html method. In case of using non-HTML strings, use Write or WriteLine methods.

  2. When you insert simple text using InsertHtml method, default formatting will be applied to this text.

  3. Yes, the behavior was changed. Now Aspose.Words takes all formatting from HTML. This is correct and expected behavior.

  4. Write and WriteLine methods was not changed. They works the same in 9.0.0 and 9.5.0 versions.

  5. Aspose.Words 9.0.0 version does not support dir attribute. That is why the only way to insert RTL text in this version is using Bidi property. 9.5.0 version already supports dir attribute. So you can insert RTL text using InsertHtml method or using Bidi properties and Write method.

  6. Yes, you are right, by default direction of text is left-to-right.

Best regards,

Hi Alexey,

Thanks for your response.

My concern is more towards 3rd query in my last post…

>In Aspose word version 9.0.0.0 documentBuilder.InsertHTML() was supporting all text formatting coming from HTML text as well as the formatting associated with documentbuilder object.for e.g

1>documentbuilder.font.color = color.blue

2>documentbuilder.insertHTML(“Hello All”);

The final Outcome is a combination of blue color text along with bold & italic formatting on text.

This functionality will no more working with Aspose Word version 9.5.0.0

how can the above functionality be achieved using Aspose 9.5.0?

Regards,

Dwarika

Hi Dwarika,

Thanks for your request. There is no way to activate this in 9.5.0. You should specify all formatting in HTML string, if you would like to use InsertHtml.

Best regards,

Hi,

As you have mentioned, this can be achieved with 9.5, by mentioning all formatting in HTML string.

But that would mean changing the entire logic of our appication.

Can you please suggest a workaround to achieve this functionality in 9.5, without any major impact?

Amit S

Hi Amit,

Thanks for your request. As I already mentioned earlier, there is not other way to achieve this. If you use InsertHtml method to insert content all formatting should be specified in HTML. If you use Write or WriteLine methods you must specify formatting via DocumentBuilder properties.

Best regards,

Hi Amit,

Thanks for your inquiry.

You may also want to look into using this code found in this post here. In your case you can pass an instance of DocumentBuilder.Font. This will allow you to change the formatting of the inserted HTML content to match the current font of the DocumentBuilder.

Thanks,

We are getting the following message when we clicked on the reference post link specified above.

Also, in your post you have mentioned specifically about DocumentBuilder.Font, we would like to know how will it work with other properties like underline,bold,italic,color etc

Access Denied

You do not have permissions to perform the requested action.

Hi Amit,

Thanks for this additional information.

My apologises for that, please find the code below.

DocumentBuilder builder = new DocumentBuilder(e.Document);
builder.MoveToMergeField(e.DocumentFieldName);
// Set up the node changing event handler. We will listen for any nodes inserted during the time that the HTML is being inserted. Change any of these run nodes to match the font of the field which the HTML is being inserted at.
e.Document.NodeChangingCallback = new HandleFontNodeChanging(e.Field.Start.Font);
builder.InsertHtml((string)e.FieldValue);
// Remove the font changer handler from the document.
e.Document.NodeChangingCallback = null;
public class HandleFontNodeChanging : INodeChangingCallback
{
    Aspose.Words.Font mFont;
    public HandleFontNodeChanging(Aspose.Words.Font font)
    {
        mFont = font;
    }

    void INodeChangingCallback.NodeInserted(NodeChangingArgs args)
    {
        if (args.Node.NodeType == NodeType.Run)
        {
            Run run = (Run)args.Node;
            run.Font.Name = mFont.Name;
            run.Font.Color = mFont.Color;
            run.Font.Size = mFont.Size;
        }
    }

    void INodeChangingCallback.NodeInserting(NodeChangingArgs args)
    {
        // Do Nothing
    }

    void INodeChangingCallback.NodeRemoved(NodeChangingArgs args)
    {
        // Do Nothing
    }

    void INodeChangingCallback.NodeRemoving(NodeChangingArgs args)
    {
        // Do Nothing
    }
}

Regarding your query, those properties are all related to font. You can ensure these properties are passed on by adding them to the logic in the node changing handler. You may also want to extend the functionality so that all properties are automatically syncronized (through reflection perhaps) and properties are only passed over if the property of HTML content is not default (i.e the formatting of that property has actually been set in the HTML).

If you have any queries please feel free to ask.

Thanks,

hi Adam Skelton,

Thanks for your quick suggestions & reply…!!

My question is not only related to font but all other properties related to paragraph, table & rest of others Aspose objects.

Is there any workaround so that we can easily map these properties + the HTML tags given by user will work together & give a combinational output.

Thanks & Regards,

Dwarika

Hi Dwarika,

Thanks for your inquiry.

I have made a sample project which will hopefully help with this issue. It uses reflection to copy the formatting of the DocumentBuilder format related objects onto the inserted content from HTML.

You can use the attached code by calling InsertHtmlWithBuilderFormatting along with the DocumentBuilder object and the HTML string.

If there are any issues please feel free to ask.

Thanks,

Hello Adam Skelton

As suggested by you, we used the “InsertHtmlWithBuilderFormatting.cs” implementation in our sample code to verify how it works with existing code base.

However we have certain issues with this kind of code changes,

  1. We have used DocumentBuilder.InsertHTML method extensively in our existing application and using “InsertHtmlWithBuilderFormatting.cs” will be workaround.
  2. Using this utility we need to change our current implementation which we would not want to do.
  3. Since this utility is not a part of Aspose standard releases, we are not sure how long this utility will work for us.
  4. The major problem we are facing here is we not able to produce report with mixed settings; i.e. properties set using DocumentBuilder.Font / Cell / Row and HTML formatting tags applied to the input string.

e.g. We are setting font-name, font-size and color using documentbuilder.Font object and we are providing “bold” as HTML tag to the input string as given below,

LineOne: This is to test TEXT with Aspose Words 9.5 dll

  1. It seems properties set using documentbuilder font class is showing up in the output. But the HTML tags are not showing any effect in the output document.
  2. Our requirement is to have both the properties should show up in the final rendered output as it was working in Aspose 9.0.0.0
  3. Also if you take a look at Name: Mr Test Person
    Job Profile: Lead Test Staff
    Company: Company India. This is also not rendering as expected. We would like to see HTMl formatting along with other properties which is not the case.
  4. Do you have idea how do we achieve this? Also with minimal code changes!

Attached is the sample code we are using to test this. If you look through there is a button “generate report” and on the click event of this button we are generating the document.

If you look though the btnGenerateDoc_Click code, you will fine one region called Text Object; to the end of this region we are rendering a text piece with HTML table.

When we generate the entire document the text piece of this string renders somewhere in the middle of the page while the table renders at the bottom. Attached is the errorimage.png for your reference.

SampleCodeandDocs.zip is attached with actual word output (TestAspose(9_5).doc) , error image (errorimage.PNG). and the sample code.

Regards,

Dwarika

Hi Dwarinka,

Thanks for your request. As I already mentioned earlier in this thread, there is no direct way to use formatting specified by DocumentBuilder when you use InsertHtml method. The code provided by Adam shows the technique how you can achieve this. As you understand, there is no way to achieve this without changing the code.

I see only two options, I already mentioned this earlier:

  1. Use InsertHtml method to insert content into the document and specify all formatting in HTML tags. Do not use DocumentBuilder settings at all.

  2. Use Write and WriteLine methods to insert content and use DocumentBuidler settings to specify formatting.

Best regards,

Hi Dwarinka,

Sorry for the delay.

Just to add on to what Alexey has said.

  1. Yes this code is a workaround, as you are trying to achieve a hybrid of functionality from previous versions and the current verisons.
  2. As Alexey said there no way around this without either including the formatting on the HTML string or using this implementation. I would suggest this method as you can keep all of your HTML content the same and instead replace InsertHtml with the work around method.
  3. This code should work for all future versions as long as there is no drastic change to the way InsertHtml or DocumentBuilder works which is not likely . In any case to avoid worry I would suggest creating a few simple unit tests of your application.
  4. Thanks for attaching your sample project here. I have attached the same class with a few changes, now the formatting from HTML is combined with the DocumentBuilder formatting. I knew the older code did not combine formatting but it was serving as an example at the time. Using the new code you’re document appears very close to as expected now.

If you have any other queries, please feel free to ask.

Thanks,

Hello Adam Skelton,

Thanks for addressing the queries and the latest sample code with changes.

We have used this new file by downloading it from the forum and made changes in the code as suggested by you.

Still it is not working for us as far as HTML formatting is concerned.

Take a look at changed code sample and output document attached with this post.

Take a look at first line LineOne: This is to test TEXT with Aspose Words 9.5 dll where we have provided HTML Bold tag () and second line LineTwo: This is to test TEXT with Aspose Words 9.5 dll where we have provided HTML Underline tag ()

LineOne: This is to test TEXT with Aspose Words 9.5 dll

LineTwo: This is to test TEXT with Aspose Words 9.5 dll

Let us know whether we have missed out something because of which it is not working.

Regards,

Dwarika

Hi Dwarinka,

Thanks for your inquiry.

You are still using the old version of the code in your project. The new version in my previous posts includes the CopyFormatting method accepting three parameters whereas the old version only accepts two. The third parameter is the default formatting accquired by a new DocumentBuilder object.

Thanks,