Use Aspose.PDF for .NET API with PowerShell

We try to use the Aspose PDF for .Net library by using PowerShell. Does Aspose has a PowerShell-compatible DLL? If yes with which PowerShell version could this work.

@daniele.divenosa

You can use Aspose.PDF in PowerShell scripts while using .NET 4.0 DLL in the API Package. We have successfully tested it using PowerShell version 5.1.19041.610 in Windows 10. A sample PowerShell Script is also attached for your kind reference.

CreatePdf.zip (471 Bytes)

Thank you for your feedback and your sample.
Unfortunately your PowerShell script doesn’t work by us. The only differences which we could found was, that you reference to Aspose.Total.lic but we have only Aspose.PDF.Net.lic.

Here some details.
Aspose_PowerShell_Error.png (57.9 KB)

Could this make the difference?
Best regards

@daniele.divenosa

The error is not related to the license file. Your program is unable to find the Aspose.PDF DLL. Please place the Aspose.PDF DLL folder in the folder from where you are loading/including it in the script. In case issue still persists, please feel free to let us know.

We execute the follow command…

Add-type -literalpath ($dllPath) -ErrorAction STOP

And the result is:

Add-type : Could not load file or assembly ‘file:///C:\scripts\PowershellModules\AsposePDF\Aspose.Pdf.dll’ or one of its
dependencies. Operation is not supported. (Exception from HRESULT: 0x80131515)
At C:\scripts\PowershellModules\AsposePDF\CreatePdf.ps1:13 char:1
Add-type -literalpath ($dllPath) -ErrorAction STOP

 CategoryInfo          : NotSpecified: (:) [Add-Type], FileLoadException
 FullyQualifiedErrorId : 
System.IO.FileLoadException,Microsoft.PowerShell.Commands.AddTypeCommand

The DLL referenz exists (check the picture):
AsposePDF_File_location.png (61.5 KB)

Are there some pre-requisites missing or is this a permission issues some where?

@daniele.divenosa

It seems like permissions related issues as we tested the same script with Aspose.PDF for .NET 21.3 and did not notice any issue. For your kind reference, an output PDF is also attached. Please try giving all permissions for the folder where script is located OR try launching the PowerShell with Admin Rights. Also, please try to put the complete folder in a Drive other than C and check again.
output.pdf (2.0 KB)

Thank you for your feedback the problem is solved now.
The last version of Aspose.PDF .Net 21.3 resolved the problem. We previously used the previous version .Net 21.2.

@daniele.divenosa

It is nice to hear that your issue has been resolved. Please keep using our API and feel free to create a new topic in case you need further assistance.

Hello,

I need to use aspose in powershell. I can’t download the example you posted. Can you send it back to me? Thank you

@MiloudB

Below is the content of the shared file:

# Load the Aspose.Pdf.dll
Add-type -literalpath ($PSScriptRoot + "\Aspose.Pdf.dll") -ErrorAction STOP
Add-Type -AssemblyName System.IO
Add-Type -AssemblyName System.Drawing

$license = new-object Aspose.Pdf.License
$license.SetLicense($PSScriptRoot + "\Aspose.Total.NET.lic")
# Instantiate Document Object
$doc = new-object Aspose.Pdf.Document
# Add a page to pages collection of document
$page = $doc.Pages.Add()
# TextFragment
$txtFrag = new-object Aspose.Pdf.Text.TextFragment("This is test. Generated by PowerShell Script")
# Add the image into paragraphs collection of the section
$page.Paragraphs.Add($txtFrag)

$doc.Save($PSScriptRoot + "\output.pdf")
1 Like