1

I have set pdfview scrolling direction as horizontal/vertical to scroll the pdf horizontally/vertically. But it appears in both cases there are always two pages appear in one view(1st page acquires 80% of the screen and 2nd page covers 20% of the screen). If I useusePageViewController then only it appears one page in a view but if I use pageviewcontroller then higlighting text feature doesn't work. Here is the code.

#import <PDFKit/PDFKit.h>

@interface ViewController ()<PDFDocumentDelegate,PDFViewDelegate>
{
    PDFView *pdfView;
    PDFPage *page;
    NSMutableArray *arrHighlitedSelections;
}
@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];
    arrHighlitedSelections = [NSMutableArray new];

    pdfView = [[PDFView alloc] initWithFrame: self.view.bounds];

    pdfView.maxScaleFactor = 4.0;
    pdfView.minScaleFactor = pdfView.scaleFactorForSizeToFit;
    pdfView.displayDirection = kPDFDisplayDirectionHorizontal;
    pdfView.displayMode = kPDFDisplaySinglePageContinuous;
    pdfView.displaysRTL = YES ;
    [pdfView usePageViewController:YES withViewOptions:nil];
    [pdfView setDisplaysPageBreaks:YES];
    pdfView.delegate = self;

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(selectionChangedNotification:) name:PDFViewSelectionChangedNotification  object:nil];

    [self.view addSubview:pdfView];

    NSURL * URL = [[NSBundle mainBundle] URLForResource: @"sample" withExtension: @"pdf"];

    PDFDocument *document = [[PDFDocument alloc] initWithURL: URL];
    pdfView.document = document;

    UITapGestureRecognizer *pdfVwTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clearHighlightGesture:)];
    [pdfView addGestureRecognizer:pdfVwTapRecognizer];
}


#pragma mark - Highlight Texts

- (void)highLight:(id) sender {
    PDFSelection *slc = pdfView.currentSelection;
    NSArray *arrSelections = slc.selectionsByLine;
    [arrHighlitedSelections addObject:arrSelections];

    NSMutableArray *arr = [NSMutableArray new];

    for (int  i = 0; i<[arrHighlitedSelections count]; i++) {
            for (PDFSelection *sl in arrHighlitedSelections[i]) {
                slc.color = [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:0.0/255.0 alpha:0.6];
                [arr addObject:sl];
        }
    }

    [pdfView setHighlightedSelections:arr];
}

#pragma mark - Selection Changed Notification

- (void) selectionChangedNotification:(NSNotification *) notification
{
    PDFView *pdfVw = notification.object;
    NSLog(@"selected text: %@",pdfVw.currentSelection.string);

    UIMenuItem *highlight = [[UIMenuItem alloc] initWithTitle:@"Highlight" action:@selector(highLight:)];

    [[UIMenuController sharedMenuController] setMenuItems:@[highlight]];
} 

How can I fit one pdf page on a single view in PDFKit?

Poles
  • 3,585
  • 9
  • 43
  • 91

2 Answers2

0

Try to set this part

pdfView.maxScaleFactor = 4.0;
pdfView.minScaleFactor = pdfView.scaleFactorForSizeToFit;

after you set the document

PDFDocument *document = [[PDFDocument alloc] initWithURL: URL];
pdfView.document = document;

Maybe also try to set the other relevant properties after the document has been set on the pdfView

christopher.online
  • 2,614
  • 3
  • 28
  • 52
0

try this

pdfiew.displayMode = kPDFDisplaySinglePageContinuous;
pdfView.displayDirection = kPDFDisplayDirectionHorizontal;
[pdfView usePageViewController:YES withViewOptions:nil];
 self.pdfView.autoScales = YES;
pdfView.minScaleFactor = self.pView.scaleFactorForSizeToFit;