Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Thu Apr 07, 2022 7:24 pm

Hi there,

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.

replaysMike
 
Posts: 14
Joined: Tue Feb 08, 2022 8:43 pm

Fri Apr 08, 2022 10:16 am

Hi,

Thank you for your inquiry.
Our Spire.Pdf supports converting multiple PDF documents to images in multi-threading, while it doesn't support converting a Pdf with multiple pages to images at present. How much time did you spend using single thread? You could provide your PDF document, then we will investigate if the conversion time can be optimized. Please also tell us your test environment (Win10, 64bit). You can attach your file here or send it to us via email ([email protected]). Thank you in advance.

Sincerely,
Kylie
E-iceblue support team
User avatar

kylie.tian
 
Posts: 412
Joined: Mon Mar 07, 2022 2:30 am

Fri Apr 08, 2022 9:27 pm

Hi Kylie,

I'm converting a 64 page document exporting each page as a 300dpi image. I need that resolution as it's being analyzed by an OpenCV computer vision algorithm. It takes a good 30+ seconds to do as it only utilizes a single thread. I could probably get it to just a couple seconds on my 32-core processor if I could export each page on a different thread. And the thing is, I have 10,000+ documents to process.

I'm using the licensed version of SpirePDF. It's Windows 10, 64-bit on a 32-core AMD Threadripper CPU 128GB ram :)

PDF is https://www.dropbox.com/s/3zln2crwbs41ifu/lm358.pdf?dl=0 as your forum only allows 2MB uploads :-(

replaysMike
 
Posts: 14
Joined: Tue Feb 08, 2022 8:43 pm

Mon Apr 11, 2022 10:33 am

Hi,

Thank you your reply.
I used the latest Spire.PDF Pack(Hot Fix) Version:8.3.9 to convert your PDF to image in a single-thread, which took 12 seconds without DPI and 20 seconds with DPI. Setting a higher DPI will definitely take more time than the default DPI. The conversion time of around 20 seconds is actually a reasonable time. I'm afraid it is difficult to reduce the time further. How long do you want to optimize for?
You mentioned that you use the licensed version, have you purchased our Spire.PDF? I didn't find your purchase information according to your email. Could you please provide the authorized email address or order number for our reference? Thank you in advance.

Sincerely,
Kylie
E-iceblue support team
User avatar

kylie.tian
 
Posts: 412
Joined: Mon Mar 07, 2022 2:30 am

Tue Apr 12, 2022 11:12 pm

Hi Kylie - yes I absolutely have a paid license for Spire PDF. It's order #10881 - looks like I used my other email for the forum signup.

Yea the single thread time isn't so bad for me, it's just the fact I can't use more than a single thread to extract an image from different pages in the pdf doc. So all pages have to be done synchronously. Is your tested time for a single image or the whole document?

Maybe one workaround is to load the document in multiple threads and each extract a section of images. Not ideal, but haven't tried that yet.

replaysMike
 
Posts: 14
Joined: Tue Feb 08, 2022 8:43 pm

Wed Apr 13, 2022 10:33 am

Hi,

Thanks for sharing your order number.
The test time I provided was for the whole PDF document. I will submit a task to our dev team and they will investigate if the time(20s) could be optimized when doing the conversion in a single-thread. Once there is any update, I will inform you immediately.

Sincerely,
Kylie
E-iceblue support team
User avatar

kylie.tian
 
Posts: 412
Joined: Mon Mar 07, 2022 2:30 am

Thu Jul 10, 2025 1:24 am

Hello,

Thank you for your patience.

We just release official version(Hotfix: Spire.Pdf.11.7.2) which optimized the time consumption of converting PDF to images, welcome to test. Looking forward to your testing feedback.

Website:
https://www.e-iceblue.com/Download/download-pdf-for-net-now.html
Nuget:
https://www.nuget.org/packages/Spire.PDF/11.7.2
https://www.nuget.org/packages/Spire.PDFfor.NETStandard/11.7.2
Sincerely,
Talia
E-iceblue support team
User avatar

talia.liu
 
Posts: 341
Joined: Mon Apr 14, 2025 3:33 am

Return to Spire.PDF

cron