Errors on creating a multipage PDF

后端 未结 4 874
时光取名叫无心
时光取名叫无心 2020-12-09 14:39

Has anyone already created a PDF document in an iPad app? I see that there are new functions in the UIKit to do this, but I can\'t find any code example for it.

<         


        
4条回答
  •  猫巷女王i
    2020-12-09 15:01

    Here is a method that will create a new PDF document. It does not do text formatting though.

        - (void) createNewPDF: (NSString *) saveFileName withContent: (NSString *) content forSize: (int) fontSize andFont:(NSString *) font andColor: (UIColor *) color
    {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *newFilePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:saveFileName];
    
        CGRect a4Page = CGRectMake(0, 0, 595, 842);
    
        NSDictionary *fileMetaData = [[NSDictionary alloc] init];
    
        if (!UIGraphicsBeginPDFContextToFile(newFilePath, a4Page, fileMetaData )) {
            NSLog(@"error creating PDF context");
            return;
        }
    
        CGContextRef mpdfContext = UIGraphicsGetCurrentContext();
        UIGraphicsBeginPDFPage();
        CGContextSetTextMatrix(mpdfContext, CGAffineTransformMake(1, 0, 0, -1, 0, 0));
        CGContextSetTextDrawingMode (mpdfContext, kCGTextFill);
        CGContextSelectFont (mpdfContext, [font cStringUsingEncoding:NSUTF8StringEncoding], fontSize, kCGEncodingMacRoman);                                                 
        CGContextSetFillColorWithColor(mpdfContext, [color CGColor]);
        CGContextShowTextAtPoint (mpdfContext, 20, 20, [content cStringUsingEncoding:NSUTF8StringEncoding], [content length]);
        UIGraphicsEndPDFContext();
    
    }
    

提交回复
热议问题