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 with the PSPDFDocumentSaveOptionForceSaving save option as follows:

1
try document.save(options: [.forceSaving])
1
[document saveWithOptions:@{PSPDFDocumentSaveOptionForceSaving: @YES} 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

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:

1
try document.save(options: [PSPDFDocumentSaveOption.strategy: PSPDFDocumentSaveStrategy.rewriteAndOptimizeFileSize.rawValue])
1
[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.

When PSPDFDocumentSaveStrategyRewriteAndOptimizeFileSize or PSPDFDocumentSaveStrategyRewrite are used, incremental saving will automatically be disabled.

ℹ️ 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::

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;
}