Stack Overflow when converting a document to PDF - aspose.words 25.5.0 - .NET Framework 4.8

    Dim oDoc As Aspose.Words.Document = Nothing
    Dim oWordsOptions As Aspose.Words.Loading.LoadOptions = Nothing

    oWordsOptions = New Aspose.Words.Loading.LoadOptions
    oWordsOptions.ResourceLoadingCallback = New SkipImagesCallback
    oDoc = New Aspose.Words.Document(fileName:=sFile, loadOptions:=oWordsOptions)
    oDoc.LayoutOptions.Callback = New AsposeWordsRenderPageLayoutCallback

    With oDoc.LayoutOptions.RevisionOptions
        .ShowRevisionBars = False
        .ShowRevisionMarks = False
    End With

    oDoc.Save(fileName:=sWorkFile, saveFormat:=Aspose.Words.SaveFormat.Pdf)

Friend classes used in the above:

    Friend Class SkipImagesCallback
        Implements Aspose.Words.Loading.IResourceLoadingCallback

        Public Function ResourceLoading(args As Aspose.Words.Loading.ResourceLoadingArgs) _
                     As Aspose.Words.Loading.ResourceLoadingAction _
             Implements Aspose.Words.Loading.IResourceLoadingCallback.ResourceLoading

            Return Aspose.Words.Loading.ResourceLoadingAction.Skip
        End Function
    End Class

    Friend Class AsposeWordsRenderPageLayoutCallback
        Implements Aspose.Words.Layout.IPageLayoutCallback

        Private dStarted As Date = System.DateTime.Now

        Public Sub Notify(args As PageLayoutCallbackArgs) Implements IPageLayoutCallback.Notify
            If DateDiff(Interval:=DateInterval.Second, Date1:=dStarted, Date2:=System.DateTime.Now) > 15 Then
                Throw New Exception(message:="Conversion is taking too long")
            End If
        End Sub
    End Class

The result of the above code:

Process is terminated due to StackOverflowException.

AnonymizedCopy.zip (29.8 KB)

@rozeboosje

The StackOverflowException you are encountering during the conversion of a document to PDF using Aspose.Words could be related to the implementation of your IPageLayoutCallback. Specifically, the Notify method in your AsposeWordsRenderPageLayoutCallback class is designed to throw an exception if the conversion takes longer than 15 seconds. If the document being processed is large or complex, this could lead to repeated calls to the Notify method, resulting in a stack overflow.

To address this issue, consider the following steps:

  1. Increase the Timeout: You can increase the timeout duration in the Notify method to allow more time for the conversion process. For example, you might change the condition to check for a longer duration before throwing an exception.

    Public Sub Notify(args As PageLayoutCallbackArgs) Implements IPageLayoutCallback.Notify
        If DateDiff(Interval:=DateInterval.Second, Date1:=dStarted, Date2:=System.DateTime.Now) > 30 Then ' Increased to 30 seconds
            Throw New Exception(message:="Conversion is taking too long")
        End If
    End Sub
    
  2. Optimize Document Loading: Ensure that the document you are loading is not excessively large or complex. If possible, simplify the document or break it into smaller parts for processing.

  3. Check Resource Loading: Since you are using a custom resource loading callback to skip images, ensure that this callback is not causing any unintended side effects. You might want to test the conversion without the callback to see if the issue persists.

  4. Use Progress Callbacks: Instead of throwing an exception based on time, consider implementing a progress callback that can provide feedback on the loading process without terminating it.

  5. Review Documentation: For more details on handling document loading and callbacks, you can refer to the Aspose documentation on [LoadOptions](https://github.com/aspose-words/Aspose.Words-for-.NET/tree/master/Examples/DocsExamples/DocsExamples/File Formats and Conversions/Load Options/Working with LoadOptions.cs).

By following these suggestions, you should be able to mitigate the StackOverflowException and successfully convert your document to PDF.

If the problem persists, please provide additional details about the document you are trying to convert, as well as any other relevant code snippets or configurations.

Sources:
[1]: [Working with LoadOptions.cs](https://github.com/aspose-words/Aspose.Words-for-.NET/tree/master/Examples/DocsExamples/DocsExamples/File Formats and Conversions/Load Options/Working with LoadOptions.cs)

As you can see I’ve already implemented the suggestions made by your bot.

The error happens quite quickly, it certainly doesn’t take 15 seconds.

The attached document is an anonymised and very small copy of the original and it still causes the error.

Most other Word documents do not cause any errors.

@rozeboosje
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): WORDSNET-28248

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

1 Like