PDFBookmarkEditor.Save hangs when trying to save Large PDF with added bookmarks

Hi,

When using PDFBookMarkEditor to save bookmarks to a PDF, I came across a problem that causes PDFBookmarkEditor.Save to hang and not return control.

See the following command line app (In Visual Studio, create a new Command Line app, add Aspose.pdf.kit as a reference (tested with the latest v5 dll) and overwrite the Main sub with the below code;)
'
Sub Main()
Dim sourcePDF As String = “C:\UXGuide.pdf”
Dim destPDF As String = “C:\UXGuide_2.pdf”
Dim Editor As New Aspose.Pdf.Kit.PdfBookmarkEditor

Dim ExtractedBookMarks As Aspose.Pdf.Kit.Bookmarks
Dim NewBookmark As New Aspose.Pdf.Kit.Bookmark

Editor.BindPdf(sourcePDF)
ExtractedBookMarks = Editor.ExtractBookmarks()
NewBookmark.ChildItem = ExtractedBookMarks

'We must bind to the source PDF again because PdfBookmarkEditor.ExtractBookmarks
'closes the stream to the source PDF
Editor.BindPdf(sourcePDF)

Editor.DeleteBookmarks()

Editor.CreateBookmarks(NewBookmark)

'Normally I would add other bookmarks here and then re-add the extracted bookmarks
'it works for small PDF’s but this is an example of a PDF that causes an exception


Try
Editor.Save(destPDF)
Catch ex As Exception
Console.Write(ex.ToString)
End Try

Console.Write(Environment.NewLine)
Console.Write(“Press any key to end…”)
Console.Read()

End Sub


The save call will not return, and the application will be left waiting for it to return. It does not even throw an exception. (if you are lucky enough to get it to throw an exception, it may return SystemNullException).

The PDF I used for this test can be found here;

Windows User Experience Interaction Guidelines (pdf)
http://www.microsoft.com/downloads/en/confirmation.aspx?displaylang=en&FamilyID=e49820cb-954d-45ae-9cb3-1b9e8ea7fe8c

Any help in explaining why this happens, how it can be avoided would be appreciated.

Regards,

Hi Chris,

We’ll investigate this issue at our end and you’ll be updated with the results the earliest possible.

We’re sorry for the inconvenience.
Regards,

Hi Chris,

I have reproduced this issue at my end and logged it as PDFKITNET-21830 in our issue tracking system. Our team will look into this issue and you’ll be updated via this forum thread once it is resolved.

We’re sorry for the inconvenience.
Regards,

Hi,

Please can you provide me with an update on this.

I have encountered this issue again with other PDFs of around 100 meg.

having left the process to run for a long time it eventually throws the following;

System.OutOfMemoryException: Exception of type ‘System.OutOfMemoryException’ was thrown.
at x30a678191d9b1780.x53863f600a504e05.x4130c8bdf0fc6582(Stream x160656af282c4d7b, Stream x0ecec17af75ebd13, String x319214f212f6cc99, String x682935b2be2ef7d8, Int32 xee83661000f1d43d, KeySize xc66814dd5c4c3675, Algorithm x39d921119affa3f4)
at x30a678191d9b1780.x53863f600a504e05.x4130c8bdf0fc6582(Stream x160656af282c4d7b, Stream x0ecec17af75ebd13)
at Aspose.Pdf.Kit.PdfBookmarkEditor.Save(Stream outputStream)
at Aspose.Pdf.Kit.PdfBookmarkEditor.Save(String outputFile)

This is a serious issue that we need looked at now, we cannot begin to ship our product while this bug exists because 1) we need to be able to add bookmarks to large documents and 2) because no error occurrs until there is no memory left the method is unresponsive and we have no way of knowing if it will work until it either completes after a very long time or the System.OutOfMemoryException is thrown.

The System.OutOfMemoryException suggests to me you have a memory leak occurring in this class - I have not come across this problem with other classes in the toolkit.

Ok, the stack trace I posted above was using Aspose. PDF.Kit 4.9

