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

Free Support Forum - aspose.com

Infinite loop and uncaught System.Exception when saving bookmarks

I’ve got a PDF file uploaded from a remote user that for some reason, when saving the modifications to a memoryStream, create an infinite loop of sorts. Inside a Try Catch block, the bookmark object is created and when pdfDocument1.Save(pdfFS) is reached, the only feedback feedback comes from the Output window: Exception thrown: ‘System.Exception’ in Aspose.Pdf.dll. That repeats for as long as I’m willing to allow it. The odd thing is the catch block isn’t hit – so on some level, the exception isn’t really thrown.

I’ve downloaded the PDF, resaved from Adobe Acrobat as a new reduced PDF, and the code runs fine again. My concern is encountering other files with the problem and never being alerted. How can I get more information on this exception? How can I ensure the exception gets pushed back to my Try Catch block?

@hejira

Thank you for contacting support.

Would you please share respective source files and narrowed down code snippet so that we may try to reproduce and investigate it in our environment. Before sharing requested data, please ensure using latest version of the API.

Thank you for replying. I would like to share the file, but unfortunately these are high school transcripts and subject to FERPA. Any modification made to the files to hide sensitive data also corrects the file’s error.

Out of 100 or so files, 4 created this error. I will tinker with these to see if they have anything in common. The code where where the error occurs is pdfDocument1.Save(pdfFS) – again, this code runs fine for almost all of the files. Finding the reason the Save method exception is looping seems key. Sorry i can’t share the file.

Public Shared Function BookmarkPDF(ByRef doc1Bytes As Byte(), ByVal newBookmarkDesc As String, Optional recordID As Integer = 0, Optional ByRef w As webAppUser = Nothing, Optional nomName As String = “”) As Byte()

    ' adds an initial bookmark to a document's outline collection

    Dim pdfFS As Stream = New MemoryStream

    Dim pdflicense As Aspose.Pdf.License = New Aspose.Pdf.License()
    pdflicense.SetLicense(cfg.localPath & "\App_Data\Aspose.Pdf.lic")
    pdflicense.Embedded = True

    'a new PDF object is needed.  
    Dim pdfDocument1 As Aspose.Pdf.Document = Nothing
    Dim fs1 As Stream = New MemoryStream(doc1Bytes)

    Try

        ' save the bytes to the new PDF
        pdfDocument1 = New Aspose.Pdf.Document(fs1)

        ' set the bookmark to page 1; clear any existing artifactual bookmarks as they can cause errors if more are added later
        Dim bookmarkAt As Integer = 1
        pdfDocument1.Outlines.Delete()

        If newBookmarkDesc <> "" Then

            ' create a bookmark object
            Dim pdfOutline As OutlineItemCollection = New OutlineItemCollection(pdfDocument1.Outlines)
            pdfOutline.Title = newBookmarkDesc
            pdfOutline.Italic = True
            pdfOutline.Bold = True
            pdfOutline.Action = New Aspose.Pdf.Annotations.GoToAction(pdfDocument1.Pages(bookmarkAt))  ' Set the destination page number

            ' Add a bookmark in the document's outline collection.
            pdfDocument1.Outlines().Add(pdfOutline)

        End If

        'save the document to a memory stream
        pdfDocument1.Save(pdfFS)

    Catch ex As Exception
        ' some files are corrupted/encrypted and cannot be merged.   insert a blank page with text.
        sendMissingPageAlert(newBookmarkDesc, ex.Message, recordID, w, nomName)
        pdfDocument1 = createMissingPage(newBookmarkDesc)
        pdfDocument1.Save(pdfFS)
    Finally
        If Not IsNothing(pdfDocument1) Then pdfDocument1.Dispose()
    End Try

    ' tidy up and return the stream as a byte array
    fs1.Dispose()

    Return common.convertStreamToByteArray(pdfFS)

End Function

@hejira

Thank you for elaborating it further.

Please note that we need the data in order to reproduce it in our environment and then addressing your concerns. The data is used for testing purposes only while forum attachments are accessible to thread owner and Aspose staff only. Kindly share the data so that we may proceed further.