2016-09-22 2 views
0

Ich habe diesen Code, der meine UIView in PDF konvertiert. Eigentlich habe ich zwei String-Arrays, das erste Array enthält Fragezeichenfolgen und das zweite Array enthält Antwortstrings. Ich möchte die ganze Konversation drucken und dafür konvertiere ich meine UIView in PDF. Aber das Problem ist, dass ich die Größe von UIView in Übereinstimmung mit den Frage-Antwortstrings dynamisch vergrößere. Am Ende der Konversation wird die Ansichtshöhe sehr groß. Jetzt muss ich diese Ansicht in mehrere Ansichten aufteilen, um mehrere Seiten PDF zu drucken.IOS Objective C teilt eine große UIView in mehrere UIViews und konvertiert sie in PDF

Ich weiß bereits, wie mehrere Seiten PDF von mehreren UIViews drucken, aber was ich mit kämpfen muss, ist, wie man eine große Ansicht in mehrere kleine A4-Standardgrößenansichten aufteilt.

Ich speichere auch die erstellte PDF Datei in document directory, aber ich brauche nichts über diesen Teil wissen, deshalb zeige ich diesen Code in dieser Funktion nicht.

Jede Art von Hilfe würde sehr geschätzt werden. Vielen Dank.

Hier ist mein Code:

-(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 

} 

Der Code, den ich hier bin mit druckt mehrere Seiten derselben Ansicht wiederholt.

Antwort

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

Verwenden Sie die oben genannte Funktion und Logik, um Ihre Arbeit zu erledigen ...... –

Verwandte Themen