Open document
FSPDFViewCtrl supports opening PDFs from file paths, memory buffers, network URLs, and custom data sources.
Open from file path
The most common approach. openDoc:password:completion: creates FSPDFDoc, loads the document, and attaches it to FSPDFViewCtrl:
objc
NSString *path = [[NSBundle mainBundle] pathForResource:@"Sample" ofType:@"pdf"];
[pdfViewCtrl openDoc:path password:nil completion:^(FSErrorCode error) {
if (error != FSErrSuccess) {
NSLog(@"Failed to open document,error code: %d", (int)error);
}
}];You can also create FSPDFDoc manually and assign it with setDoc::
objc
FSPDFDoc *doc = [[FSPDFDoc alloc] initWithPath:path];
FSErrorCode error = [doc load:nil];
if (error == FSErrSuccess) {
[pdfViewCtrl setDoc:doc];
}Open from memory buffer
When PDF data is already in memory (for example decrypted bytes):
objc
NSData *pdfData = [NSData dataWithContentsOfFile:path];
[pdfViewCtrl openDocFromMemory:pdfData password:nil completion:^(FSErrorCode error) {
if (error != FSErrSuccess) {
NSLog(@"Failed to open document");
}
}];Open from custom data source
Implement FSFileReaderCallback for custom read logic (encrypted storage, network streams, etc.):
objc
[pdfViewCtrl openDocFromFileReader:customFileReader password:nil completion:^(FSErrorCode error) {
if (error != FSErrSuccess) {
NSLog(@"Failed to open document");
}
}];Open from URL
openDocFromURL:password:cacheOption:httpRequestProperties:completion: downloads and caches PDFs from a URL:
objc
NSURL *url = [NSURL URLWithString:@"https://example.com/sample.pdf"];
CacheFileOption *cacheOption = [[CacheFileOption alloc] init];
cacheOption.cacheFilePath = @"/path/to/cache/sample.pdf";
[pdfViewCtrl openDocFromURL:url password:nil cacheOption:cacheOption
httpRequestProperties:nil completion:^(FSErrorCode error) {
if (error != FSErrSuccess) {
NSLog(@"Failed to open document");
}
}];CacheFileOption sets local cache path and whether to wait for a full download before open; HttpRequestProperties sets custom HTTP headers.
Go to a page after open
Rendering is multithreaded—call gotoPage:animated: only after the document opens successfully. Prefer onDocOpened:error: on IDocEventListener:
objc
// Register document event listener
[pdfViewCtrl registerDocEventListener:self];
// IDocEventListener
- (void)onDocOpened:(FSPDFDoc *)document error:(int)error {
if (error == FSErrSuccess) {
[self.pdfViewCtrl gotoPage:2 animated:YES];
}
}Close document
objc
[pdfViewCtrl closeDoc:^{
NSLog(@"文档已关闭,资源已释放");
}];After closeDoc:, resources are released and onDocWillClose: / onDocClosed:error: fire on IDocEventListener.
Clear cache
After opening from a URL:
objc
// Clear cache for a URL
[pdfViewCtrl clearCacheFile:@"https://example.com/sample.pdf"];
// Clear all SDK cache
[pdfViewCtrl clearAllCacheFile];API reference
See the API reference for FSPDFDoc and FSPDFViewCtrl.