I'm creating full page images for a PDF, and it's very slow. I attempted to convert the loop that creates page images to a parallel foreach, however Spire.PDF throws errors internally:
- Code: Select all
var dpi = 300;
Parallel.ForEach(pages, (page) =>
{
// add full page images
var image = page.Document.SaveAsImage(page.PageNumber, PdfImageType.Bitmap, dpi, dpi) as Bitmap;
if (image?.Image != null)
{
images.Add(image);
}
});
I get different errors at different locations.
Error #1)
`Object reference not set to an instance of an object`
- Code: Select all
at spr⤥.ᜁ(Graphics A_0, spr⤢ A_1, Boolean A_2, Boolean A_3)
at spr⤢.ᜀ(String A_0, spr⦅ A_1, String A_2, Boolean A_3)
at spr⤢.ᜀ(String[] A_0, String A_1)
at spr⤢.ᜀ(spr⦈ A_0)
at spr⤢.()
at spr⤢.ᜍ(Boolean A_0)
at sprᥰ.ᜀ(Graphics A_0, spr⤙ A_1, spr⦇ A_2, spr⦉ A_3, Image A_4)
at sprᥰ.ᜁ(Int32 A_0, Int32 A_1, Int32 A_2)
at Spire.Pdf.PdfDocumentBase.ᜀ(Int32 A_0, Int32 A_1, Int32 A_2)
at Spire.Pdf.PdfDocumentBase.ᜀ(Int32 A_0, PdfImageType A_1, Int32 A_2, Int32 A_3)
at Spire.Pdf.PdfDocument.SaveAsImage(Int32 pageIndex, PdfImageType type, Int32 dpiX, Int32 dpiY)
at PdfExtractor.CreateFullPageImage(PdfDocument doc, Int32 pageNumber, Int32 imageId, ImageSizes size, String aboveContext)
Error #2)
- Code: Select all
System.InvalidOperationException: 'Object is currently in use elsewhere.'
Error #3)
- Code: Select all
Collection was modified; enumeration operation may not execute
Error #4)
- Code: Select all
System.ArgumentNullException: 'Value cannot be null. (Parameter 'font')
if it is not currently thread-safe, is it possible for someone to fix this issue and implement proper thread safety? It would likely be trivial to fix by placing proper locks on your data collections or utilizing concurrent types.