Growing PDF File Size

In some cases, the file size of a document will increase with each save. This could be particularly noticeable if the document contains image annotations with large images and is saved often, either explicitly or via any of the available save triggers.

Incremental saving, which is enabled by default, is the strategy PSPDFKit uses to make sure saving is as fast as possible: It always appends changes to the end of the document, but it never deletes anything. However, even though this is the fastest way of saving a document, it does come with the cost of incrementing the file size with each save. You can read more about incremental vs. full saving in PDFs here.

In most cases, the file size increase is negligible. However, there might be some cases in which you want to prioritize file size over saving performance. Below you’ll find some strategies to prevent the file size from growing unnecessarily when saving changes to a document.

Rewriting a Document When Saving

You can trigger a non-incremental document save, which will rewrite the entire document instead of appending changes at the end.

This can be done by setting PSPDFDocumentSaveOptionStrategy to PSPDFDocumentSaveStrategyRewrite as follows:

try [Document.SaveOption.strategy(.rewrite)])
[document saveWithOptions:@{PSPDFDocumentSaveOptionStrategy: @(PSPDFDocumentSaveStrategyRewrite)} error:&error]

Keep in mind that rewriting the document does not guarantee that the file size will be the same after saving.

Optimizing Size Even Further

Instead of using PSPDFDocumentSaveStrategyRewrite, you can opt for the more aggressive PSPDFDocumentSaveStrategyRewriteAndOptimizeFileSize. This will cause PSPDFKit to first determine which objects in the PDF are no longer required and then remove them from the final output:

try [Document.SaveOption.strategy(.rewriteAndOptimizeFileSize)])
[document saveWithOptions:@{PSPDFDocumentSaveOptionStrategy: @(PSPDFDocumentSaveStrategyRewriteAndOptimizeFileSize)} error:&error]

While this leads to smaller file sizes, performing this operation is not free and will increase the time it takes to save documents.

ℹ️ Note: Do not rely on file size optimization to get to a target file size. This option does not provide any kind of guarantee that the resulting file will be smaller by a fixed number of bytes.

Disabling Incremental Save When Using Auto-Save

The method described above only works when you are saving manually. But it is also possible to disable incremental saving when using auto-save with -pdfViewController:shouldSaveDocument:withOptions::

func pdfViewController(_ pdfController: PDFViewController, shouldSave document: Document, withOptions options: AutoreleasingUnsafeMutablePointer<NSDictionary>) -> Bool {
    let mergedOptions = NSMutableDictionary(dictionary: options.pointee)
    mergedOptions.addEntries(from:[PDFDocumentSaveOption.strategy: Document.SaveStrategy.rewrite.rawValue])
    options.pointee = mergedOptions
    return true
- (BOOL)pdfViewController:(PSPDFViewController *)pdfController shouldSaveDocument:(PSPDFDocument *)document withOptions:(NSDictionary<PSPDFDocumentSaveOption,id> * _Nonnull __autoreleasing *)options {
    NSMutableDictionary *mergedOptions = [NSMutableDictionary dictionaryWithDictionary:*options];
    [mergedOptions addEntriesFromDictionary:@{PSPDFDocumentSaveOptionStrategy: @(PSPDFDocumentSaveStrategyRewriteAndOptimizeFileSize)}];
    *options = mergedOptions;
    return YES;