2013-09-06 6 views
3

Я новичок в разработке ios. Я пытаюсь отобразить pdf-файл в центре UIWebview. У меня есть pdf-файл в mainbundle. Вот как мой PDF выглядит сейчас, когда загружается в UIWebView : (Я ставлю коричневый градиент в качестве фона UIWebView, чтобы вы могли видеть, что я имею в виду лучше). enter image description hereКак загрузить файл в формате PDF в UIWebview center

- (void)viewDidLoad 
{ 
UIWebView *theWebView = [[UIWebView alloc] initWithFrame:[self.view bounds]]; 
[theWebView setContentMode:UIViewContentModeScaleAspectFit]; 
[theWebView setAutoresizingMask:UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth]; 
[theWebView setScalesPageToFit:YES]; 
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"pdf"]; 
NSURL *filePathURL = [NSURL fileURLWithPath:filePath]; 
[theWebView loadRequest:[NSURLRequest requestWithURL:filePathURL]]; 

[self.view addSubview:theWebView]; 
[super viewDidLoad]; 
} 

Я хотел бы центрирования, что так, что нижнее поле такое же, как верхний край. Как мне это сделать с помощью PDF (локального)?

+0

UIWebView не предназначен для использования, как, что , Искать pdf-просмотрщики/парсеры, что-то вроде этого https://github.com/vfr/Reader –

+0

@NimrodGutman можно загрузить PDF-файл uiwebview center с помощью html-кода, используя – Ravindhiran

+0

, где-то уже видели скриншот. .. Вы уже задали любой вопрос по тому же поводу? –

ответ

0

Не уверен, что это идеальное решение для вашего вопроса.

Сначала Получить ширину и высоту PDF ==>

float width = CGPDFPageGetBoxRect(PageRef, kCGPDFMediaBox).size.width; 

float height = CGPDFPageGetBoxRect(PageRef, kCGPDFMediaBox).size.height; 

где PAGEREF является эталонным PDF-страницы.

CGSize pdfSize = CGSizeMake(width, height); 

//Create one superView to webview 

UIView *supView = [[UIView alloc] initWithFrame:[self.view bounds]]; 
supView.backgroundColor=[UIColor colorWithRed:0.663 green:0.855 blue:0.341 alpha:1] 

// Тогда Webview

UIWebView *theWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0,0, pdfSize.width, pdfSize.height)]]; 
    [theWebView setContentMode:UIViewContentModeScaleAspectFit]; 
    [theWebView setAutoresizingMask:UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth]; 
    [theWebView setScalesPageToFit:YES]; 
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"pdf"]; 
    NSURL *filePathURL = [NSURL fileURLWithPath:filePath]; 
    [theWebView loadRequest:[NSURLRequest requestWithURL:filePathURL]]; 
    [supView addSubview: theWebView]; 

    [self.view addSubview: supView]; 

EDIT:

Получить PAGEREF (часть),

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"pdf"]; 
    NSURL *filePathURL = [NSURL fileURLWithPath:filePath]; 

CFURLRef pdfURL = (__bridge CFURLRef)[[NSURL alloc] initFileURLWithPath:filePath]; 
//file ref 
CGPDFDocumentRef pdfDocRef = CGPDFDocumentCreateWithURL((CFURLRef) pdfURL); 

CGPDFPageRef PageRef = CGPDFDocumentGetPage(pdfDocRef, 1); 

float width = CGPDFPageGetBoxRect(page1, kCGPDFMediaBox).size.width; 

float height = CGPDFPageGetBoxRect(page1, kCGPDFMediaBox).size.height; 

NSLog(@"%f %f",height,width); 
+0

ошибка использования необъявленного идентификатора PageRef, pdfPageRef – Ravindhiran

+0

@ Ravindhiran..both использовать одну и ту же переменную ... PageRef ... –

+0

@ Ravindhiran .. посмотрим на мой отредактированный answer ... –

1
CGRect rect = [[UIScreen mainScreen] bounds]; 
    CGSize screenSize = rect.size; 
    UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0,0,screenSize.width,screenSize.height)]; 

    NSString *path = [[NSBundle mainBundle] pathForResource:@"pdf" ofType:@"pdf"]; 
    NSURL *targetURL = [NSURL fileURLWithPath:path]; 
    NSURLRequest *request = [NSURLRequest requestWithURL:targetURL]; 
    [webView loadRequest:request]; 

    [self.view addSubview:webView];