【问题标题】:Creating Password Protected PDF in Objective-C在 Objective-C 中创建受密码保护的 PDF
【发布时间】:2012-03-07 08:25:20
【问题描述】:

我尝试使用此处提供的信息

https://developer.apple.com/library/mac/#documentation/graphicsimaging/conceptual/drawingwithquartz2d/dq_pdf/dq_pdf.html#//apple_ref/doc/uid/TP30001066-CH214-TPXREF101

但是真的很模糊……

我也尝试使用 UIKit 使用此代码...文件已创建..但它是 0 kb..无法写入任何数据..

BOOL allowCopy=YES;
BOOL allowPrint=YES;
//NSString *password=@"test";

CGContextRef pdfContext; //our pdfContext

CFStringRef path;

CFURLRef url;

CGRect pageRect = CGRectMake(0, 0,500, 500);

NSString *fileName=@"Test.pdf";
// CFStringRef passwordString = (__bridge_retained CFStringRef)password;

const char *filename = [fileName UTF8String];

// Create a CFString from the filename we provide to this method when we call it
path = CFStringCreateWithCString (NULL, filename, kCFStringEncodingUTF8);

// Create a CFURL using the CFString we just defined
url = CFURLCreateWithFileSystemPath (NULL, path, kCFURLPOSIXPathStyle, 0);

CFMutableDictionaryRef myDictionary = NULL;
// This dictionary contains extra options mostly for 'signing' the PDF

myDictionary = CFDictionaryCreateMutable(NULL, 0,
                                         &kCFTypeDictionaryKeyCallBacks,
                                         &kCFTypeDictionaryValueCallBacks);

CFDictionarySetValue(myDictionary, kCGPDFContextOwnerPassword, CFSTR("user"));
CFDictionarySetValue(myDictionary, kCGPDFContextUserPassword, CFSTR("user"));

if (!allowCopy) 
    CFDictionarySetValue(myDictionary, kCGPDFContextAllowsCopying, kCFBooleanFalse);     
//kCGPDFContextAllowsCopying is set to TRUE by default

if (!allowPrint) 
    CFDictionarySetValue(myDictionary, kCGPDFContextAllowsPrinting, kCFBooleanFalse);    
//kCGPDFContextAllowsPrinting is set to TRUE by default

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:fileName];

if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
    [[NSFileManager defaultManager] createFileAtPath:dataPath contents:nil attributes:nil];
// CGRect papersize= CGRectMake(0, 0, 400, 400);

pdfContext = CGPDFContextCreateWithURL (url, &pageRect, myDictionary);

// Cleanup our mess
CFRelease(myDictionary);

CFRelease(url);

//Now, this is a tricky part. We make use of a do - while loop in order to create as many pages as needed

CGContextBeginPage (pdfContext, &pageRect); //begins a new PDF page

//create layout for our page

CGRect bounds = CGRectMake(0, 0,500,500);

UIGraphicsPushContext(pdfContext); //pushing the context, as explained at the beginning of this post

CGContextSaveGState(pdfContext);

CGContextTranslateCTM(pdfContext, 0, bounds.origin.y);

CGContextScaleCTM(pdfContext, 1, -1);

CGContextTranslateCTM(pdfContext, 0, -(bounds.origin.y + bounds.size.height));

//[@"HELLO" drawInRect:CGRectMake(10,15,10,30) withFont:[UIFont fontWithName:@"Helvetica-Bold" size:40]];

//THIS IS THE NASTY PART
CGContextRestoreGState(pdfContext);

UIGraphicsPopContext();

CGContextEndPage (pdfContext); //ends the current page

// We are done with our context now, so we release it

CGContextRelease (pdfContext);

CFRelease(path);

// Create our PDF Context with the CFURL, the CGRect we provide, and the above defined dictionary

如果有人可以帮忙..谢谢..

【问题讨论】:

    标签: objective-c pdf passwords xcode4.2


    【解决方案1】:

    修改清单 13-4 并添加以下位于 cmets 之间的行:

    CFDictionarySetValue(myDictionary, kCGPDFContextTitle, CFSTR("My PDF File"));
    CFDictionarySetValue(myDictionary, kCGPDFContextCreator, CFSTR("My Name"));
    ///
    CFDictionarySetValue(myDictionary, kCGPDFContextUserPassword, CFSTR("userpassword"));
    CFDictionarySetValue(myDictionary, kCGPDFContextOwnerPassword, CFSTR("ownerpassword"));
    ///
    pdfContext = CGPDFContextCreateWithURL (url, &pageRect, myDictionary); 
    

    文章末尾列出了支持的安全选项。

    【讨论】:

      【解决方案2】:
      CFMutableDataRef theData = 0;
      theData =CFDataCreateMutable(NULL, 0);
      
      
      const CGRect mediaRect = CGRectMake(0.0f, 0.0f, 100, 100);
      
      
      CGContextRef cgContext = 0;
      CGDataConsumerRef theConsumer =CGDataConsumerCreateWithCFData(theData);
      if(theConsumer != 0)
      {
          CFMutableDictionaryRef auxiliaryInfo = CFDictionaryCreateMutable(NULL,0,NULL,NULL);
          CFDictionarySetValue(auxiliaryInfo, kCGPDFContextUserPassword, CFSTR("password"));
          CFDictionarySetValue(auxiliaryInfo, kCGPDFContextOwnerPassword, CFSTR("password1"));
          cgContext = CGPDFContextCreate(theConsumer, &mediaRect, (CFDictionaryRef)auxiliaryInfo);
          CGDataConsumerRelease(theConsumer);
          CFRelease(auxiliaryInfo);
      }
      
      
      if(cgContext != 0)
      {
          CGContextBeginPage(cgContext, &mediaRect);
          CGContextSaveGState(cgContext);
      
      
          CGContextEndPage(cgContext);
          CGContextRestoreGState(cgContext);
          CGContextFlush(cgContext);
      
          CGContextRelease(cgContext);
      }
      
      NSString* outputPath = [@"path/test" stringByAppendingPathExtension:@"pdf"];
      
      [(NSData*)theData writeToFile:outputPath atomically:YES];
      

      【讨论】:

        猜你喜欢
        • 2010-09-27
        • 2011-02-02
        • 2011-12-23
        • 2012-09-09
        • 1970-01-01
        • 1970-01-01
        • 2010-10-18
        • 2010-11-09
        相关资源
        最近更新 更多