Protect PDF content
To protect PDF content, you can specify some security options in the secondary dictionary and pass them to cgw.contextcreate. You can set whether the owner password, user password, and PDF file can be printed or copied by using the following keywords:
· Kcgw.contextownerpassword: defines the owner password of the PDF document. If this value is specified, the document is encrypted using the owner password; otherwise, the document is not encrypted. The value of this keyword must be an ASCII CFString object. Only the first 32 bits are used for passwords. This value has no default value. If the value cannot be expressed as ASCII, the document cannot be created and NULL is returned. Quartz uses 40-bit encryption.
· Kcg1_contextuserpassword: defines the user password of the PDF document. If the document is encrypted, the value is the document's user password. If not specified, the user password is blank. The value of this keyword must be an ASCII CFString object. Only the first 32 bits are used for passwords. If the value cannot be expressed as ASCII, the document cannot be created and NULL is returned.
· Kcg?contextallowsprinting: Specifies whether the document can be printed when the user password lock is used. The value must be a CFBoolean object. The default value is kCGBooleanTrue.
· Kcg?contextallowscopying: Specifies whether the document can be copied when the user password lock is used. The value must be a CFBoolean object. The default value is kCGBooleanTrue.
Code list 14-4 (next chapter) shows how to check whether the PDF document is locked and use a password to open the document.
Set password when creating a PDF file
It is easy to use Quartz to create a PDF file and draw other image contexts. We specify a PDF file address, set a PDF image context, and use the same drawing program as other graphics contexts. The MyCreatePDFFile function shown in code listing 13-4 shows all the work of creating a PDF.
Note that the code is used to draw a PDF in cgw.contextbeginpage and cgw.contextendpage. We can pass a CFDictionary object to specify page attributes, including media, crop, bleed, trim, and art boxes.
Void MyCreatePDFFile (CGRectpageRect, const char * filename)
{
CGContextRef implements context;
CFStringRef path;
CFURLRef url;
CFData boxData = NULL;
CFMutableDictionaryRef myDictionary = NULL;
CFMutableDictionaryRef pageDictionary = NULL;
Path = CFStringCreateWithCString (NULL, filename, kCFStringEncodingUTF8 );
Url = CFURLCreateWithFileSystemPath (NULL, path, kCFURLPOSIXPathStyle, 0 );
CFRelease (path );
MyDictionary = CFDictionaryCreateMutable (NULL, 0, & kCFTypeDictionaryKeyCallBacks, & kCFTypeDictionaryValueCallBacks );
CFDictionarySetValue (myDictionary, kcg1_contexttitle, CFSTR ("MyPDF File "));
CFDictionarySetValue (myDictionary, kcg1_contextcreator, CFSTR ("MyName "));
CFDictionarySetValue (myDictionary, kcgw.contextownerpassword, CFSTR ("zhoumin "));
CFDictionarySetValue (myDictionary, kcg1_contextuserpassword, CFSTR ("zhoumin "));
---------------------------------
// Add zhoumin
// Kcg1_contextownerpassword and kcg1_contextuserpassword are encrypted in pdf.
---------------------------------
Encryption context = cgw.contextcreatewithurl (url, & pageRect, myDictionary );
CFRelease (myDictionary );
CFRelease (url );
PageDictionary = CFDictionaryCreateMutable (NULL, 0, & kCFTypeDictionaryKeyCallBacks, & kCFTypeDictionaryValueCallBacks );
BoxData = CFDataCreate (NULL, (const UInt8 *) & pageRect, sizeof (CGRect ));
CFDictionarySetValue (pageDictionary, kcg1_contextmediabox, boxData );
Cgw.contextbeginpage (encryption context, & pageRect );
MyDrawContent (encryption context );
Cgw.contextendpage (encryption context );
CGContextRelease (encryption context );
CFRelease (pageDictionary );
CFRelease (boxData );
}
PDF decryption
------------------------------------------------------------------
Cgw.documentref pdf;
Pdf = cgw.documentcreatewithurl (Invalid URL );
If (pdf = NULL ){
NSLog (@ "can't open '% @'", signed URL );
CFRelease (publish URL );
}
If (cgw.documentisencrypted (pdf) {// determine whether the pdf is encrypted www.2cto.com
If (! Cgw.documentunlockwithpassword (pdf, "") {// determine whether the password is ""
NSString * password = @ "zhoumin ";
If (password! = NULL ){
If (! Cgw.documentunlockwithpassword (pdf, [password UTF8String])
// Use password to decrypt the pdf. If the password is valid, yes is returned.
NSLog (@ "invalid password .");
}
}
}
If (! Cgw.documentisunlocked (pdf) {// determines whether the pdf is enabled.
NSLog (@ "cgw.documentisunlocked ");
}
If (cgw.documentgetnumberofpages (pdf) = 0 ){
NSLog (@ "cg1_documentgetnumberofpages = 0 ");
}
------------------------------------------------------------------
Official Apple documentation
Http://developer.apple.com/library/ios/#documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_pdf_scan/dq_pdf_scan.html#//apple_ref/doc/uid/TP30001066-CH220-CJBDCGCB
Author: z251257144