如何在Cocoa应用程序中打开PDF

3

我希望在Cocoa应用程序中打开PDF文件。我尝试使用Preview打开PDF,代码如下:

[[NSWorkspace sharedWorkspace] openURL:[NSURL fileURLWithPath:@"filepath"]];

但我想要类似于Xcode帮助窗口的方式打开附加的PDF文件。使用PDFKit如何实现这一点?

3个回答

5

我导入了QuartzFrameWork,并在xib中添加了一个PDFView。以下代码对我有用:

NSURL *url = [NSURL fileURLWithPath:@"filePath"]
PDFDocument *pdfDoc = [[PDFDocument alloc] initWithURL:url];
[_pdfView setDocument:pdfDoc];


0

在 .h 文件中

@interface MyViewController:UIViewController <UIDocumentInteractionControllerDelegate>
@property(nonatomic, retain) UIDocumentInteractionController *documentInteractionController;

在 .m 文件中

@synthesize documentInteractionController;
- (void)viewDidLoad{
[super viewDidLoad];
documentInteractionController = [[UIDocumentInteractionController alloc] init];
}
- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller{
return self;
}
-(void) openPdf{
NSString *filePath=filePath = [[NSBundle bundleForClass:[self class]] pathForResource:@"filename" ofType:@"pdf"];
NSURL *URL = [NSURL fileURLWithPath:filePath];
if (URL) {
    self.documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:URL];
    [self.documentInteractionController setDelegate:self];
    [self.documentInteractionController presentPreviewAnimated:YES];
}
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接