PDFsharp & MigraDoc Foundation
http://forum.pdfsharp.com/

Multithread and XGraphics.DrawImage
http://forum.pdfsharp.com/viewtopic.php?f=2&t=4450
Page 1 of 1

Author:  SuperMENG [ Tue Jun 06, 2023 5:00 am ]
Post subject:  Multithread and XGraphics.DrawImage

I attempted to utilize C# multithreading to process a PDF document, where each thread handles a separate page and then combines them. Overall, it functions well, but occasional errors occur. Specifically, the error stems from the XGraphics.DrawImage method, indicating that the same key already exists in the table.

To address this issue, I implemented a lock on the document object before calling the XGraphics.DrawImage method. While this solution resolves the problem and prevents conflicts, it does introduce some slowdown because the method has to wait for other threads to finish their tasks due to the document lock.

I'm uncertain about whether XGraphics.DrawImage directly relates to the document (potentially involving ID retrieval) and requires locking the entire document to prevent the error. If it is possible, Could I consider separating the locking mechanism from the DrawImage method. This way, I could lock the document specifically for operations related to keys or IDs, while allowing DrawImage to proceed independently on its own thread. This separation might potentially enhance performance by minimizing the need for thread synchronization caused by the document lock. Consequently, it could result in improved efficiency.

Thank you.

Author:  Thomas Hoevel [ Tue Jun 06, 2023 8:10 am ]
Post subject:  Re: Multithread and XGraphics.DrawImage

SuperMENG wrote:
I attempted to utilize C# multithreading to process a PDF document, where each thread handles a separate page and then combines them.
PDFsharp was designed to support document creation with only a single thread per document, but multiple threads creating individual documents at the same time.

With your approach of using multiple threads for a single document, you may encounter problems.
Adding an image to a PDF requires adding several objects to the PDF. Several pages can share the same image.

Author:  SuperMENG [ Wed Jun 07, 2023 7:28 am ]
Post subject:  Re: Multithread and XGraphics.DrawImage

Thomas Hoevel wrote:
SuperMENG wrote:
I attempted to utilize C# multithreading to process a PDF document, where each thread handles a separate page and then combines them.
PDFsharp was designed to support document creation with only a single thread per document, but multiple threads creating individual documents at the same time.

With your approach of using multiple threads for a single document, you may encounter problems.
Adding an image to a PDF requires adding several objects to the PDF. Several pages can share the same image.


Thank you for the information. I have now resolved this issue by simultaneously composing several documents using multithreading. As a result, the processing speed has been maximized to the best extent possible

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/