Abort (terminate, cancel) PDFTron processing operations

Product: PDFTron.Net.x64

Product Version:9.5.0.1

Please give a brief summary of your issue:
(Think of this as an email subject)

Hello!
I have .Net 7 MVC (C#) application. My goal is to implement timeout option for operations that used PDFTron.NET library.
For example, my app needed to terminate all operations executed one after the second (in sync mode) such as:

Convert.OfficeToPDF()
Convert.ToXod()

And my question is hot to do it correctly, without side effects from PDF.Net library.

Please describe your issue and provide steps to reproduce it:
(The more descriptive your answer, the faster we are able to help you)

For ex, how to abort the operations, that running in the function:

    private (byte[] content, int pageCount) ConvertOfficeToXod(byte[] docData)
    {
        using (var filter = new MemoryFilter(docData.Length, true))
        using (var filterWriter = new FilterWriter(filter))
        {
            filterWriter.WriteBuffer(docData);
            filterWriter.Flush();
            using (var pdf = new pdftron.PDF.PDFDoc())
            {
                var opt = new pdftron.PDF.ConversionOptions();

                Convert.OfficeToPDF(pdf, filter, null);
                var pageCount = pdf.GetPageCount();

                return (ConvertToXOD(pdf), pageCount);
            }
        }
    }

Thanks!

Hello Dmitry,

You can cancel an in progress conversion. For the Office To PDF conversion I recommend using the StreamingPDFConversion which allows you to perform a page by page conversion allowing you to cancel the conversion as needed. You can find an example here:

Here is an example on canceling a XOD conversion using a filter.

Let us know if you have any questions.

Hello and thanks for you response.
Unfortunately, the first link is NOT FOUND page.

Dow you have some examples with StreamingPDFConversion with option to stop the conversion.

Another question, about DocumentConversion.CancelConversion(). I used to cancel conversion from another Task, but this not work, no cancelation, even after long period of time. The ConversionStatus always DocumentConversionResult.e_document_conversion_incomplete

Thanks.

Do you have some examples with StreamingPDFConversion with option to stop the conversion.
Yes. I have an updated example in our documentation found here: Convert-office | Apryse Documentation

Another question, about DocumentConversion.CancelConversion() . I used to cancel conversion from another Task, but this not work, no cancelation, even after long period of time.
Can you provide us an example that shows how you are canceling the conversion so I can test in my environment?

Yes. I have an updated example in our documentation found here: Convert-office | Apryse Documentation

Unfortunately, I am not found the ex with cancelation option.

Can you provide us an example that shows how you are canceling the conversion so I can test in my environment?

I have experimental with few of approaches, that one of them:

using (var cts = new CancellationTokenSource())
//using (var timer = new System.Timers.Timer(TimeSpan.FromSeconds(5)))
{
cts.CancelAfter(TimeSpan.FromSeconds(cancelAfter));
using (PDFDoc pdfdoc = new PDFDoc())
{
pdfdoc.InitSecurityHandler();
Console.WriteLine(“Converting from Office Stream”);

        using (OfficeToPDFOptions options = new OfficeToPDFOptions())
        {
            options.SetSmartSubstitutionPluginPath(pluginPath);

            using (var conversion = pdftron.PDF.Convert.StreamingPDFConversion(pdfdoc, inputPath + fileName, options))
            {
                                  var task = Task.Run(() =>
                 {
                     try
                     {
                         conversion.Convert();
                     }
                     catch (Exception ex)
                     {
                         Console.WriteLine($"Failed to convert {ex.Message}");
                     }
                 }, cts.Token);


                cts.Token.ThrowIfCancellationRequested();
                Console.WriteLine("Conversion started");

                while (conversion.GetConversionStatus() == DocumentConversionResult.e_document_conversion_incomplete)
                {
                    if (conversion.IsCancelled())
                    {
                        break;
                    }

                    if (cts.IsCancellationRequested)
                    {
                        conversion.CancelConversion();
                        Console.WriteLine("Conversion Canceled: " + conversion.IsCancelled());
                        Console.WriteLine("Task Status: " + task.Status);
                    }

                    Console.WriteLine("Task Canceled: " + task.IsCanceled);
                }

                if (conversion.GetConversionStatus() == DocumentConversionResult.e_document_conversion_success)
                {
                    // save the result
                    pdfdoc.Save(outputPath + outputFileName, SDFDoc.SaveOptions.e_linearized);
                    Console.WriteLine("Saved " + outputFileName);
                }
            }
        }
    }
}

I believe the issue here is that the cancelation token is not actually canceled. If you set it to canceled inside your task then it will be canceled during your conversion process, just throwing an exception from inside the task is not setting cts.Cancel();. Can you try that out and let me know if it works for you?

Hello and thanks for response.

Your can see in the code below, that Cancelation of the token is happened by the :

using (var cts = new CancellationTokenSource())
//using (var timer = new System.Timers.Timer(TimeSpan.FromSeconds(5)))
{
**cts.CancelAfter(TimeSpan.FromSeconds(cancelAfter));**

And Cancel of the Conversion is in the while loop in main thread

 while (conversion.GetConversionStatus() == DocumentConversionResult.e_document_conversion_incomplete)
                {
                    if (conversion.IsCancelled())
                    {
                        break;
                    }

                    if (cts.IsCancellationRequested)
                    {
                        **conversion.CancelConversion();**
                        Console.WriteLine("Conversion Canceled: " + conversion.IsCancelled());
                        Console.WriteLine("Task Status: " + task.Status);
                    }

                    Console.WriteLine("Task Canceled: " + task.IsCanceled);
                }

If You run this code, can be seen, that this function conversion.CancelConversion(); is not canceled the conversion running in separate Task.

In additional, can you provide examples with code of using CancelConversion(), in best case - using it in multitask environment (like we needed). Or another approach to cancel conversion at a given point in time.

Thanks!

I have attached an example that I believe will work for you, can you review it an update it. I run two tasks here one to perform the conversion and a second to cacel the cancelation token. You could use the same cancelation token in the second task but here I am passing it to the function and doing a page by page conversion of the input document, as it would cancel the second task and this allows you to exit the thread safely.
Program.cs (1.7 KB)

Thanks, I am checking your solution.