2012-02-15 38 views
0

这里我需要通过一个页面中的一些动态内容生成PDF文件,下一页包含图像,但我的代码生成为一个页面,你可以帮助我用iPhone中的静态内容和图像生成PDF

此画文本方法用于产生所述动态内容

- (void) drawText 
    { 
     CGContextRef currentContext = UIGraphicsGetCurrentContext(); 
     CGContextSetRGBFillColor(currentContext, 0.0, 0.0, 0.0, 1.0); 

     NSString *textToDraw=[[NSString alloc] initWithString:@""]; 
     for (int i=0; i<[fieldsArr count]; i++) 
     { 
      textToDraw=[textToDraw stringByAppendingFormat:[NSString stringWithFormat:@"%@\t%@\n",[fieldsArr objectAtIndex:i],[valuesArr objectAtIndex:i]]]; 
     } 


     textToDraw=[textToDraw stringByAppendingFormat:@"\n\n\n\n\n\n"]; 
     UIFont *font = [UIFont systemFontOfSize:14.0]; 

     CGSize stringSize = [textToDraw sizeWithFont:font 
            constrainedToSize:CGSizeMake(pageSize.width - 2*kBorderInset-2*kMarginInset, pageSize.height - 2*kBorderInset - 2*kMarginInset) 
             lineBreakMode:UILineBreakModeWordWrap]; 

     CGRect renderingRect = CGRectMake(kBorderInset + kMarginInset, kBorderInset + kMarginInset + 50.0, pageSize.width - 2*kBorderInset - 2*kMarginInset, stringSize.height); 

     [textToDraw drawInRect:renderingRect 
         withFont:font 
       lineBreakMode:UILineBreakModeWordWrap 
        alignment:UITextAlignmentLeft]; 
    } 

此方法被用于绘制的图像

- (void) drawImage 
    { 
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
     NSString *documentsDirectory = [paths objectAtIndex:0];  
     NSString *getImagePath = [documentsDirectory stringByAppendingPathComponent:@"displayImage.jpg"]; 
     UIImage * demoImage = [UIImage imageWithContentsOfFile:getImagePath]; 
     [demoImage drawInRect:CGRectMake((pageSize.width - demoImage.size.width/2)/2, 500, demoImage.size.width/2, demoImage.size.height/2)]; 
    } 

此方法是用于基因评级PDF文件

- (void) generatePdfWithFilePath: (NSString *)thefilePath 
    { 
     UIGraphicsBeginPDFContextToFile(thefilePath, CGRectZero, nil); 

     NSInteger currentPage = 0; 
     BOOL done = NO; 
     do 
     { 
      //Start a new page. 
      UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, pageSize.width, pageSize.height), nil); 

      //Draw a page number at the bottom of each page. 

      //Draw some text for the page. 
      [self drawText]; 

      //Draw an image 
      [self drawImage]; 
      done = YES; 
     } 
     while (!done); 

     // Close the PDF context and write the contents out. 
     UIGraphicsEndPDFContext(); 
    } 

请帮我在

回答

0

不同的页面文字和图像分开检查的内容你正在写的类型,如果它属于的ImageView然后开始一个新的PDF页面。

0

当你想画image.Here是更改代码,添加一个新的页面,在此我添加了新的一页绘制图像之前

- (void) generatePdfWithFilePath: (NSString *)thefilePath 
    { 
     UIGraphicsBeginPDFContextToFile(thefilePath, CGRectZero, nil); 

     NSInteger currentPage = 0; 
     BOOL done = NO; 
     do 
     { 
      //Start a new page. 
      UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, pageSize.width, pageSize.height), nil); 

      //Draw a page number at the bottom of each page. 

      //Draw some text for the page. 
      [self drawText]; 

      //Again start a new page to Draw an image 

      UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, pageSize.width, pageSize.height), nil); 


      [self drawImage]; 
      done = YES; 
     } 
     while (!done); 

     // Close the PDF context and write the contents out. 
     UIGraphicsEndPDFContext(); 
    }