【问题标题】:Airprint a pdf page打印一个 pdf 页面
【发布时间】:2013-10-12 19:55:56
【问题描述】:

我需要使用 airprint 从 PDF 中打印几页,(并向它们添加重叠图像) 我知道如何打印整个 pdf,但我只需要打印用户选择的页面,

应该是这样的

[printPages:[NSArray arrayWithObjects:1,2,5,6,9,11]]

所以我可以用这些 PDF 页面打印唯一的文档

我需要在页面中添加一些叠加图像:D

你知道有没有可能?

提前谢谢你!

【问题讨论】:

    标签: ios objective-c pdf airprint


    【解决方案1】:

    我终于创建了这个函数

    -(void) splitPdf:(NSArray*) pages andNewFilePath:(NSString*)newFilePath andShouldRotate:(BOOL) shouldRotate andShouldPrintHoles:(BOOL)shouldPrintHoles{
    
        UIImage *holes;
    
        if(shouldRotate)    holes = [UIImage imageNamed:@"holes_rotated.png"];
        else                holes = [UIImage imageNamed:@"holes.png"];
    
    
        //create empty pdf file;
        UIGraphicsBeginPDFContextToFile(newFilePath, CGRectMake(0, 0, 792, 612), nil);
    
        CGPDFDocumentRef templateDocument = mPdf;
    
        //get amount of pages in template
        size_t count = CGPDFDocumentGetNumberOfPages(templateDocument);
    
        //for each page in template
        for(int i = 0; i< [pages count]; i = i+2){
            //get bounds of template page
    
            int pageNumber = [((NSNumber*)[pages objectAtIndex:i]) intValue];
            if (pageNumber <= count){
    
                CGPDFPageRef templatePage   = CGPDFDocumentGetPage(templateDocument,pageNumber);
                CGPDFPageRef templatePage2  = CGPDFDocumentGetPage(templateDocument,pageNumber+1);
    
                CGRect templatePageBounds = CGPDFPageGetBoxRect(templatePage, kCGPDFCropBox);
    
                //create empty page with corresponding bounds in new document
                //UIGraphicsBeginPDFPageWithInfo(templatePageBounds, nil);
                float margin = templatePageBounds.size.width*MARGIN_PROPORTION;
                CGRect newRect = CGRectMake(0, 0, templatePageBounds.size.width + margin, templatePageBounds.size.height*A4_PROPORTION + margin);
                UIGraphicsBeginPDFPageWithInfo(newRect, nil);
                CGContextRef context = UIGraphicsGetCurrentContext();
    
                CGContextSaveGState(context);
                if(shouldRotate){
                    CGContextTranslateCTM( context, 0.5f * templatePageBounds.size.width, 0.5f * templatePageBounds.size.height) ;
                    CGContextRotateCTM(context, radians(90));
                    CGContextTranslateCTM( context, -0.5f * templatePageBounds.size.width, -0.5f * templatePageBounds.size.height) ;
                }
    
                //flip context due to different origins
                if(!shouldRotate){
                    CGContextTranslateCTM(context,75, templatePageBounds.size.height + 130);
                    CGContextScaleCTM(context, 0.75, -0.75);
                }
                else{
                    CGContextTranslateCTM(context,-100, templatePageBounds.size.height - 70);
                    CGContextScaleCTM(context, 0.75, -0.75);
                }
    
                //Center the image with margins
                if(!shouldRotate)    CGContextTranslateCTM(context, margin*0.5f, -margin*A4_PROPORTION);
                else                 CGContextTranslateCTM(context, margin*A4_PROPORTION,margin*0.5f);
    
    
    
                CGContextDrawPDFPage(context, templatePage);  //copy content of template page on the corresponding page in new file
    
                if(!shouldRotate) CGContextTranslateCTM(context, 0,538);
                else              CGContextTranslateCTM(context, 538,0);
    
                CGContextDrawPDFPage(context, templatePage2);
    
    
                CGContextRestoreGState(context);              //Restore state
    
                /* Here you can do any drawings */
                if(shouldPrintHoles){
                    CGContextMoveToPoint(context, 0, 0);
                    if(!shouldRotate) {
                        [holes drawAtPoint:CGPointMake(0, ((templatePageBounds.size.height*A4_PROPORTION+margin) * 0.5f) - holes.size.height*0.5f)];
                    }
                    else{
                        [holes drawAtPoint:CGPointMake(((templatePageBounds.size.width*A4_PROPORTION+margin) * 0.5f) - holes.size.width*0.5f,0)];
                    }
    
                }
            }
            else{
                NSLog(@"Page to split is bigger than number of pages of the document");
            }
        }
    
        UIGraphicsEndPDFContext();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-23
      • 2017-04-11
      • 1970-01-01
      • 2017-12-26
      • 1970-01-01
      相关资源
      最近更新 更多