2016-09-22 2 views
0

UIViewPDF으로 변환하는 코드가 있습니다. 실제로 내가 뭘하는지 두 문자열 배열, 첫 번째 배열을 질문 문자열을 보유하고 두 번째 배열 응답 문자열을 보유하고있다. 전체 대화를 인쇄하고 싶습니다. 따라서 UIViewPDF으로 변환 중입니다. 그러나 문제는 질문 답변 문자열에 따라 UIView의 크기를 동적으로 늘리는 것입니다. 따라서 대화가 끝나면보기 높이가 매우 커집니다. 이제 다중보기 PDF을 인쇄하려면 여러보기로보기를 분할해야합니다.IOS 목표 C는 여러 UIViews에 큰 UIView를 분할하고 PDF로 변환

이미 복수 페이지 을 여러 개의 UIViews에서 인쇄하는 방법을 알고 있지만 여러 개의 작은 A4 표준 크기보기로 큰보기를 분할하는 방법에 대해 고심하고 있습니다.

나는 또한 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

위의 기능과 논리를 사용하여 작업을 완료하십시오 ...... –