In order to minimize the number of spire references throughout the application, I refactored to consolidate the Spire pdf functionality into a single project. In that project we have embedded the license.elic.xml file as an embedded resource. Here is the class it uses to obtain the license and create the pdf documents.
- Code: Select all
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Xml.Linq;
using Spire.License;
using Spire.Pdf;
namespace Utilities.Exports.Pdf.SpirePdf
{
/// <summary>
/// Used to obtain a licensed Spire PDF <see cref="PdfDocument"/>
/// </summary>
public static class PdfDocumentFactory
{
static PdfDocumentFactory()
{
string resourceName = "PenLink.Utilities.Exports.Pdf.SpirePdf.license.elic.xml";
string licenseKey;
// There is a stream based license loader for Spire PDF, but it doesn't read the stream immediately, but
// later on. Meaning you need to leave the stream open-ended. Instead, read the
// license key from the file and use that variant of license loading.
Assembly assembly = Assembly.GetExecutingAssembly();
using (Stream resourceStream = assembly.GetManifestResourceStream(resourceName))
{
XDocument licenseDocument = XDocument.Load(resourceStream);
licenseKey = licenseDocument.Root.Attribute("Key")
?.Value;
Debug.Assert(licenseKey is not null, "Unable to find the license key inside the license file.");
}
LicenseProvider.SetLicenseKey(licenseKey);
}
public static PdfDocument Create()
=> new PdfDocument();
public static void EnsureInitialized()
{
// Do nothing. The act of calling this method ensures the static constructor is executed.
}
}
}
I utilize this code in another static class in the same project to generate pdf from xps. Here's that code
- Code: Select all
using System;
using System.Threading;
using Utilities.Exports.Pdf.SpirePdf;
using Spire.Pdf;
namespace Utilities.Exports.Pdf
{
/// <summary>
/// General utility for PDF documents.
/// </summary>
public static class PdfUtility
{
public static void GeneratePdfFromXps(
string xpsPath
, string pdfPath
, CancellationToken cancellation
, Action<CancellationToken> midProgressAction = null
)
{
cancellation.ThrowIfCancellationRequested();
using (PdfDocument doc = PdfDocumentFactory.Create())
{
cancellation.ThrowIfCancellationRequested();
doc.LoadFromXPS(xpsPath);
midProgressAction?.Invoke(cancellation);
cancellation.ThrowIfCancellationRequested();
doc.SaveToFile(pdfPath, FileFormat.PDF);
}
cancellation.ThrowIfCancellationRequested();
}
public static void MergePdfs(string[] inputFiles, string outputFile)
{
// Since this doesn't invoke the PdfDocumentFactory to create the document, we still need to ensure licensing.
PdfDocumentFactory.EnsureInitialized();
PdfDocument.MergeFiles(inputFiles, outputFile);
}
}
}
The issue I've been noticing is with the first method GeneratePdfFromXps(), but I would imagine the issue would also exist for the second method.
I have confirmed through debugging that the issue is not with loading the in the key.
Seems that our license is develop OEM subscription, so as I understood we should be able get license for multiple processes.
How can I apply license properly and get rid of the evaluation warning?