ByteScout PDF Renderer SDK - C# - Rendering In Background Thread - ByteScout
Announcement
Our ByteScout SDK products are sunsetting as we focus on expanding new solutions.
Learn More Open modal
Close modal
Announcement Important Update
ByteScout SDK Sunsetting Notice
Our ByteScout SDK products are sunsetting as we focus on our new & improved solutions. Thank you for being part of our journey, and we look forward to supporting you in this next chapter!

ByteScout PDF Renderer SDK – C# – Rendering In Background Thread

  • Home
  • /
  • Articles
  • /
  • ByteScout PDF Renderer SDK – C# – Rendering In Background Thread

ByteScout PDF Renderer SDK – C# – Rendering In Background Thread

Program.cs

using System;
using System.IO;
using System.Threading;
using Bytescout.PDFRenderer;

namespace RenderingInBackgroundThread
{
class Program
{
private static ManualResetEvent _doneEvent = new ManualResetEvent(false); // synchronization event
private static int _counter; // thread counter

static void Main(string[] args)
{
// Get all PDF files in current directory
string[] pdfFiles = Directory.GetFiles(“.”, “*.pdf”);
_counter = pdfFiles.Length;

// Render PDF files in separate threads
foreach (string pdfFile in pdfFiles)
{
Thread backgroundThread = new Thread(BackgroundThreadProc);
backgroundThread.Start(pdfFile);
Console.WriteLine(pdfFile + ” – conversion started.”);
}

// Wait until threads finished
_doneEvent.WaitOne();

Console.WriteLine(“Done.”);
Console.WriteLine();
Console.WriteLine(“Press any key to continue…”);
Console.ReadKey();
}

// Rendering thread function
private static void BackgroundThreadProc(object data)
{
string fileName = (string) data;

try
{
// Create renderer
using (RasterRenderer renderer = new RasterRenderer())
{
renderer.LoadDocumentFromFile(fileName);

// Setup rendering
RenderingOptions renderingOptions = new RenderingOptions();
renderingOptions.JPEGQuality = 90;

float renderingResolution = 300;

// Render document pages
for (int i = 0; i < renderer.GetPageCount(); i++) { renderer.Save(fileName + ".page" + i + ".jpg", RasterImageFormat.JPEG, i, renderingResolution, renderingOptions); } } Console.WriteLine(fileName + " - successfully converted."); } catch (Exception exception) { Console.WriteLine(exception.ToString()); } if (Interlocked.Decrement(ref _counter) == 0) { // Set event if all threads finished _doneEvent.Set(); } } } } [/csharp]


  Click here to get your Free Trial version of the SDK

Tutorials:

prev
next