1

我已将 pdfview 滚动方向设置为水平/垂直以水平/垂直滚动 pdf。但是在这两种情况下,一个视图中总是出现两个页面(第一个页面占据了 80% 的屏幕,第二个页面占据了 20% 的屏幕)。如果我使用usePageViewController它,那么它只会在视图中显示一页,但如果我使用 pageviewcontroller,那么高亮文本功能将不起作用。这是代码。

#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]];
} 

如何在 PDFKit 的单个视图中放置一个 pdf 页面?

4

2 回答 2

0

尝试这个

pdfiew.displayMode = kPDFDisplaySinglePageContinuous;
pdfView.displayDirection = kPDFDisplayDirectionHorizontal;
[pdfView usePageViewController:YES withViewOptions:nil];
 self.pdfView.autoScales = YES;
pdfView.minScaleFactor = self.pView.scaleFactorForSizeToFit;
于 2018-04-30T10:42:37.373 回答
0

尝试设置这部分

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

设置文档后

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

也许还可以尝试在文档设置后设置其他相关属性pdfView

于 2018-02-28T02:44:22.297 回答