If I use Aspose.PDF.Kit 5.2 then it still throws System.OutOfMemoryException but the stack trace is;
System.OutOfMemoryException: Exception of type ‘System.OutOfMemoryException’ was
thrown.
at System.IO.MemoryStream.ToArray()
at Aspose.Pdf.Kit.PdfBookmarkEditor.Save(Stream outputStream)
at Aspose.Pdf.Kit.PdfBookmarkEditor.Save(String outputFile)

Hi Chris,

I’m sorry to inform you that this issue is not yet resolved. However, I have asked our development team to share the ETA of this issue. You’ll be notified via this forum thread as soon as I receive a response.

We’re sorry for the inconvenience.
Regards,

Hi Chris,

Our team has further investigated this issue and I would like to share with you that the fix for this issue will be available in our monthly release due early March 2011. You’ll be notified via this forum thread once it is released.

We’re sorry for the inconvenience.
Regards,

The issues you have found earlier (filed as 21830) have been fixed in this update.


This message was posted using Notification2Forum from Downloads module by aspose.notifier.

Hi,


On BookmarkEditor.Save method I am getting IOException - the process cannot access the file because it is being used by another process for a PDF that has 160 MB in size using Aspose.Pdf version 7.6

using (PdfBookmarkEditor pdfBookmarkEditor = new PdfBookmarkEditor())
{
pdfBookmarkEditor.BindPdf(_pdfPath);
pdfBookmarkEditor.DeleteBookmarks();

foreach (Bookmark bMark in bookmarks)
{
pdfBookmarkEditor.CreateBookmarks(bMark);
}

pdfBookmarkEditor.Save(_pdfPath); – here I am getting that error.
}

Hi Andrei,


Thanks for your inquiry. We will appreciate if you please share your source document via some file sharing services e.g. dropbox, skydrive etc. We will test the scenario at our end and will guide you accordingly.

Moreover, you are using an old version. We will suggest you to download and try latest release of Aspose.Pdf for .NET 9.0.0. As we have made number of fixes and enhancement in API since 7.6.0.

We are sorry for the inconvenience caused.

Best Regards,

Hello,


We cannot use Aspose.Pdf version 9.0 for this release, so we need to cope with Aspose 7.6 for the time being.

The final pdf file gets generated from concatenating more than 100 documents (word, emails, txt files). I have them on the disk, I convert each one to pdf and then concatenate them and add pdf bookmarks for each of the documents in the final file.

The code looks like that:

// this creates an array of pdf documents.
Aspose.Pdf.Document[] pdfs = CreatePdfArray(documents);

Aspose.Pdf.Facades.PdfFileEditor pdfEditor = new PdfFileEditor();

using (Aspose.Pdf.Document finalDocument = new Aspose.Pdf.Document())
{
// concatenate all pdfs into a single one and then save it to disk.
pdfEditor.Concatenate(pdfs, finalDocument);
finalDocument.Save(pdfPath);
}


Then we add the bookmarks:

using (PdfBookmarkEditor pdfBookmarkEditor = new PdfBookmarkEditor())
{
pdfBookmarkEditor.BindPdf(pdfPath);

// delete any bookmarks if they existed before.
pdfBookmarkEditor.DeleteBookmarks();

foreach (Aspose.Pdf.Facades.Bookmark bMark in bookmarks)
{
pdfBookmarkEditor.CreateBookmarks(bMark);
}
// save the file again with the bookmarks.
pdfBookmarkEditor.Save(pdfPath); – this is where it throws IO exception
}

This is only happening with hundreds of documents that concatenate into a single file of size greater than 150 MB on disk.

You can try on your end with 150 PDF files: concatenate them into an array and then add bookmarks for each of them. Then save the file.

Hope this gives you more details about my issue.

Andrei

Hi Andrei,


Thanks for sharing additional information. Please note we maintain a single code base of Aspose Components, so all the fixes and new features/enhancements are implemented in latest version of DLL. So we are unable to provide any hotfix for older versions. We will suggest you to download and try latest release of Aspose.Pdf, hopefully it will fix the issue.

