Growing PDF File Size

In some cases, you might experience a large increase in PDF file size when saving documents. This happens especially when using image annotations with large images and triggering a save of a document often.

This increase in file size is due to incremental saving, which PSPDFKit uses by default if saving is not configured in another way. Incremental saving always appends changes to the end of a document and never deletes anything. Incremental saving is used by default since it’s faster than rewriting an entire document, and usually, the file size increase is almost unnoticeable. As a result, it could be that the data of an image annotation is written and stored in a PDF multiple times.

Rewrite a Document When Saving

To prevent the document file size from growing, 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 with the PSPDFDocumentSaveOptionForceSaving save option as follows:

1
try document.save(options: [.forceSaving])
1
[document saveWithOptions:@{PSPDFDocumentSaveOptionForceSaving: @YES} error:&error]

Optimizing Size Even Further

In addition to disabling incremental saving, you can also use PSPDFDocumentSaveStrategyRewriteAndOptimizeFileSize in your saving options. This will cause PSPDFKit to first determine which objects in the PDF are no longer required and then remove them from the final output. While this leads to smaller file sizes, performing this operation is not free and will increase the time it takes to save documents. You can enable this as follows:

1
try document.save(options: [PSPDFDocumentSaveOption.strategy: PSPDFDocumentSaveStrategy.rewriteAndOptimizeFileSize.rawValue])
1
[document saveWithOptions:@{PSPDFDocumentSaveOptionStrategy: @(PSPDFDocumentSaveStrategyRewrite)} error:&error]

ℹ️ Note: When PSPDFDocumentSaveStrategyRewriteAndOptimizeFileSize or PSPDFDocumentSaveStrategyRewrite are used, incremental saving will automatically be disabled.

Disabling Incremental Save When Using Auto-Save

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

Copy
1
2
3
4
5
6
func pdfViewController(_ pdfController: PSPDFViewController, shouldSave document: PSPDFDocument, withOptions options: AutoreleasingUnsafeMutablePointer<NSDictionary>) -> Bool {
    let mergedOptions = NSMutableDictionary(dictionary: options.pointee)
    mergedOptions.addEntries(from:[PSPDFDocumentSaveOption.forceSaving: true])
    options.pointee = mergedOptions
    return true
}
Copy
1
2
3
4
5
6
- (BOOL)pdfViewController:(PSPDFViewController *)pdfController shouldSaveDocument:(PSPDFDocument *)document withOptions:(NSDictionary<PSPDFDocumentSaveOption,id> * _Nonnull __autoreleasing *)options {
    NSMutableDictionary *mergedOptions = [NSMutableDictionary dictionaryWithDictionary:*options];
    [mergedOptions addEntriesFromDictionary:@{PSPDFDocumentSaveOptionForceSaving: @YES}];
    *options = mergedOptions;
    return YES;
}