This production application has a strange problem, only on some virtual machines.
To reproduce the problem I created a small console application and the whole project (excluding the spire license) is attached to this post.
The application will unlock the spire license, then generates a document without the evaluation warning.
After "sleeping" for more then 1 second, a second document is create which will contain the evaluation warning.
If the second document is created after 500 ms, the second document will not contain the evaluation warning.
I tested the application on several Azure virtual machines and this problem only occurs on some of the virtual machines.
As far as I know these machines are all configured the same.
I also tried to unlock the license every time a document is created, but that did not fix the problem.
I know it has to do something with the specific machines, because the same executables are used, but have different results on different machines, but I can't seem to find the setting(s) that are different between de machines that might cause this problem to occur.
Some additional system information
We are using Spire.PDF v10.7.21 on a .NET 8 console application
Microsoft Windows Server 2019 Standard Version 10.0.17763 Build 17763 (Azure virtual machine)
Country settings: Netherlands
Regional format: Dutch (Netherlands)
Windows display language: English (United States)
Can you please help us with this problem?
- Code: Select all
// This application is created to analyze a problem with the spire licensing.
using Spire.Pdf;
using System.Xml.Linq;
static void CreatePdfFile(int sleepCount)
{
Console.WriteLine($"Sleep for {sleepCount}ms.");
Task.Delay(sleepCount).Wait();
byte[] fileBytes = File.ReadAllBytes("Test.pdf");
byte[] fileBytesWithoutMetaData = RemoveMetadataFromPdf(fileBytes);
string resultFileName = $"Test-Without-Meta-Data-{sleepCount}.pdf";
if (File.Exists(resultFileName))
{
File.Delete(resultFileName);
}
File.WriteAllBytes(resultFileName, fileBytesWithoutMetaData);
}
static byte[] RemoveMetadataFromPdf(byte[] pdfdocument)
{
// NOTE: Document information properties are set to an empty string and NOT null, to prevent exceptions.
using (PdfDocument document = new PdfDocument(pdfdocument))
{
document.DocumentInformation.Author = string.Empty;
document.DocumentInformation.Creator = string.Empty;
document.DocumentInformation.Keywords = string.Empty;
document.DocumentInformation.Producer = string.Empty;
document.DocumentInformation.Subject = string.Empty;
document.DocumentInformation.Title = string.Empty;
Dictionary<string, string> customerDefined = document.DocumentInformation.GetAllCustomProperties();
foreach (string key in customerDefined.Keys)
{
document.DocumentInformation.RemoveCustomProperty(key);
}
using (MemoryStream stream = new MemoryStream())
{
document.SaveToStream(stream);
return stream.ToArray();
}
}
}
static void UnlockLicenses()
{
// Unlock Spire.
XDocument xDocument = XDocument.Load(new MemoryStream(SpirePoc.Properties.Resources.Spire_pdf_license));
string? key = xDocument.Element("License")?.Attribute("Key")?.Value.ToString();
if (string.IsNullOrWhiteSpace(key))
{
throw new Exception("Spire pdf license not found.");
}
Spire.Pdf.License.LicenseProvider.SetLicenseKey(key);
Spire.Pdf.License.LicenseProvider.LoadLicense();
}
Console.WriteLine("Unlock spire license.");
UnlockLicenses();
CreatePdfFile(0);
CreatePdfFile(1000);
Console.WriteLine("Press ENTER to close the application.");
Console.ReadLine();