制作一些自定义方法并根据您的要求使用它们。下面是一些例子,
// Create a method to draw Line of any size anywhere in the pdf,
- (void) drawLine:(CGFloat)ofWidth fromPoint:(CGPoint)from toPoint(CGPoint)to withColor:(UIColor *)color {
//Get Current Graphics Context as a CGContextRef That Provides Graphic Environment for drawing in Quartz 2D
CGContextRef currentContext = UIGraphicsGetCurrentContext();
//Sets the width of line we going to draw.
CGContextSetLineWidth(currentContext, ofWidth);
//Sets the strok color of context
CGContextSetStrokeColorWithColor(currentContext, color.CGColor);
//Starting point of line as X,Y Value
CGPoint startPoint = from;
//Starting point of line as X,Y Value
CGPoint endPoint = to;
//Creates new empty path in given graphic context
CGContextBeginPath(currentContext);
//Begin a new subpath at given points
CGContextMoveToPoint(currentContext, startPoint.x, startPoint.y);
//Append line from the current points to the given points as parameter
CGContextAddLineToPoint(currentContext, endPoint.x, endPoint.y);
//Terminates the subpath of the context
CGContextClosePath(currentContext);
//Draw the corrunt path using drawing mode as paramete.
CGContextDrawPath(currentContext, kCGPathFillStroke);
}
这是将任何大小、任何颜色和任何字体的文本放在任何地方的方法,
- (void) drawText:(NSString *)text ofColor:(CGFloat)red Green:(CGFloat)green Blue:(CGFloat)blue withAlpha:(CGFloat)alpha withFont:(UIFont *)font atPoint:(CGRect)Point havingTextAlignment:(UITextAlignment)textAlignment{
//Get Current Graphics Context as a CGContextRef That Provides Graphic Environment for drawing in Quartz 2D
CGContextRef currentContext = UIGraphicsGetCurrentContext();
//Fill the current context with the color.
CGContextSetRGBFillColor(currentContext,red,green,blue,alpha);
//String to Draw
NSString *textToDraw = text;
[textToDraw drawInRect:Point
withFont:font
lineBreakMode:UILineBreakModeWordWrap
alignment:textAlignment];
}
图片也是如此,
- (void) drawImage:(UIImage *)image atFrame:(CGRect)frame {
[image drawInRect:frame];
}
现在您可以使用这些方法创建任何类型的 pdf,
- (void) generatePdfWithFilePath: (NSString *)thefilePath ofData:(NSDictionary *)data andPageSize:(CGSize) pageSize{
UIGraphicsBeginPDFContextToFile(thefilePath, CGRectZero, nil);
// For exampl
[self drawLine:65.0 fromPoint: CGPointMake( 10, 50) toPoint:CGPointMake(pageSize.width - 100) withColor:[UIColor colorWithRed:239.0/255.0 green:239.0/255.0 blue:239.0/255.0 alpha:1.0]];
[self drawText:@"Testing text" ofColor:0.0 Green:0.0 Blue:0.0 withAlpha:1.0 withFont:[UIFont systemFontOfSize:16.0] atPoint:CGRectMake(60,200, pageSize.width - 200,30.0) havingTextAlignment:UITextAlignmentLeft];
UIGraphicsEndPDFContext();
}
您还可以通过以下方式获得帮助,
http://www.raywenderlich.com/6581/how-to-create-a-pdf-with-quartz-2d-in-ios-5-tutorial-part-1
http://www.raywenderlich.com/6818/how-to-create-a-pdf-with-quartz-2d-in-ios-5-tutorial-part-2