BCL easyPDF SDK
easyPDF SDK Usermanual
PDF Creator Programming API  |  Download Free Trial  |  Contact Us to Purchase

Native .NET PDFProcessor API

Usage

The native .NET API is very similar to the COM API, with only a few distinct differences.

First, you need to add using BCL.easyPDF.PDFProcessor;.

Then you need to add reference to the BCL.easyPDF.PDFProcessor.dll assembly, which is under C:\Program Files\Common Files\BCL Technologies\easyPDF 8.

.NET Framework 3.5 or above is required.

COM exceptions were replaced by the new PDFProcessorException.

The PDFProcessor class does not have a Dispose() method, it is simply garbage collected. Each member function inside PDFProcessor launches an individual external worker process. Even if a function throws, the external process quits automatically. That means you can use PDFProcessor without worrying about resource management.

The minimal C# sample code looks like this:
(Note: all Native .NET sample code and declarations have a light yellow background color)

PDFProcessor processor = new PDFProcessor();
int numberOfPages = processor.GetPageCount(@"c:\test\input.pdf");

The PDFProcessorHandle class is inherited from IDisposable, which means it really needs to be deterministically disposed. Relying on the garbage collector is not recommended, because each PDFProcessorHandle object launches a separate worker process. Even though these worker processes are sleeping while not executing a function, they are still in the memory, and only really quit when the PDFProcessorHandle object is disposed.

The PDFProcessorHandle object should be treated as if it were an expensive resource, such as a file, mutex, or a database connection. If you would like to know what really is inside PDFProcessorHandle, it is just a named pipe. However, the worker process is programmed to only quit when the pipe is closed.

If the customer's application crashes, the system automatically closes all pipes belonging to the process, which means all related worker processes automatically quit as well.

The minimal C# sample code looks like this:

PDFProcessor processor = new PDFProcessor();
using(PDFProcessorHandle handle = processor.OpenFile(@"c:\test\input.pdf", @"c:\test\output.pdf", ""))
{
   int numberOfPages = handle.GetPageCount();
}

The key here is the using keyword, which gives PDFProcessorHandle a deterministic behavior.

An alternative solution, especially if you are catching exceptions, is the try/finally idiom:

PDFProcessor processor = new PDFProcessor();
PDFProcessorHandle handle = null;
try
{
   handle = processor.OpenFile(@"c:\test\input.pdf", @"c:\test\output.pdf", "");
   int numberOfPages = handle.GetPageCount();
}
catch(PDFProcessorException ex)
{
   Console.WriteLine(ex.Message);
}
finally
{
   if(handle != null)
      handle.Dispose();
}

The key here is the finally block, which calls Dispose().

PDFProcessor's constructor is designed to never throw exceptions. That's because it does not launch a worker process and does not create a named pipe, it merely initializes a few variables to their default values. In other words, creating a PDFProcessor object is extremely lightweight, like creating a Color object.

However, as soon as you do anything else, it instantly launches a worker process.

Launch Timeout

Launching the worker process and connecting the named pipe should happen very fast, in milliseconds. However, under an abnormally heavy load, the computer may not have enough cycles to perform this task in a timely fashion.

The Native .NET API has an internal timeout built-in. The default value is 1 minute. This is because a server may momentarily slow down so much that it is not responding for seconds, but it usually recovers after a while.

The default timeout value can be changed via the LaunchTimeout member (in milliseconds). This must be performed immediately after the creation of the PDFProcessor object.

PDFProcessor processor = new PDFProcessor();
processor.LaunchTimeout = 30000; // 30 seconds
int numberOfPages = processor.GetPageCount(@"c:\test\input.pdf");

Upon timeout an exception is thrown, and the SDK cannot be used. Another attempt may be made later.