更新了我可以在 iPHone 中创建带有评论的照片的单个 PDF 页面。单击按钮时,我每次都生成一个 PDF 页面,并且我希望将这些 PDF 页面放在单个 PDF 束中。我无法将单个 PDF 文件合并成一堆。
http://mobile.tutsplus.com/tutorials/iphone/generating-pdf-documents/?search_index=3
我遵循了上面的 URL 代码。你能在这里提出一些逻辑吗?提前致谢。
*在代码中编辑* 你可以检查下面的代码。
- (IBAction)didClickOpenPDF {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pdfPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.pdf",myPDFName]];
if([[NSFileManager defaultManager] fileExistsAtPath:pdfPath]) {
ReaderDocument *document = [ReaderDocument withDocumentFilePath:pdfPath password:nil];
if (document != nil)
{
ReaderViewController *readerViewController = [[ReaderViewController alloc] initWithReaderDocument:document];
readerViewController.delegate = self;
readerViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
readerViewController.modalPresentationStyle = UIModalPresentationFullScreen;
[self presentModalViewController:readerViewController animated:YES];
}
}
}
- (IBAction)didClickMakePDF {
[self setupPDFDocumentNamed:[NSString stringWithFormat:@"%@",myPDFName] Width:850 Height:1100];
[self beginPDFPage];
CGRect textRect = [self addText:question.text
withFrame:CGRectMake(kPadding, kPadding, 400, 200) fontSize:48.0f];
// dynamic image captured by camera,comment text, lines are added here
[self finishPDF];
}
- (void)setupPDFDocumentNamed:(NSString*)name Width:(float)width Height:(float)height {
_pageSize = CGSizeMake(width, height);
NSString *myPDFName = [NSString stringWithFormat:@"%@.pdf", name];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pdfPath = [documentsDirectory stringByAppendingPathComponent:myPDFName];
UIGraphicsBeginPDFContextToFile(pdfPath, CGRectZero, nil);
int count = [gotIndexString integerValue];
for (int pageNumber = 2; pageNumber <= count; pageNumber++)
{
//Open a pdf page context
UIGraphicsBeginPDFPageWithInfo(CGRectZero, nil);
//Get graphics context to draw the page
CGContextRef currentContext = UIGraphicsGetCurrentContext();
//Flip and scale context to draw the pdf correctly
CGContextTranslateCTM(currentContext, 0, CGRectZero.size.height);
CGContextScaleCTM(currentContext, 1.0, -1.0);
NSURL *newUrl = [NSURL URLWithString:pdfPath];
NSLog(@" setupPDFDocumentNamed newUrl for loop %@ ",newUrl);
//Get document access of the pdf from which you want a page
CGPDFDocumentRef newDocument = CGPDFDocumentCreateWithURL ((__bridge_retained CFURLRef) newUrl);
NSLog(@" setupPDFDocumentNamed newDocument for loop %@ ",newDocument);
//Get the page you want
CGPDFPageRef newPage = CGPDFDocumentGetPage (newDocument, 1);
NSLog(@" setupPDFDocumentNamed newPage for loop %@ ",newPage);
//Drawing the page
CGContextDrawPDFPage (currentContext, newPage);
NSLog(@"CGContextRef context %@ ",currentContext);
//Clean up
newPage = nil;
CGPDFDocumentRelease(newDocument);
newDocument = nil;
newUrl = nil;
}
}
- (void)beginPDFPage {
UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, _pageSize.width, _pageSize.height), nil);
}
- (void)finishPDF {
UIGraphicsEndPDFContext();
}