1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| -(void)creatPDF{ CGContextRef pdfContext; CFStringRef path; CFURLRef url; CFDataRef boxData = NULL; CFMutableDictionaryRef myDictionary = NULL; CFMutableDictionaryRef pageDictionary = NULL; NSString * filePath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, true).firstObject; NSLog(@"%@",filePath); const char * filename = [[NSString stringWithFormat:@"%@/MyText",filePath] cStringUsingEncoding:kCFStringEncodingUTF8]; path = CFStringCreateWithCString (NULL, filename,kCFStringEncodingUTF8); url = CFURLCreateWithFileSystemPath (NULL, path,kCFURLPOSIXPathStyle, 0); CFRelease (path); myDictionary = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); CFDictionarySetValue(myDictionary, kCGPDFContextTitle, CFSTR("My PDF File")); CFDictionarySetValue(myDictionary, kCGPDFContextCreator, CFSTR("My Name")); CGRect pageRect = CGRectMake(0, 0, 200, 200); pdfContext = CGPDFContextCreateWithURL (url, &pageRect, myDictionary); CFRelease(myDictionary); CFRelease(url); pageDictionary = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); boxData = CFDataCreate(NULL,(const UInt8 *)&pageRect, sizeof (CGRect)); CFDictionarySetValue(pageDictionary, kCGPDFContextMediaBox, boxData); CGPDFContextBeginPage (pdfContext, pageDictionary); CGFloat colors[4] = {1,0,0,1}; CGContextSetFillColorSpace(pdfContext, CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB)); CGContextSetFillColor(pdfContext, colors); CGContextFillRect(pdfContext, CGRectMake(0, 0, 100, 100)); CGPDFContextEndPage (pdfContext); CGPDFContextBeginPage (pdfContext, pageDictionary); CGContextSetFillColorSpace(pdfContext, CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB)); CGContextSetFillColor(pdfContext, colors); CGContextFillRect(pdfContext, CGRectMake(0, 0, 100, 100)); CGPDFContextEndPage (pdfContext); CGContextRelease (pdfContext); CFRelease(pageDictionary); CFRelease(boxData);
}
|