I have a need to convert PDF files to a collection of images, one per page. I successfully used Aspose.PDF on Windows to convert to images - everything worked fine. However, our environment requires that I use Aspose.PDF on Linux, so I compiled and ran on Ubuntu 20.04, and I get the following thrown exception:
ArgumentNullException: Value cannot be null. (Parameter ‘key’)
This seems to be the exact same issue that was encountered in Feb 2019, on MacOS / Linux, on .NET Core 2.2:
I am awaiting resolution of this issue before purchasing. I am currently a happy user of Aspose.Words on Linux, with a Developer OEM license. If this issue is resolved and I am able to successfully run on Linux, I will purchase the same license for Aspose.PDF.
Here is the stack trace:
eric@eric-ubuntu:~/Documents/20-08-19-Aspose-Pdf-First$ dotnet run
/home/eric/Documents/ittl2/AVGL/13462649.pdf
13462649
.pdf
/home/eric/Documents/ittl2/AVGL/13462649image1_out.jpg
Unhandled exception. System.ArgumentNullException: Value cannot be null. (Parameter 'key')
at System.Collections.Generic.Dictionary`2.FindEntry(TKey key)
at System.Collections.Generic.Dictionary`2.TryGetValue(TKey key, TValue& value)
at #=zwOlmJWC0f5xaBb03$4$O55e4hGtJNOHxbQ==.#=zdG5j0Q4=(#=z9YeI0io= #=zAMh25x0=)
at #=zLqz12LYV$GPxQ6F56k7S1FQdrowBmJgDAg==.#=zOeJFTKwFsxFw.#=z3j8uTJ9FssxY(#=z9YeI0io= #=zAMh25x0=, #=zD_bqYRU=& #=zFGXr3$M=)
at #=zi3TXwmokU72w_8_FToZ9wAzknVruVLUxTrlvJzg=.#=zVTCBAtGRgKyy(#=z2$0aIuTnlxZFBS39b42X$FF6D2iI #=znJ3LyUAgOGtv)
at #=zVzWhE1DM4Ie1knWt6njKwsCHnQfh.#=zxuZ4ZGg=(#=zJRoqtKfEX723e63CZPklJ$i67_W1VnvqkWS2DOGUBU0$& #=zRBCY2gg=)
at #=zVzWhE1DM4Ie1knWt6njKwsCHnQfh.#=zxuZ4ZGg=()
at Aspose.Pdf.Devices.ImageDevice.#=zxuZ4ZGg=(Page #=zZPNcMZI=)
at Aspose.Pdf.Devices.JpegDevice.Process(Page page, Stream output)
at Program.ProcessPdf(FileInfo fi) in /home/eric/Documents/20-08-19-Aspose-Pdf-First/Program.cs:line 73
at Program.ProcessDirectory(DirectoryInfo di) in /home/eric/Documents/20-08-19-Aspose-Pdf-First/Program.cs:line 19
at Program.ProcessDirectory(DirectoryInfo di) in /home/eric/Documents/20-08-19-Aspose-Pdf-First/Program.cs:line 23
at Program.Main(String[] args) in /home/eric/Documents/20-08-19-Aspose-Pdf-First/Program.cs:line 12
eric@eric-ubuntu:~/Documents/20-08-19-Aspose-Pdf-First$
Here is the program:
using System;
using System.IO;
using Aspose.Pdf;
using Aspose.Pdf.Devices;
using Aspose.Pdf.Drawing;
class Program
{
static void Main(string[] args)
{
DirectoryInfo di = new DirectoryInfo(@"/home/eric/Documents/ittl2");
ProcessDirectory(di);
}
private static void ProcessDirectory(DirectoryInfo di)
{
foreach (var fi in di.GetFiles("*.pdf"))
{
ProcessPdf(fi);
}
foreach (var subDi in di.GetDirectories())
{
ProcessDirectory(subDi);
}
}
private static void ProcessPdf(FileInfo fi)
{
Console.WriteLine(fi.FullName);
var pdfDi = fi.Directory;
var ext = fi.Extension;
var baseName = fi.Name.Substring(0, fi.Name.Length - ext.Length);
Console.WriteLine(baseName);
Console.WriteLine(ext);
Document pdfDocument = new Document(fi.FullName);
for (int pageCount = 1; pageCount <= Math.Min(pdfDocument.Pages.Count, 4); pageCount++)
{
var imagePath = System.IO.Path.Combine(pdfDi.FullName, baseName + "image" + pageCount + "_out" + ".jpg");
Console.WriteLine(imagePath);
var fi2 = new FileInfo(imagePath);
if (fi2.Exists)
fi2.Delete();
using (FileStream imageStream = new FileStream(System.IO.Path.Combine(pdfDi.FullName, baseName + "image" + pageCount + "_out" + ".jpg"), FileMode.Create))
{
// Create JPEG device with specified attributes
// Width, Height, Resolution, Quality
// Quality [0-100], 100 is Maximum
// Create Resolution object
Resolution resolution = new Resolution(300);
// JpegDevice jpegDevice = new JpegDevice(500, 700, resolution, 100);
JpegDevice jpegDevice = new JpegDevice(resolution, 100);
// Convert a particular page and save the image to stream
jpegDevice.Process(pdfDocument.Pages[pageCount], imageStream);
// Close stream
imageStream.Close();
}
}
}
}