您可以使用 CoreGraphics 轻松创建/编辑 pdf。只需创建一个定义大小的矩形,然后创建上下文,推送上下文,调用 CFPDFContextBeginPage 并像往常一样开始绘图...这是一个示例:
CGRect mediaBox = CGRectMake(0, 0, 550, 800);
NSString *url = [path stringByExpandingTildeInPath];
CGContextRef ctx = CGPDFContextCreateWithURL((CFURLRef)[NSURL fileURLWithPath:url], &mediaBox, NULL);
UIGraphicsPushContext(ctx);
//Page1
CGPDFContextBeginPage(ctx, NULL);
CGContextScaleCTM(ctx, 1, -1);
CGContextTranslateCTM(ctx, 0, -mediaBox.size.height);
//name and value are NSStrings
[name drawInRect:rectName withFont:fontName];
[value drawInRect:rectValue withFont:fontValue];
[[UIImage imageNamed:@"logo.png"] drawInRect:CGRectMake(8, 15, 91, 99)];
CGPDFContextEndPage(ctx);
UIGraphicsPopContext();
CFRelease(ctx);
更新
你可以复制你需要的页面,然后像这样在它们上面画图:
CGPDFDocumentRef originalDoc = NULL;
CGContextRef pdfContext = NULL;
CGRect docRect = CGRectZero;
NSURL *originalURL = [[NSBundle mainBundle] URLForResource:@"test" withExtension:@"pdf"];
NSString *newPath = @"/Users/***/Desktop/new.pdf";
CFURLRef newURL = CFURLCreateWithFileSystemPath (NULL,
(CFStringRef)newPath,
kCFURLPOSIXPathStyle,
false);
//Original doc and it's dimesnions
originalDoc = CGPDFDocumentCreateWithURL((CFURLRef)originalURL);
CGPDFPageRef firstPage = CGPDFDocumentGetPage(originalDoc, 1);
if (firstPage == NULL) {
NSLog(@"This document has no pages..Exiting.");
}
docRect = CGPDFPageGetBoxRect(firstPage, kCGPDFCropBox);
NSLog(@"%@", NSStringFromRect(docRect));
//New doc context
if (newURL != NULL) {
pdfContext = CGPDFContextCreateWithURL(newURL, &docRect, NULL);
if (pdfContext == NULL) {
NSLog(@"Error creating context");
}
CFRelease(newURL);
} else {
NSLog(@"Error creating url");
}
然后分别复制每一页。在这个特定的示例中,我将“Bates”(编号)添加到页面中。
//Copy original to new, and write bates
size_t count = CGPDFDocumentGetNumberOfPages(originalDoc);
for (size_t pageNumber = 1; pageNumber <= count; pageNumber++) {
CGPDFPageRef originalPage = CGPDFDocumentGetPage(originalDoc, pageNumber);
CGContextBeginPage (pdfContext,nil);
CGContextSetRGBFillColor(pdfContext, 0, 0, 255, 0.1);
CGContextSetRGBStrokeColor(pdfContext, 0, 0, 255, 0.5);
// Draw a circle (filled)
CGContextFillEllipseInRect(pdfContext, CGRectMake(0, 0, 25, 25));
CGContextSaveGState(pdfContext);
//flip context due to different origins
CGContextTranslateCTM(pdfContext, 0.0, (docRect.size.height - (docRect.size.height * 0.80))/2);
CGContextScaleCTM(pdfContext, 1.0, 0.8);
//copy content of template page on the corresponding page in new file
CGContextDrawPDFPage(pdfContext, originalPage);
CGContextRestoreGState(pdfContext);
//flip context back
//CGContextTranslateCTM(pdfContext, 0.0, -(docRect.size.height - (docRect.size.height * 0.80))/2);
//CGContextScaleCTM(pdfContext, 1.0, 1.25);
CGContextSetRGBFillColor(pdfContext, 0, 0, 255, 0.1);
CGContextSetRGBStrokeColor(pdfContext, 0, 0, 255, 0.5);
// Draw a circle (filled)
CGContextFillEllipseInRect(pdfContext, CGRectMake(0, 0, 25, 25));
NSString *bate = [self generateStringForCount:(int)pageNumber-1];
int fontSize = 15;
CGContextSelectFont(pdfContext, "Helvetica", fontSize, kCGEncodingMacRoman);
CGContextSetTextDrawingMode(pdfContext, kCGTextFill);
CGContextSetTextPosition(pdfContext, 0.0f, round(fontSize / 4.0f));
CGContextShowText(pdfContext, [bate UTF8String], strlen([bate UTF8String]));
CGContextEndPage(pdfContext);
}
CFRelease(pdfContext);