Je suis nouveau dans le développement d'ios. J'essaye d'afficher un pdf dans un centre d'UIWebview.i ai un pdf dans le mainbundle. Voici à quoi ressemble mon pdf maintenant quand chargé dans un UIWebView: (j'ai mis dans un gradient marron comme le UIWebView fond afin que vous puissiez voir ce que je veux dire mieux).
- (void)viewDidLoad { UIWebView *theWebView = [[UIWebView alloc] initWithFrame:[self.view bounds]]; [theWebView setContentMode:UIViewContentModeScaleAspectFit]; [theWebView setAutoresizingMask:UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth]; [theWebView setScalesPageToFit:YES]; NSSsortingng *filePath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"pdf"]; NSURL *filePathURL = [NSURL fileURLWithPath:filePath]; [theWebView loadRequest:[NSURLRequest requestWithURL:filePathURL]]; [self.view addSubview:theWebView]; [super viewDidLoad]; }
Je voudrais centrer cela afin que la marge inférieure soit la même que la marge supérieure. Comment est-ce que je ferais ceci avec un pdf (local)?
CGRect rect = [[UIScreen mainScreen] bounds]; CGSize screenSize = rect.size; UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0,0,screenSize.width,screenSize.height)]; NSSsortingng *path = [[NSBundle mainBundle] pathForResource:@"pdf" ofType:@"pdf"]; NSURL *targetURL = [NSURL fileURLWithPath:path]; NSURLRequest *request = [NSURLRequest requestWithURL:targetURL]; [webView loadRequest:request]; [self.view addSubview:webView];
Je ne sais pas si c'est la solution idéale pour votre question.
D'abord get la largeur et la hauteur du PDF ==>
float width = CGPDFPageGetBoxRect(PageRef, kCGPDFMediaBox).size.width; float height = CGPDFPageGetBoxRect(PageRef, kCGPDFMediaBox).size.height;
où Pageref est la reference de la page 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]
// Puis webview
UIWebView *theWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0,0, pdfSize.width, pdfSize.height)]]; [theWebView setContentMode:UIViewContentModeScaleAspectFit]; [theWebView setAutoresizingMask:UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth]; [theWebView setScalesPageToFit:YES]; NSSsortingng *filePath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"pdf"]; NSURL *filePathURL = [NSURL fileURLWithPath:filePath]; [theWebView loadRequest:[NSURLRequest requestWithURL:filePathURL]]; [supView addSubview: theWebView]; [self.view addSubview: supView];
MODIFIER:
Obtenez Pageref (partie),
NSSsortingng *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);