Moreover, to test your scenario at our end we need a working code as we are unable to resolve bookmarks reference. Please provide a working sample code.


// delete any bookmarks if they existed before.
pdfBookmarkEditor.DeleteBookmarks();

foreach (Aspose.Pdf.Facades.Bookmark bMark in bookmarks)
{
pdfBookmarkEditor.CreateBookmarks(bMark);
}

We are sorry for the inconvenience caused.

Best Regards,


Hello,

The complete error that I get is:

The process cannot access the file 'Temp\Document 19 March.pdf' because it is being used by another process.

The code for creating the bookmarks is here:

Bookmarks bookmarks = new Bookmarks();
for (int i = 0; i < 10; i++)
{
Aspose.Pdf.Facades.Bookmark newBookmark = new Aspose.Pdf.Facades.Bookmark()
{
Title = "level " + i,
Action = "GoTo",
PageNumber = 10
};

for (int j = 1; j <= 30; j++)
{
Aspose.Pdf.Facades.Bookmark childBookmark = new Aspose.Pdf.Facades.Bookmark()
{
Title = "document " + j,
Action = "GoTo",
PageNumber = j
};

newBookmark.ChildItems.Add(childBookmark);
}

bookmarks.Add(newBookmark);
}

The idea is that we have a lot of nested bookmarks with bookmarks that can contain tens or hundreds of other child bookmarks and so on.

Example:

Bookmark 1 (Level 0)
- 100 children Bookmarks (Level 1)

Bookmarrk 2 (Level 0)
- 100 children Bookmarks (Level 1)

You can increase the number of bookmarks and make sure the final PDF file is pretty large (>150 MB or >200MB).

Hope you can reproduce it now.

Thanks,
Andrei

Hi Andrei,


Thanks for sharing additional information and your patience. We have manged to reproduce the reported issue with a 274MB file and logged it as PDFNEWNET-36621 for further investigation and resolution. We will notify you via this forum thread as soon as it is resolved.

We are sorry for the inconvenience caused.

Best Regards,

Hi Andrei,


Thanks for your patience. We have further investigated the issue PDFNEWNET-36621. If you want to make incremental updates then you should change your code as following:
1. Create FileStream for source file with Open FileMode and ReadWrite Access.
2. Pass this stream as parameter into BindPdf() method.
3. Pass the same stream into Save() method

Please check following updated sample code snippet for the purpose. Hopefully it will help you to accomplish the task.


Aspose.Pdf.Facades.Bookmarks bookmarks = new
Aspose.Pdf.Facades.Bookmarks();<o:p></o:p>

for (int i = 0; i < 10; i++)

{

Aspose.Pdf.Facades.Bookmark newBookmark = new Aspose.Pdf.Facades.Bookmark()

{

Title = "level " + i,

Action = "GoTo",

PageNumber = 10

};

for (int j = 1; j <= 30; j++)

{

Aspose.Pdf.Facades.Bookmark childBookmark = new Aspose.Pdf.Facades.Bookmark()

{

Title = "document " + j,

Action = "GoTo",

PageNumber = j

};

newBookmark.ChildItems.Add(childBookmark);

}

bookmarks.Add(newBookmark);

}

PdfBookmarkEditor pdfBookmarkEditor = new PdfBookmarkEditor();

//create FileStream - open source file.

FileStream stream = new FileStream("36621.pdf", FileMode.Open, FileAccess.ReadWrite);

pdfBookmarkEditor.BindPdf(stream);

// delete any bookmarks if they existed before.

pdfBookmarkEditor.DeleteBookmarks();

foreach (Aspose.Pdf.Facades.Bookmark bMark in bookmarks)

{

pdfBookmarkEditor.CreateBookmarks(bMark);

}

// save the file again with the bookmarks.

//save result into the same stream.

pdfBookmarkEditor.Save(stream);



Please feel free to contact us for any further assistance.


Best Regards,

The issues you have found earlier (filed as PDFNEWNET-36621) have been fixed in Aspose.Pdf for .NET 9.2.0.

The blog post for this release is created over this link


This message was posted using Notification2Forum from Downloads module by Aspose Notifier.