Retain Font Formatting during Converting Saving As Word Document DOC to PDF using C# ASP.NET Windows Server 2008

Hello, I’m getting crasy with Aspose.Words 6.0.1 for .NET. I have read almost all topic I could find on this forum but unfortunatly It was not of any help :frowning:

Here is my problem: I am using Aspose.words on windows server 2008, ASP.NET 3.5. to create a PDF file from a .doc or .docx file to a predefined directory. That directory has all privileges. On my local computer, everything is working fine, but not on the final hosting environment ( also windows server 2008, ASP.NET 3.5, full trust configuration)

Here is the c# code:

License license = new Aspose.Words.License();
license.SetLicense("Aspose.Words.lic");

Document doc = new Document(inPath + inFile + FileExt);
doc.SaveOptions.ExportImagesFolder = inPath;
doc.Save(outPath + outFile, SaveFormat.Pdf); 

Result :
it does not work. I Get the infamous “System.Security.SecurityException: Request for the permission of type ‘System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089’ failed.”

A file is created into the directory, but it is just an empty one: Lenght = 0.

This error occurs when calling the "doc.Save(…) method.

BUT ! … When I change the SaveFormat parameter to SaveFormat.Rtf, it works fine. I mean a RTF file is successfuly writen to the directory and no errors at all are fired.

Strange, is n’t it ?

Hope somebody could help me with some advise because I really do not know what to do. Thanks a lot for your assistance.

Jose M. H.

Hi

Thanks for your request.

  1. You don’t need to specify ExportImagesFolder when you use SaveFormat.Pdf or SaveToPdf method. This option is needed when you use Aspose.Words+Aspose.Pdf to convert Word documents to PDF (SaveFormat.AsposePdf is used.). In your case you can use code like the following:
Document doc = new Document("in.doc");
doc.Save("out.pdf", SaveFormat.Pdf);
  1. Have you tried to save the document into the MemoryStream? Does this work on server environment?

Also, you can try saving your document in the root folder of your application (just for testing)

doc.Save(Server.MapPath("out.pdf"), SaveFormat.Pdf);

Best regards.

Thanks a lot for your answer Alexey.

  1. Ok, I understand. I have changed the code.

  2. I did not tryed to save the document to the Memory Stream. I am going to try.
    Saving to the Server.MapPath will attempt to write in an unauthorized folder. So let me set the rights and have a try also.

I will report the result in a few minuts

Best regards.
Jose M. H.

Hi Alexey,

After giving the rights to the application folder, I tryed as recommended:

doc.Save(Server.MapPath("out.pdf"), SaveFormat.Pdf);

but I have the same error.

I tryed also saving to a memory stream, but with also the same result.

System.IO.MemoryStream ms = new System.IO.MemoryStream();
doc.Save(ms, SaveFormat.Pdf);

However, both methods work when changing SaveFormat to RTF.

Best regards
Jose M. H.

Thank you for additional information. Could you please provide me full stack trace of the error?

Best regards.

Thanks again for your answer Alexey,

Here is the stack trace when saving to memory stream:

[SecurityException: Request for the permission of type 'System.Security.Permissions.UIPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.]
System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlMark& stackMark, Boolean isPermSet) +0
System.Security.CodeAccessPermission.Demand() +58
System.Windows.Forms.NativeWindow.CreateHandle(CreateParams cp) +61
System.Windows.Forms.TimerNativeWindow.EnsureHandle() +130
System.Windows.Forms.TimerNativeWindow.StartTimer(Int32 interval) +32
System.Windows.Forms.Timer.set_Enabled(Boolean value) +179
System.Windows.Forms.Timer.Start() +12
Ჺ.ἑ.ἠ(ἦ ἡ) +56
Ჺ.ἑ.Ἐ(Ἢ Ἑ, Ἢ Ἒ, Int32 Ἓ, Object Ἔ) +66
Ჺ. .ᵧ(Object ‏) +140
Ჺ.ᵂ.ᵗ(ᜌ ᵔ) +487
Ჺ.ᵂ.ᵒ(ᜌ ᵓ, ᜌ ᵔ) +324
Ჺ.ᵂ.ᵒ(ᜌ ᵓ, ᜌ ᵔ) +570
Ჺ.Ά.Ᾱ() +2182
Ჺ.ᾱ.Ᾱ() +136
ប.Ӣ.Ც(Document ඔ, ӣ ᩥ) +210
Aspose.Words.Document.UpdatePageLayout() +13
Aspose.Words.Document.get_Ӣ() +16
Aspose.Words.Document.Ԕ(Stream Ԏ, String Ԍ, SaveFormat ԕ) +24
Aspose.Words.Document.Save(Stream stream, SaveFormat saveFormat) +18
All_Users_Logged_UserAdmin_Upload.ConvertToPdf(String inPath, String inFile, String FileExt) in f:\users\globeinfos.fr\httpdocs\All_Users\Logged\UserAdmin\Upload.aspx.cs:95
All_Users_Logged_UserAdmin_Upload.BtnUploadNow_Click(Object sender, EventArgs e) in f:\users\globeinfos.fr\httpdocs\All_Users\Logged\UserAdmin\Upload.aspx.cs:60
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +6785
System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +242
System.Web.UI.Page.ProcessRequest() +80
System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context) +21
System.Web.UI.Page.ProcessRequest(HttpContext context) +49
ASP.all_users_logged_useradmin_upload_aspx.ProcessRequest(HttpContext context) in c:\windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\9ddbce15\b59c97c4\App_Web_ledfbldn.2.cs:0
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +181
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +75

