Как загрузить PDF с помощью PDFKit для iPad - PullRequest
0 голосов
/ 07 ноября 2018

У меня есть этот кусок кода.

LoadPdf(void* currentView,NSData* content,NSString* urlName) {
    UIView tmpView = (UIView)currentView;

    NSData *data = content;

    PDFDocument *pdfDocument = [[PDFDocument alloc] initWithData:data];

    PDFView *pdfView = [[PDFView alloc] initWithFrame: CGRectMake(0, 0, 1000, 1000)];
    pdfView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleBottomMargin;
    pdfView.autoScales = NO ;
    pdfView.displayDirection = kPDFDisplayDirectionHorizontal;
    pdfView.displayMode = kPDFDisplaySinglePageContinuous;
    pdfView.displaysRTL = YES ;
    [pdfView setDisplaysPageBreaks:YES];
    [pdfView setDisplayBox:kPDFDisplayBoxTrimBox];
    pdfView.document = pdfDocument;

    [tmpView addSubview:pdfView];
}

Но это не загрузка PDF в виде. Кроме того, если я CGRectMake весь PDF не отображается в отладчике. Как я могу инициализировать PDFView и загрузить его?

Ответы [ 2 ]

0 голосов
/ 08 ноября 2018

High Aan,

проверил это на себе со следующим кодом:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    UIView *tmpView = [self view];

    NSData *data = [NSData dataWithContentsOfFile: @"local File on disk.pdf"];

    PDFDocument *pdfDocument = [[PDFDocument alloc] initWithData:data];

    PDFView *pdfView = [[PDFView alloc] initWithFrame: CGRectMake(0, 0, 1000, 1000)];
    pdfView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleBottomMargin;
    pdfView.autoScales = NO ;
    pdfView.displayDirection = kPDFDisplayDirectionHorizontal;
    pdfView.displayMode = kPDFDisplaySinglePageContinuous;
    pdfView.displaysRTL = YES ;
    [pdfView setDisplaysPageBreaks:YES];
    [pdfView setDisplayBox:kPDFDisplayBoxTrimBox];
    pdfView.document = pdfDocument;

    [tmpView addSubview:pdfView];

}

и отображение PDF без проблем.

Единственное, что я нашел, это

UIView tmpView = (UIView)currentView;

вы разместили tmpview статически, должно быть.

UIView *tmpView = (UIView *)currentView;

В противном случае проверьте правильность ваших данных Pdf.

0 голосов
/ 07 ноября 2018

Я нашел очень хорошую статью, в которой подробно объясняется, как использовать PDFView. Учебник .

Некоторый код в Swift. Я знаю, что вы спрашивали об этом в Objective-C, но, поскольку вы работаете с iOS, вы должны понимать контекст.

    if let path = Bundle.main.path(forResource: "MFI_2018_01", ofType: "pdf") {
        let url = URL(fileURLWithPath: path)
        if let pdfDocument = PDFDocument(url: url) {
            pdfView.displayMode = .singlePageContinuous
            pdfView.autoScales = true
            // pdfView.displayDirection = .horizontal
            pdfView.document = pdfDocument
        }
    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...