Using PSPDFDocument

Creating a PSPDFDocument is cheap, but accessing it's various properties might require expensive PDF parsing. So it's a very good idea to keep those objects around to avoid the expense of re-creating them whenever necessary.

Don't create a PSPDFDocument in code like collectionView:cellForItemAtIndexPath: but keep a dictionary/other data structure around that creates these documents as needed and then returns the same object as you need to access it again. This will improve performance quite a lot. Using NSCache with the UID as key and the document as object is ideal.

If you already have a way to identify your document's uniquely, use this and set the UID property to your unique string. If you don't set UID, PSPDFKit will extrapolate a UID automatically based on the path, files and maybe content. This process might be a bit expensive, so manually setting the UID is a good idea - if you already have such a system in place. If not, let PSPDFKit do its thing and don't worry too much.

If you create PSPDFDocument objects in a tight loop, ensure to add an @autoreleasepool so objects can be deallocated early. If you edit annotations, make sure there's only one document instance around where you edit. PSPDFKit automatically saves annotation state, and keeping multiple independent copies of the document around that all save to the file might corrupt the file.

Was this page helpful? We're happy to answer any questions.