AAN
AAN

Reputation: 31

How to load pdf using PDFKit for iPad

I have this piece of code.

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

But it is not loading the PDF in the view. Also if I CGRectMake the entire PDF is not displayed in the debugger. How can I initialize PDFView and load it?

Upvotes: 0

Views: 1324

Answers (2)

ivion
ivion

Reputation: 567

High Aan,

tested it on myself with the following Code:

- (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];

}

and it display the PDF without Problems.

The only thing I found is

UIView tmpView = (UIView)currentView;

you allocated tmpview statically, it should be.

UIView *tmpView = (UIView *)currentView;

Otherwise test if your data is valid Pdf.

Upvotes: 1

Harsh
Harsh

Reputation: 2908

I found a very good article which explains in details how to use the PDFView. Tutorial.

Some code in Swift. I know you asked it in Objective-C but since you are into iOS, you should be able to understand the context.

    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
        }
    }

Upvotes: 0

Related Questions