Working with PDF Document Views

Operating system requirements: iOS 5.0 or later.

Mobile device: iPad.

Description

This example describes some methods of working with PDF document view. After starting the example the following operations are executed:

Required Files

It is required to add the following file in the Displaying of Regular Report example:

Source Code

Executing the example requires to place a PDF document search delegate class, which implements operations that should be executed on finding specified text and search finish, and also to add class header file to source code of the ViewController class (see the displaying of Regular Report section). Then is it required to place the following code in the body of the executeExample method of the ViewController class:

// Delete all window subviews
NSArray *reportSubviews = [self.view subviews];
for(UIView *subView in reportSubviews)
{
    [subView removeFromSuperview];
}
// Get regular report object
MAProceduralReportViewController *proceduralReportController = (MAProceduralReportViewController *)m_controller;
// Get array of regular report views
NSArray *dataViewControllers = [proceduralReportController dataViewControllers];
// Parse regular report views in cycle
for( UIViewController *controller in dataViewControllers)
{
    // Check if the current view is a regular report viewer
    if([controller isMemberOfClass:[MAProceduralReportDataViewController class]])
    {
        // Get sheet viewer view
        MAProceduralReportDataViewController *dataViewController =(MAProceduralReportDataViewController *)controller;
        // Get current regular report document URL
        NSURL *url = [dataViewController urlForCurrentDocument];
        CFURLRef urlForCurrentDocument = (CFURLRef)url;
        // Create a PDF document by means of obtained URL
        CGPDFDocumentRef pdfDocument = CGPDFDocumentCreateWithURL(urlForCurrentDocument);
        // Create a PDF document view
        PdfView *pdfView = [[PdfView alloc] initWithFrame:self.view.bounds];
        // Load PDF document to this view
        [pdfView setDocument:pdfDocument];
        // Display PDF document view on mobile device screen
        [self.view addSubview: pdfView];
        // Display page number tooltip in the view
        [pdfView showPageLabel:YES];
        // Get PDF document contents view
        UIView *contentView = [pdfView contentView];
        // Get PDF document contents display borders
        CGRect bounds = [contentView bounds];
        // Display view width and height in the development environment console
        NSLog(@"PDF document view contents width: %f", bounds.size.width);
        NSLog(@"PDF document view contents height: %f", bounds.size.height);
        // Specify PDF document parent view controller
        [pdfView setReportViewController:(UIViewController<PdfSearchDelegate> *)dataViewController];
        UIViewController *reportController = [pdfView reportViewController];
        // Determine width and height of PDF document parent view
        CGRect reportControllerBounds =[[reportController view] bounds];
        NSLog(@"PDF document parent view width: %f", reportControllerBounds.size.width);
        NSLog(@"PDF document parent view height: %f", reportControllerBounds.size.height);
        // Create a PDF document search delegate
        SearchDelegate *searchDelegate = [[SearchDelegate alloc] init];
        // Set search delegate for PDF document view
        [pdfView setSearchDelegate:searchDelegate];
        // Search PDF document pages containing specified string
        [pdfView findPagesComprisingString:@"Spain"];
    }
}

After executing the example the mobile device screen displays the PDF document view:

The development environment console displays information on the PDF document parent view, and also PDF document contents:

PDF document view contents width: 768.000000

PDF document view contents width: 1004.000000

PDF document parent view width: 768.000000

PDF document parent view height: 960.000000

 

When the specified text is found, the development environment console displays a message containing the number of PDF document page with the specified string:

Specified text is found on page 3

 

After search is finished the development environment console displays the appropriate message:

Text search is finished

 

Then it is required to place in the body of the executeExample method before the strings:

// Search PDF document pages containing specified string
[pdfView findPagesComprisingString:@"Spain"];

the following code:

// Stop searching PDF document pages
[pdfView stopSearching];

After executing the updated example the search is stopped, that is why the message with number of PDF document page with specified string is not displayed in the development environment console.

See also:

Examples of Component Use