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

Free Support Forum - aspose.com

How to Implement IPageLayoutCallback and Interrupt Document.UpdateFields using .NET

Hi @tahir.manzoor, I have read your answers related to timeout issues. I am processing huge amount of files and I have to do Update Fields for each file. I implemented timeout using Interruption token, but since it is deprecated, can you please help me in achieving the same functionality using IPageLayoutCallback.

Code snippet I used is this one:

 public Document TryToUpdateFields(Document doc, int timeout)
        {
            InterruptionToken token = new InterruptionToken();
            bool finished = SaveWithTimeout(token,
           () =>

           {

               token.BindToCurrentThread();

               try

               {

                   doc.UpdateFields();

               }

               catch (Exception ex)

               {

                   Console.WriteLine("Interrupted");

               }

           }, timeout);


            return doc;

        }


        private bool SaveWithTimeout(InterruptionToken token, ThreadStart threadStart, int timeout)

        {

            Thread workerThread = new Thread(threadStart);

            workerThread.Start();


            bool finished = workerThread.Join(timeout);

            if (!finished)

            {

                token.Interrupt();

            }

            return finished;

        }

Thank you,
Nimisha

@nimijac

Following code example shows how to implement IPageLayoutCallback interface. Hope this helps you.

public class WordToPdf : Aspose.Words.Layout.IPageLayoutCallback
{
    private readonly System.Diagnostics.Stopwatch _stopwatch = new System.Diagnostics.Stopwatch();
    private readonly TimeSpan _timeout = TimeSpan.FromSeconds(30);

    public void DoConvert(Stream sourceStream)
    {
        var doc = new Aspose.Words.Document(sourceStream);
        doc.LayoutOptions.Callback = this;

        //using (var pdfStream = GetStream())
        {
            try
            {
               doc.UpdateFields();
              doc.Save(MyDir + "output.pdf", new PdfSaveOptions());
            }
            catch (Exception ex)
            {
                // oh no! The save timed-out
            }
        }
    }

    void IPageLayoutCallback.Notify(PageLayoutCallbackArgs args)
    {
        // Ensure Started
        if (!this._stopwatch.IsRunning)
        {
            this._stopwatch.Start();
        }

        // Check Timeout
        if (this._stopwatch.Elapsed > this._timeout)
        {
            this._stopwatch.Stop();
            //throw new MyTimeoutException($"The conversion timed-out after {this._timeout.TotalSeconds} seconds.");
            throw new Exception($"The conversion timed-out after {this._timeout.TotalSeconds} seconds.");
        }
    }
}

Hi @tahir.manzoor,

This is still not as efficient as interruption token. Do you have a better way?

Thank you,
Nimisha

@nimijac

In your case, you need to use IPageLayoutCallback interface. Could you please share the issue detail that you are facing by implementing this interface and not with InterruptionToken class?

Hi Tahir,

Sorry for replying so late.

When using pagecallback, doing updatepagelayout after updatefields is giving me exception. I believe it is because updatefields is not completed properly.

Is there a way to check if update fields is complete or not? I am implementing a bulk transaction , so I would like to know which files are in proper alignment.

Do you know why some word documents have “?” Instead of page numbers in table of contents. I have been working on this issue for past several days with no proper answer to this question.

Thank you,
Nimisha

This is the issue I get when calling updatepagelayout after updating fields using page callback. There are no inner exception or message, it just goes to catch block.

IMG_20210901_160259.jpg (1.31 MB)

Hi Tahir,

I implemented the same thing using interruption token, I got an error that “Page layout is still building”. Do you have any workaround for it?

Thank you,
Nimisha

@nimijac

You are facing the expected behavior of Aspose.Words. If you interrupt these methods, the exception may occur.

Please attach the following resources here for testing:

  • Your input Word document.
  • Please share your expected output/behavior.
  • Please create a standalone console application (source code without compilation errors) that helps us to reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we will start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip and upload them.

Aspose_TOC.zip (8.2 MB)
VM34617250–_final.docx (56.0 KB)

Hi Tahir,

I have attached sample project and input file to test the functionality. I have commented code for updatepagelayout as well as updating TOC using Interruption token. Please let me know if you have problem updating nuget packages.

As you can see in output file, instead of page numbers , “?” is coming. And I cant call updatepagelayout method because it will give an error.

As already mentioned, we are doing bulk import of files, if we dont use Ipagelayoutcallback or limit the time each file gets to update Table of Content, we cannot complete the import process successfully.

Thank you,
Nimisha

Unfortunately, we are unable to compile your shared project. Please check the attached image. image.png (83.2 KB)

Please create a standalone console application (source code without compilation errors) that helps us to reproduce your problem on our end and attach it here for testing.

Could you please share the problematic output document also?

Hi Tahir,

If I send a code that contains evaluation version of license, can you add license for version 21.4 and test it?

Thank you,
Nimisha

@nimijac

Yes, we will test your code with valid license of Aspose.Words. Please share the simplified code example to reproduce the same issue. Thanks for your cooperation.

CONSOLE_TOC.7z (5.2 MB)
Hi Tahir,

Please use license for v21.4, Input error file is already present in solution(App_data folder)

issues we are facing:

  1. Generates Table of contents with “?”.
  2. Does not generate PDF for these files.

Thank you,
Nimisha

@nimijac

We have investigated your issue and noticed that the output document is expected. The code interrupted the Document.UpdateFields method. So, the page numbers are not updated.

The PDF file is not save because the IPageLayoutCallback.Notify method is called and your code throws an exception.

Please note that when update field process or updating page layout is interrupted the output may not be correct.