创建PDF

- (void) createPDF {
	NSMutableData *outputData = [[NSMutableData alloc] init];
    CGDataConsumerRef dataConsumer = CGDataConsumerCreateWithCFData((CFMutableDataRef)outputData);
    CFMutableDictionaryRef attrDictionary = NULL;    
    attrDictionary = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
    CFDictionarySetValue(attrDictionary, kCGPDFContextTitle, @"My Document");
    CGContextRef pdfContext = CGPDFContextCreate(dataConsumer, NULL, attrDictionary); 
    CFRelease(dataConsumer);
    CFRelease(attrDictionary);
	
    UIImage *myUIImage = [UIImage imageNamed:@"wheat.png"];
    CGImageRef pageImage = [myUIImage CGImage];
    CGPDFContextBeginPage(pdfContext, NULL);
    CGContextDrawImage(pdfContext, CGRectMake(0, 0, [myUIImage size].width, [myUIImage size].height), pageImage);
    
	CGContextSelectFont(pdfContext, "Helvetica", 35, kCGEncodingMacRoman);
    CGContextSetTextDrawingMode(pdfContext, kCGTextFill);
    CGContextSetRGBFillColor(pdfContext, 0, 0, 0, 1);
    const char *text = "Hello World!";
    CGContextShowTextAtPoint(pdfContext, 260, 390, text, strlen(text));
	
	CGPDFContextEndPage(pdfContext);
    CGPDFContextClose(pdfContext);
    CGContextRelease(pdfContext); 
	
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *appFile = [documentsDirectory stringByAppendingPathComponent:@"tmp.pdf"];
    [outputData writeToFile:appFile atomically:YES];
	[outputData release];
}
 

你可能感兴趣的:(ios,iPhone,pdf)