We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Multithreaded RedactionAnnotation.Redact throwing an exception

Hi all,

I have been trying to multi-thread RedactionAnnotation.Redact but keep hitting a System.IndexOutOfRangeException: Index was outside the bounds of the array. exception no matter which way I try to multi-thread. Example code below:

Using threads:

var documents = new[]
{
	@"Sample1.pdf",
	@"Sample2.pdf"
};

var threads = new List<Thread>();
foreach (var filePath in documents)
{
	var thread = new Thread(() =>
	{
		var document = new Document(filePath);
		foreach (var page in document.Pages)
		{
			for (var i = 0; i <= 10; i++)
			{
				var annotation = new RedactionAnnotation(page, new Rectangle(i, i, i + 1, i + 1))
				{
					FillColor = Color.Black, Color = Color.Black, BorderColor = Color.Black
				};

				page.Annotations.Add(annotation);
				annotation.Redact();
			}
		}
	});

	thread.Start();
	threads.Add(thread);
}

foreach (var thread in threads)
{
	thread.Join();
}

Using tasks:

var documents = new[]
{
	@"Sample1.pdf",
	@"Sample2.pdf"
};

var tasks = new List<Task>();
foreach (var filePath in documents)
{
	var task = new Task(() =>
	{
		var document = new Document(filePath);
		foreach (var page in document.Pages)
		{
			for (var i = 0; i <= 10; i++)
			{
				var annotation = new RedactionAnnotation(page, new Rectangle(i, i, i + 1, i + 1))
				{
					FillColor = Color.Black, Color = Color.Black, BorderColor = Color.Black
				};

				page.Annotations.Add(annotation);
				annotation.Redact();
			}
		}
	});
	task.Start();
	tasks.Add(task);
}

Task.WaitAll(tasks.ToArray());

Attached are the sample pdfs I used.
Thanks!
Pdf_Lock_Documents.zip (634.3 KB)

@bvk

A task under the ID PDFNET-47604 has been logged in our issue tracking system in order to investigate the recommended way to use API in multi-threaded environment. We will surely inform you as soon as the task is closed. Please be patient and spare us little time.

We are sorry for the inconvenience.