在受密码保护的PDF文件中插入图像目标C.

时间:2016-08-11 09:28:53

标签: ios objective-c iphone pdf pdf-generation

iOS中的PDF文件很容易插入图像。但有没有办法在受密码保护的PDF中插入图像。

1 个答案:

答案 0 :(得分:0)

后来,我工作并解决了它。

     NSString *pdfFilePath1 = [self getPDFFileName];
     pdfData = [NSData dataWithContentsOfFile:pdfFilePath1];
     NSMutableData* outputPDFData = [[NSMutableData alloc] init];

     CGDataConsumerRef dataConsumer  =CGDataConsumerCreateWithCFData((CFMutableDataRef)outputPDFData);

    CFMutableDictionaryRef attrDictionary = NULL;
    attrDictionary = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
    CFDictionarySetValue(attrDictionary, kCGPDFContextTitle, @"My Doc");
    ///Set password
    CFDictionarySetValue(attrDictionary, kCGPDFContextUserPassword, CFSTR("userpassword"));
    CFDictionarySetValue(attrDictionary, kCGPDFContextOwnerPassword, CFSTR("ownerpassword"));
    ///
    CGContextRef pdfContext = CGPDFContextCreate(dataConsumer, NULL, attrDictionary);
    CFRelease(dataConsumer);
    CFRelease(attrDictionary);
    CGRect pageRect;

    // Draw the old "pdfData" on pdfContext
    CFDataRef myPDFData = (__bridge CFDataRef) pdfData;
    CGDataProviderRef provider = CGDataProviderCreateWithCFData(myPDFData);
    CGPDFDocumentRef pdf = CGPDFDocumentCreateWithProvider(provider);
//Pass Password to PDF
    CGPDFDocumentUnlockWithPassword(pdf, "userpassword");
    CGDataProviderRelease(provider);


    size_t numPages = CGPDFDocumentGetNumberOfPages(pdf);
    if (numPages > 0) {
        // Loop through each page in the source file
        for (size_t i = 1; i <= numPages; i++) {
            CGPDFPageRef pdfPage = CGPDFDocumentGetPage(pdf, i);
            if (pdfPage) {
                // Get the page size
                CGRect pdfCropTempBoxRect = CGPDFPageGetBoxRect(pdfPage, kCGPDFMediaBox);
                //CGRect pdfCropBoxRect=CGRectMake(0, 0, _docPanel.frame.size.width, _docPanel.frame.size.height);

                if(i==pageNumber)
                {
                    int offset=1;
                    int offset_y=editView.frame.size.height;
                    if ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad )
                    {
                        offset=10;
                        offset_y=0;
                    }

                    // Copy the page from the source file to the context
                    CGContextBeginPage(pdfContext, &pdfCropTempBoxRect);
                    CGContextDrawPDFPage(pdfContext, pdfPage);

                    CGFloat contentHeight = self.docPanel.scrollView.contentSize.height;
                    self.pdfPageHeight = contentHeight / self.pdfPageCount;

                    // also calculate what half the screen height is. no sense in doing this multiple times.
                    self.halfScreenHeight = (self.docPanel.frame.size.height / 2);

                    float imgX=(editView.frame.origin.x/_docPanel.frame.size.width)*(pdfCropTempBoxRect.size.width);
                    //float imgY=(1-(verticalContentOffset-self.pdfPageHeight*(i-1)+editView.frame.origin.y+editView.frame.size.height)/self.pdfPageHeight)*pdfCropTempBoxRect.size.height-(i-1)*offset+offset_y;
                    float imgY=(1-(verticalContentOffset-self.pdfPageHeight*(i-1)+editView.frame.origin.y+editView.frame.size.height)/self.pdfPageHeight)*pdfCropTempBoxRect.size.height+offset_y;
                   // imgY = (pageNumber*self.pdfPageHeight)-imgY;
                    if ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone )
                    {
                        imgY = imgY - (editView.frame.size.height/2);
                    }
                    pageRect = CGRectMake(imgX,imgY,editView.frame.size.width-2 ,editView.frame.size.height-2);


                    CGImageRef pageImage = [img CGImage];
                    CGContextDrawImage(pdfContext, pageRect, pageImage);

                    //CGPDFContextEndPage(pdfContext);

                    CGPDFContextEndPage(pdfContext);
                }
                else
                {
                    CGContextBeginPage(pdfContext, &pdfCropTempBoxRect);
                    CGContextDrawPDFPage(pdfContext, pdfPage);
                    CGPDFContextEndPage(pdfContext);
                }
            }
        }
    }




    CGPDFContextClose(pdfContext);
    CGContextRelease(pdfContext);



    NSString *pdfFilePath = [self getPDFFileName];
    [outputPDFData writeToFile:pdfFilePath atomically:YES];