Trying to make a PDF that can't be modified in any way and testing different dictionary options to see how they work. The problem is that while the documentation says that I can use one thing, it's not allowing me to use that thing and the app crashing. Here's my set up;
NSDictionary *tempDict = [NSDictionary dictionaryWithObjectsAndKeys:@"user", kCGPDFContextOwnerPassword, (kCGPDFAllowsCommenting | kCGPDFAllowsLowQualityPrinting), kCGPDFContextAccessPermissions, nil];
UIGraphicsBeginPDFContextToFile(self.savePath, CGRectZero, tempDict);
from the docs for UIGraphicsBeginPDFContextToFile:
A dictionary that specifies additional information to be associated with the PDF file. You can use these keys to specify additional metadata and security information for the PDF, such as the author of the PDF or the password for accessing it. The keys in this dictionary are the same keys you pass to the CGPDFContextCreate function and are described in the Auxiliary Dictionary Keys section of CGPDFContext. The dictionary is retained by the new context, so on return you may safely release it.
from the docs for kCGPDFContextAccessPermissions
/* The document's access permissions, expressed as a CFNumber. The number is defined by ORing together the desired CGPDFAccessPermissions values. */
CG_EXTERN const CFStringRef kCGPDFContextAccessPermissions
CG_AVAILABLE_STARTING(10.13, 11.0);
from the docs for CGPDFDocument
// To get access permissions from a CGPDFDocument, call CGPDFDocumentGetAccessPermissions. Setting permissions // can only be done using the kCGPDFContextAccessPermissions property in the auxiliary info dictionary passed // in to CGPDFContextCreate.
so, from what i gather, i should be able to OR properties together like so: (kCGPDFAllowsCommenting | kCGPDFAllowsLowQualityPrinting) in order to shove those into the PDF creation auxiliary dictionary to then be able to write a PDF with those permissions or not. Another problem is what if I don't want certain permissions turned on or off? the permissions are the following:
typedef CF_OPTIONS(uint32_t, CGPDFAccessPermissions) {
kCGPDFAllowsLowQualityPrinting = (1 << 0), // Print at up to 150 DPI
kCGPDFAllowsHighQualityPrinting = (1 << 1), // Print at any DPI
kCGPDFAllowsDocumentChanges = (1 << 2), // Modify the document contents except for page management
kCGPDFAllowsDocumentAssembly = (1 << 3), // Page management: insert, delete, and rotate pages
kCGPDFAllowsContentCopying = (1 << 4), // Extract content (text, images, etc.)
kCGPDFAllowsContentAccessibility = (1 << 5), // Extract content, but only for the purpose of accessibility
kCGPDFAllowsCommenting = (1 << 6), // Create or modify annotations, including form field entries
kCGPDFAllowsFormFieldEntry = (1 << 7) // Modify form field entries
};
I want the field kCGPDFAllowsDocumentChanges turned OFF so no changes to the PDF can be made. I've tried to do this through PDFKit writing to file with options, I've also tried with the above, and the same thing happens. I can write all other options contained in CGPDFContext.h, but I can't write the permissions for kCGPDFContextAccessPermissions without the system crashing.
Any help would be appreciated as answering this is a big deal since zero documentation or examples of code making this work for shutting off kCGPDFAllowsDocumentChanges exists at all.
You are trying to store a plain int
(uinit32_t
actually) in an NSDictionary
. You can't do that. You can only store objects in an NSDictionary
. So you need to wrap your value in an NSNumber
.
Using modern Objective-C, your code would be:
NSDictionary *tempDict = @{
(NSString *)kCGPDFContextOwnerPassword : @"user",
(NSString *)kCGPDFContextAccessPermissions : @(kCGPDFAllowsCommenting | kCGPDFAllowsLowQualityPrinting)
};