在我的应用程序中,我有一个有数百页的PDF。
我希望用户能够创建一个新的,单页PDF和电子邮件。
该单页PDF将是任何给定页面的大PDF在应用程序。
我怎样才能做到这一点?
发送电子邮件很简单:
- (void)email:(UIButton *)sender
{
NSString *emailSubject = @"subject";
NSString *messageBody = [NSString stringWithFormat:@"%ld", sender.tag];
MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
mc.mailComposeDelegate = self;
[mc setSubject:emailSubject];
[mc setMessageBody:messageBody isHTML:NO];
// [mc addAttachmentData:myData mimeType:@"application/pdf" fileName:@"thePDF.pdf"];
[self presentViewController:mc animated:YES completion:NULL];
}
我只是不知道如何创建“myData”,这将是单页PDF
发布于 2014-07-19 17:10:39
下面是我解决这个问题的方法:
- (void)emailAction:(UIButton *)sender
{
NSString *emailSubject = @"Subject";
NSString *messageBody = @"message";
MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
mc.mailComposeDelegate = self;
[mc setSubject:emailSubject];
[mc setMessageBody:messageBody isHTML:NO];
[mc addAttachmentData:[self getPage:sender.tag+1] mimeType:@"application/pdf" fileName:@"thePDF.pdf"];
[self presentViewController:mc animated:YES completion:NULL];
}
- (NSMutableData *)getPage:(NSInteger)pageNumber
{
CGPDFDocumentRef SourcePDFDocument = CGPDFDocumentCreateWithURL((CFURLRef)[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"myPDF" ofType:@"pdf"]]); // Create the CGPDFDocument from the URL
if (SourcePDFDocument == NULL) {
return nil;
}
// Reference to Page current Page
CGPDFPageRef SourcePDFPage = CGPDFDocumentGetPage(SourcePDFDocument, pageNumber);
if (SourcePDFPage == NULL) {
self.view.backgroundColor = [UIColor blueColor];
}
CGRect mediaBox = CGPDFPageGetBoxRect(SourcePDFPage, kCGPDFMediaBox);
NSMutableData *outputData = [NSMutableData data];
CGDataConsumerRef consumer = CGDataConsumerCreateWithCFData((__bridge CFMutableDataRef)outputData);
CGContextRef context = CGPDFContextCreate(consumer, &mediaBox, NULL);
// draw
CGContextBeginPage(context, &mediaBox);
CGContextDrawPDFPage(context, SourcePDFPage);
CGContextEndPage(context);
// cleanup
CGDataConsumerRelease(consumer);
CGContextRelease(context);
return outputData;
}
https://stackoverflow.com/questions/24794769
复制相似问题