Hope it could help.

Best regards
José M. H.

Unfortunately, I cannot tell you what the reason of this problem is. Maybe there is some issue with security polices. Please check assembly policy as described here:
https://docs.microsoft.com/en-us/previous-versions/msp-n-p/ff921345(v=pandp.10)

I tried to play with trust levels on my side but can’t reproduce the problem. Could you also attach your document?

Best regards.

Thank you Alexey.

Please find attached the “.doc” document I am using for the tests.

I tryed also with a simple “.docx” file containing only one line : “qsdqsdqsd”
Same result.

I continu some more tests on my side.

Best regards
Jose M. H.

Hi

Thank you for additional information. Unfortunately, I cannot reproduce the problem on my side. I tested this on WinXP and on Win2003 and PDF document is generated without any issues. I hope you have some progress. Please let me know if you have any additional information.

Best regards.

Hi Alexey,

as I cannot access any useful advanced tool on the server side, I made some investigation on my local computer.
it seems there is a main difference between saving with SaveFormat.Pdf and SaveFormat.Rtf option:

  • Saving with option SaveFormat.Rtf:

Aspose.word assembly does not scan disks for font files.

  • Saving with option SaveFormat.Pdf:

Aspose.word assembly scan disks for all registered font files and read them all, even if the file to be converted does not include any fonts.

So I suppose this can be the reason why I get that “ugly” permission error. The web hosting service I am currently using may not allow such scanning process on folder like /windows/fonts or others. But I cannot confirm it, as the administration consol provided by the web hosting service is rather “poor”…

If anyone as an opinion, it will be much appreciated :wink:

Jose M. H.

Hi

Thank you for additional information. Yes, you are right, Aspose.Word needs fonts to convert document to PDF. It reads installed fonts from registry. You can ask your hosting to allow you to read fonts.

I also created new issue #7283 in our defect database. We will further investigate the problem.

Best regards.

Hi Alexey.

Unfortunatly, I fear that asking the web hosting service to do so will result in something like “shouting in the ear of a deaf” :wink:
They are not cooperative at all… But anyway, I will try.

It’s a pity, because Aspose.word is really a great tool : powerful, very easy to use, very easy to install, no need of registration process at server side, an so on… A nice piece of work.

Thanks again for your assistance. I really appreciated your reactivity.

Jose M. Hernandez
FRANCE

Hi,

As expected, the web hosting refuse to change anything related with files permissions. In addition of that, I have been informed that I dont have acces to the registry, and that I will never have.

All this because I am using a “mutualized hosting”…

I think that this kind of limitation can be common to most of mutualized hosting. So the Save() method to PDF format cannot work at all in such environment.

As you already created an issue (7283), I think that the alternative to this problem could be to implement a new option to force Aspose.word to get fonts from a given folder. Of course, that folder may be quite large (350Mo, may be more) but space is less a problem than permissions on hosting. At least, we could use aspose.word to convert documents to PDF format :wink:

Best regards
Jose M. Hernandez

Yes, that’s what we thought too. We were thinking exactly about adding an option to specify your own directory where to read true type fonts from. Will do asap.

Hi Roman,

Thank you very much for your answer.

I will wait for the new release

Best regards
Jose M. Hernandez

Hi,

I can see that there is a new release of ASPOSE.WORD ( 6.1.0) but I am not sure that it can solve this thread issue (#7283). May I have some hope ? :wink:

Best regards
Jose M. Hernandez

Hi

Thanks for your request. Unfortunately, this issue is unresolved yet. Sorry for inconvenience.

Best regards.

Hi,

ASPOSE.WORD release is now 6.3.0, but it seems that issue 7283 is not solved yet :frowning:

Do you think this will be taken in account in a futur release ?

Best regards
Jose H

Hi

Thanks for your request. Unfortunately, the issue is still unresolved.

Best regards.

Hello.

You can use FontSettings class to specify the fonts folder explicitly.
Since the initial issue is not a bug of AW it doesn’t make sense now.