Hello,
I spent a lot of time trying to use the Aspose.PDF library on a .net environment which runs unit tests on Ubuntu VM. We followed the instructions mentioned here (How to Install Aspose.PDF for .NET|Aspose.PDF for .NET) about working with .NET Core DLLs in Non-Windows Environment.
Specifically I installed the
- libgdiplus package
- package with Microsoft compatible fonts: ttf-mscorefonts-installer.
The error I am getting when the tests are trying to run the tests is:
System.PlatformNotSupportedException : COM Interop is not supported on this platform.
Stack Trace:
at System.Threading.Thread.SetApartmentStateUnchecked(ApartmentState state, Boolean throwOnError)
at System.Threading.Thread.SetApartmentState(ApartmentState state, Boolean throwOnError)
at System.Threading.Thread.SetApartmentState(ApartmentState state)
Can you please help me to resolve this issue?
Thank you in advance.
Kind regards,
Angelina Sokratous
@asokratous
Did I understand correctly that you have tests in your C# development environment that you want to run as if from Ubuntu using a virtual machine. Does the project use the Aspose.Pdf library, which throws an exception with this approach?
What VM are you using, why not Docker?
Hello @sergei.shibanov,
Yes, we have tests in our .net environment that we are running from the VMs which are azure DevOps agents. The project that throws the exception uses to Aspose.Pdf library.
Any idea?
For now, I can only advise trying Aspose.Pdf.Drawing instead of Aspose.Pdf
. There is another nuance about fonts, but try this first and write about the result.
Hello @sergei.shibanov ,
I am using Aspose.Pdf to remove any javascript that is embedded in a pdf file. The necessary methods reside in Aspose.Pdf, therefore it will not help me to use the Drawing library.
@asokratous
I draw your attention - I wrote about Aspose.Pdf.Drawing.
This library uses Aspose.Drawing instead of System.Drawing.Common. Its main limitation is that api printing is not supported at the moment. But this better work in non Windows platforms.
Thank you @sergei.shibanov. I will give it a try and get back at you.
Hi @sergei.shibanov,
I have tried your suggestion but it didn’t work… I am receiving the same error “COM Interop is not supported on this platform”…
I have also tried other libraries (itextSharp, pdfSharp) but I am getting the same error.
@asokratous
the library is strongly tied to the fonts available in MS Windows. And when working in other OS, it often helps to install them.
So you shoud install package with Microsoft compatible fonts: ttf-mscorefonts-installer
Another nuance when working with the library in Linux:
In which folder do you have the fonts installed?
The package looks for fonts in folders:
“/usr/share/fonts”
“/usr/share/fonts/truetype/msttcorefonts”
“/usr/share/fonts/msttcore”
“/usr/local/share/fonts”
“~/.fonts”
and does not take into account what is located in the cache. Let’s say that the font is shown by the command “fc-list | grep “.ttf” | cut -f2 -d: | sort | uniq" does not mean that it will be used by the library.
@sergei.shibanov
I have already installed the package ttf-mscorefonts-installer. Our fonts are located under:
/usr/share/fonts/truetype/msttcorefonts
@asokratous
I think I should try to reproduce this issue.
Do I understand correctly that you use the Aspose.Pdf.Drawing library in the tests for your software in a virtual machine (what?) for Ubuntu (version number?) and you throw an exception System.PlatformNotSupportedException : COM Interop is not supported on this platform ?
Please highlight the code snippet used so I can try to reproduce the problem.
@sergei.shibanov,
Yes we are running the tests on Ubuntu VMs.
The code snippet is the following:
private void RemoveJavaScript(string filePath)
{
var doc = new Document(filePath);
var keys = (IList)doc.JavaScript.Keys;
// remove JS actions that are added on page level
foreach (var p in doc.Pages)
{
p.Actions.RemoveActions();
}
// remove JS that is added on document level
foreach (string key in keys)
{
doc.JavaScript.Remove(key);
}
doc.Save(filePath);
}
@asokratous
Does this happen with any document? It would be nice if you attached a document with which this is manifested, so that when I reproduce it, there would be no doubt about the document.
@sergei.shibanov,
yes it happens with every pdf file. Attached find a sample one:
output - Copy.pdf (165.2 KB)
@asokratous
Thanks for the input - I’ll try to reproduce the issue in VirtualBox with Ubuntu. This will take time, please be patient.
@sergei.shibanov
Please find below the code for the unit test also:
[Fact]
public void ScanAndClean_JsActionsOnPageLevelAreEmbeded_JsActionsRemoved()
{
var service = new AntiVirusService();
string dir = Directory.GetCurrentDirectory();
string fileName = "output.pdf";
string filePath = Path.Combine(dir, fileName);
Document doc = new Document();
// Add page
doc.Pages.Add();
JavascriptAction javaScript = new JavascriptAction("this.print({bUI:true,bSilent:false,bShrinkToFit:true});");
// Assign JavascriptAction object to desired action of document
doc.OpenAction = javaScript;
// Adding JavaScript at page level
doc.Pages[1].Actions.OnOpen = new JavascriptAction("app.alert('page 1 opened')");
doc.Pages[1].Actions.OnClose = new JavascriptAction("app.alert('page 1 closed')");
var jsActionsBefore = doc.Pages[1].Actions.OnOpen;
service.ScanAndClean(filePath);
doc = new Document(filePath);
Assert.NotNull(jsActionsBefore);
Assert.Null(doc.Pages[1].Actions.OnOpen);
}
}
The method ScanAndClean(filePath) calls the RemoveJavaScript method I pasted above.
Thank you
@asokratous
Thanks for the provided code.
Unit test ScanAndClean_JsActionsOnPageLevelAreEmbeded_JsActionsRemoved
run on Ubuntu? From what environment and what test framework is used?
@sergei.shibanov,
yes the test run on ubuntu. We are using xunit framework.
@asokratous
I am preparing to reproduce the problem. Are you using the .NET 6 SDK included in the Ubuntu package manager or the .NET 7 SDK from the MS repository?