Avoid Styling Issue of Kerning Text using HarfBuzz Package during Word DOCX Document to PDF Conversion using C# .NET

I am facing styling issue while converting Word to PDF.

For simplicity I am attaching 2 very simple documents. Both documents are same other than one is having text style as “Normal” and other has text styling as “Title”

If you closely see both the .docx documents you can see the difference in spacing between first “T” and “e” letters.

The problem is when it get converted into PDF the spacing is not similar as in the word document. It looks like for both the spacing is applied with “Normal” styling.

This looks like a small issue but for me it’s creating lots of issues, as for big documents the page counts are getting different as some text is going to 2 lines which is originally in 1 line.

Please suggest if this can be handled with some code changes or if I am missing something.

Thanks
rizzz86

docx_files.zip (23.7 KB)

@rizzz86,

We have logged this problem in our issue tracking system. Your ticket number is WORDSNET-21375. We will further look into the details of this problem and will keep you updated on the status of the linked issue. We apologize for any inconvenience.

Thanks awais.hafeez for your reply.

Please give this issue priority as it’s critical on my side. Appreciate your cooperation.

@rizzz86,

The issue occurs because the problematic text uses kerning. Aspose.Words supports kerning via Aspose.Words.Shaping.HarfBuzz NuGet package. Please try the following code and refer to: Enable OpenType Features

Document doc = new Document("C:\\Temp\\docx_files\\TestDOCX - 2.docx");
doc.LayoutOptions.TextShaperFactory = Aspose.Words.Shaping.HarfBuzz.HarfBuzzTextShaperFactory.Instance;
doc.Save("C:\\temp\\docx_files\\20.11.TestDOCX - 2.pdf");

Thanks @awais.hafeez for your prompt response.

I tried to install Aspose.Words.Shaping.HarfBuzz NuGet package from Visual Studio “Manage Nuget Pckages” option. I selected version 20.10.0 so that it will be compatible with my Aspose.Words version. But after installation I am getting following error when I tried to build my project:

Invalid static method invocation syntax: “[MSBuild]::IsOSUnixLike()”. Method ‘[MSBuild]::IsOSUnixLike’ not found. Static method invocation should be of the form: $([FullTypeName]::Method()), e.g. $([System.IO.Path]::Combine(a, b)).

Any ideas?

@rizzz86,

Please first completely remove Aspose.Words for .NET and Aspose.Words.Shaping.HarfBuzz references from your visual studio project. After that install the ‘latest 20.11 versions’ of Aspose.Words for .NET and Aspose.Words.Shaping.HarfBuzz packages from NuGet. Hope this helps.

@awais.hafeez,

I was able to include Aspose.Words.Shaping.HarfBuzz in my project. When I follow the steps you have mentioned even after that it was giving the same error. But later I used VS 2019 to include this package and it worked. With VS 2015 it is still complaining. Anyway we can go ahead with VS 2019 as it’s working for us.

I have now added following line of code at the time of document generation:

doc.LayoutOptions.TextShaperFactory = Aspose.Words.Shaping.HarfBuzz.HarfBuzzTextShaperFactory.Instance;

But after adding this line its generating damaged PDF document with 1KB size only. If I remove this line of code then the document generation is fine.

To further test, I created a small windows form application to test this case and with winform app it worked fine. The generated document also seems to be fine and have proper spacing between the letters.

My actual application is a .Net Web application where it is still generating damaged PDF document when Aspose.Words.Shaping.HarfBuzz is applied.

The solution you have provided seems working with desktop app, but please confirm if there is anything I have to take care of in case of Web application.

Thanks

@rizzz86,

We are even unable to produce a PDF output via Web Application on our end. The web app throws following exception:

System.Exception
  HResult=0x80131500
  Message=Text shaper factory failed to return text shaper for 'C:\Windows\Fonts\arial.ttf', face index '0'
  Source=Aspose.Words
  StackTrace:
   at    .(    , ITextShaperFactory )
Inner Exception 1:
BadImageFormatException: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)

For the sake of any correction, we have logged this problem in our issue tracking system. The ID of this issue is WORDSNET-21399. We will further look into the details of this problem and will keep you updated on the status of linked issue. We apologize for your inconvenience.

Thanks @awais.hafeez

Hope you guys will come up with the fix soon.

@rizzz86,

WORDSNET-21399 is currently pending for analysis and is in the queue. We will inform you via this forum thread as soon as this issue will get resolved in future or any more updates may be available. We apologize for any inconvenience.

Hi @awais.hafeez, please confirm if there is any update on this.

Thanks

@rizzz86,

We have completed the analysis of these issues and concluded to close these issues with “not a bug” status. Please check the following analysis details:

WORDSNET-21375: please see my reply here.

WORDSNET-21399: NuGet package contains two native harfbuzz.dll compiled for x86 and x64 platforms and special MSBuild script.

Depending on project’s target platform, the MSBuild script chooses necessary native harfbuzz.dll (x86 or x64) and copies them to TargetDir of the project.

This MSBuild script detects project’s target platform using {PlatformTarget} and {Prefer32Bit} parameters of the project. It is mostly OK for all types of projects except ASP.NET WebForms.

Project’s target platform may depend on {Use64BitIISExpress} parameter of the project. In general, there is no good way to detect actual project’s target platform if {Use64BitIISExpress} set to “Default” and {PlatformTarget} set to “Any CPU”.

The workaround: Please try to set explicit value in {Use64BitIISExpress} = x86 or x64. And REBUILD the project (see Use64BitIISExpress.png (23.4 KB)).

The issues you have found earlier (filed as WORDSNET-21399) have been fixed in this Aspose.Words for .NET 23.2 update also available on NuGet.