2016-09-22 13 views
0

我有這段代碼將我的UIView轉換爲PDF。 其實我所做的是我有兩個字符串數組,第一個數組保存問題字符串,第二個數組保存答案字符串。我想打印整個對話,爲此我將UIView轉換爲PDF。但問題是我根據問題答案字符串動態地增加了UIView的大小。所以,在對話結束時,視圖高度變得非常大。現在我需要將該視圖分成多個視圖來打印多頁PDFiOS Objective C將一個大的UIView分割爲多個UIView並將它們轉換爲PDF

我已經知道如何從多個UIViews打印多個頁面PDF,但我正在努力的是如何將大視圖分割爲多個小A4標準尺寸的視圖。

我也將創建的PDF文件保存到document directory中,但我不需要知道任何關於該部分的內容,這就是爲什麼我不在該函數中顯示該代碼的原因。

任何形式的幫助將不勝感激。謝謝。

這裏是我的代碼:

-(void)createPDFfromUIView:(UIView*)aView saveToDocumentsWithFileName:(NSString*)aFilename { 

NSMutableData* pdfData = [NSMutableData data]; 

UIGraphicsBeginPDFContextToData(pdfData, CGRectMake(0.0f, 0.0f, 612.0f, 792.0f), nil); 
int numberOfPages = ceil(aView.frame.size.height/792.0f); 

for (int i = 0 ; i < numberOfPages ; i++) 
{ 
    UIGraphicsBeginPDFPage(); 

    CGContextRef pdfContext = UIGraphicsGetCurrentContext(); 
    [aView.layer renderInContext:pdfContext]; 
} 
UIGraphicsEndPDFContext(); 

[self printItem:pdfData]; // I am using this function to print the pdf file 

} 

我在這裏使用的代碼重複印刷相同觀點的多個頁面。

回答

0
-(void) createPDF 
{ 

    NSString *pdfFilePath = [NSString stringWithFormat:@"%s",filePath]; 
    CGPDFDocumentRef document = CGPDFDocumentCreateWithURL((__bridge CFURLRef)[NSURL fileURLWithPath:pdfFilePath]); 
    NSUInteger totalPages = CGPDFDocumentGetNumberOfPages(document); 


    UIGraphicsBeginPDFContextToFile(pdfFilePath, CGRectZero, nil); 

     for (int i = 1; i <= totalPages; i++) 
     { 
      CGPDFPageRef pageRef = CGPDFDocumentGetPage(document, i); 

      CGRect cropBoxRect = CGPDFPageGetBoxRect(pageRef, kCGPDFCropBox); 
      CGRect mediaBoxRect = CGPDFPageGetBoxRect(pageRef, kCGPDFMediaBox); 
      CGRect effectiveRect = CGRectIntersection(cropBoxRect, mediaBoxRect); 

      NSUInteger rotationAngle = 0;//; 
      int pageAngle = (CGPDFPageGetRotationAngle(pageRef) + rotationAngle) % 360; 

      NSInteger pageWidth = 0; 
      NSInteger pageHeight = 0; 

      switch (pageAngle) // Page rotation angle (in degrees) 
      { 
       default: // Default case 
       case 0: case 180: // 0 and 180 degrees 
       { 
        pageWidth = effectiveRect.size.width; 
        pageHeight = effectiveRect.size.height; 
        break; 
       } 

       case 90: case 270: // 90 and 270 degrees 
       { 
        pageWidth = effectiveRect.size.height; 
        pageHeight = effectiveRect.size.width; 
        break; 
       } 
      } 

      if (pageWidth % 2) pageWidth--; 
      if (pageHeight % 2) pageHeight--; 

      CGRect pageFrame = CGRectZero; 
      pageFrame.size = CGSizeMake(pageWidth, pageHeight); 

      UIGraphicsBeginPDFPageWithInfo(pageFrame, nil); 
      CGContextRef context = UIGraphicsGetCurrentContext(); 
      CGContextSaveGState(context); 


      CGContextScaleCTM(context, 1.0f, -1.0f); 
      CGContextTranslateCTM(context, 0.0f, -pageFrame.size.height); 
      CGContextConcatCTM(context, CGPDFPageGetDrawingTransform(pageRef, kCGPDFCropBox, pageFrame, (int)rotationAngle, true)); 
      CGContextSetInterpolationQuality(context, kCGInterpolationHigh); 
      CGContextSetRenderingIntent(context, kCGRenderingIntentDefault); 
      CGContextDrawPDFPage(context, pageRef); 
      CGContextRestoreGState(context); 
      CGContextTranslateCTM(context, pageWidth/2, pageHeight/2); 
      CGContextRotateCTM(context, degreesToRadians(rotationAngle)); 
      CGFloat aspectRatio = (CGFloat)pageWidth/pageHeight; 

      if (rotationAngle == 90 || rotationAngle == 270) 
      { 
       CGContextTranslateCTM(context, - pageHeight/2, - pageWidth/2); 
       CGContextScaleCTM(context, 1/aspectRatio, aspectRatio); 
      } 
      else 
      { 
       CGContextTranslateCTM(context, - pageWidth/2, - pageHeight/2); 
      } 

      UIImage *pageViewImage = [self getPageImageAtIndex:i]; 
      [pageViewImage drawInRect:pageFrame]; 

     } 

    UIGraphicsEndPDFContext(); 
    CGPDFDocumentRelease(document); 
} 


-(UIImage *) getPageImageAtIndex:(int) index 
{ 

// Design page view and take snapshot of view with fixed number of questions and answers on view and return the snapshot image from here. 

int startIndex = 0; 
int endIndex = 0; 

if(i==1) 
{ 
    startIndex = 0; // start from 0 and draw upto 10 
    endIndex = 10; 
} 
else if(i==2) 
{ 
    startIndex = 11; // start from 11 and draw upto 20 
    endIndex = 20; 
} 
else if(i==3) 
{ 
    startIndex = 21; // start from 21 and draw upto 30 
    endIndex = 30; 
} 
. 
. 
. 
. 

for(int i= startIndex; i<= endIndex;i++) 
{ 

// Drawing goes here 

} 

} 
+0

使用上述功能和邏輯來完成您的工作...... –

相關問題