我已使用此功能成功打印了一个 PDF:
-(void)printPDF{
NSString* fileName = @"Certificate.PDF";
NSArray *arrayPaths =
NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory,
NSUserDomainMask,
YES);
NSString *path = [arrayPaths objectAtIndex:0];
NSString* pdfFileName = [path stringByAppendingPathComponent:fileName];
void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) =
^(UIPrintInteractionController *pic, BOOL completed, NSError *error) {
if (!completed && error) NSLog(@"Print error: %@", error);
};
NSData *pdfData = [[NSFileManager defaultManager] contentsAtPath:pdfFileName];
printController.printingItem = pdfData;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
[printController presentFromRect:self.btnPrint.frame inView:self.btnPrint.superview
animated:YES completionHandler:completionHandler];
} else {
[printController presentAnimated:YES completionHandler:completionHandler];
}
}
当我想打印几个 PDF 时,我已将其放入一个数组(证书)中。在我选择打印机之前,printController 对话框关闭,并且出现以下错误。“警告:在演示或关闭过程中尝试从视图控制器中关闭!”
我是否需要为 printcontroller 使用一些 delgate 方法,或者有什么问题?
这是我打印几个 PDF 文档的尝试。
-(void)printAllPDF{
void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) =
^(UIPrintInteractionController *pic, BOOL completed, NSError *error) {
if (!completed && error) NSLog(@"Print error: %@", error);
};
printController.printingItems = certificates;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
[printController presentFromRect:self.btnPrintAll.frame inView:self.btnPrintAll.superview
animated:YES completionHandler:completionHandler];
} else {
[printController presentAnimated:YES completionHandler:completionHandler];
}
}