Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/115.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在iOS WebView中获取PDF文档高度_Ios_Objective C_Iphone_Pdf_Uiwebview - Fatal编程技术网

在iOS WebView中获取PDF文档高度

在iOS WebView中获取PDF文档高度,ios,objective-c,iphone,pdf,uiwebview,Ios,Objective C,Iphone,Pdf,Uiwebview,我试图通过NSURL在UIWebView中显示PDF。它很好用 但是我不知道pdf文档的高度。因此,有时它会创建空白或需要滚动。PDF也可能包含多个页面 我只想显示第一页,如果它包含多页 我的代码如下: NSURL *url = [NSURL URLWithString:@"http://www.eecs.harvard.edu/econcs/pubs/online.pdf"]; NSURLRequest * request = [NSURLRequest requestWithURL:url

我试图通过
NSURL
UIWebView
中显示PDF。它很好用

但是我不知道pdf文档的高度。因此,有时它会创建空白或需要滚动。PDF也可能包含多个页面

我只想显示第一页,如果它包含多页

我的代码如下:

NSURL *url = [NSURL URLWithString:@"http://www.eecs.harvard.edu/econcs/pubs/online.pdf"];
 NSURLRequest * request = [NSURLRequest requestWithURL:url];
    [web_large loadRequest:request];
    [web_large setScalesPageToFit:YES];
现在,
WebView
有一个固定的高度

试试这个

有这样一种方法:

size_t CGPDFDocumentGetNumberOfPages(CGPDFDocumentRef document)
这将为您提供
页面
编号

例如

NSURL *pdfUrl = [NSURL fileURLWithPath:yourPath];    
CGPDFDocumentRef document = CGPDFDocumentCreateWithURL((CFURLRef)pdfUrl);
下面的代码给出了
pdf
文件中
单个
页面的
高度

  float width = CGPDFPageGetBoxRect(pdfPageRef, kCGPDFMediaBox).size.width;
    float height = CGPDFPageGetBoxRect(pdfPageRef, kCGPDFMediaBox).size.height;

希望有帮助。

试试这个。。工作正常

 NSURL* pdfFileUrl = targetURL;
CGPDFDocumentRef pdf = CGPDFDocumentCreateWithURL((CFURLRef)pdfFileUrl);

CGFloat pdfHeight = 0;
NSUInteger totalNum = CGPDFDocumentGetNumberOfPages(pdf);
 for(int i = 0; i < totalNum; i++ ) {
    CGPDFPageRef myPageRef=CGPDFDocumentGetPage(pdf, i+1);
    CGRect cropBox = CGPDFPageGetBoxRect(myPageRef, kCGPDFCropBox);
    pdfHeight+=cropBox.size.height;
    int pageRotation = CGPDFPageGetRotationAngle(myPageRef);
    CGSize pageVisibleSize = CGSizeMake(cropBox.size.width, cropBox.size.height);
    if ((pageRotation == 90) || (pageRotation == 270) ||(pageRotation == -90)) {
        pageVisibleSize = CGSizeMake(cropBox.size.height, cropBox.size.width);
    }
}
NSLog(@"%f",pdfHeight);
NSURL*pdfFileUrl=targetURL;
CGPDFDocumentRef pdf=CGPDFDocumentCreateWithURL((CFURLRef)pdfFileUrl);
CGFloat PDFHHEIGH=0;
NSInteger totalNum=CGPDFDocumentGetNumberOfPages(pdf);
对于(int i=0;i
我建议您不要在webView中加载pdf,而是在UIView中绘制pdf,就像我在自己的项目中所做的那样,它工作得非常完美。 下面是在UIView上下文中绘制pdf的mine UIView子类

PDFPage.h

#import <UIKit/UIKit.h>

@protocol PDFPageDelegate;



@interface PDFPage : UIView

@property    uint                   currentPage;
@property    unsigned long          totalPages;
@property    CGRect                 pageRect;
@property    NSString               *pdfPath;
@property    id<PDFPageDelegate>    delegate;


- (CGRect)setPdf:(NSString*)filePath;
- (void)swipeLeft;
- (void)swipeRight;
- (void)setPageNumber:(NSUInteger )targetPageNumber;

@end







@protocol PDFPageDelegate <NSObject>

- (void)pdfWillSwipeToLeft:(uint)upcommingPageNumber;
- (void)pdfWillSwipeToRight:(uint)upcommingPageNumber;
- (void)pdfTaped:(CGPoint)tapAt;
- (void)pdfDoubleTapped;

@end
如何使用:-

    NSData *pdfData = [NSData dataWithContentsOfURL:[NSURL URLWithString:pdfPath]]; // here pdfPath is webURL

    NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString * savePDFAt = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
    savePDFAt = [NSString stringWithFormat:@"%@/PDFs/",savePDFAt];
    [[NSFileManager defaultManager] createDirectoryAtPath:savePDFAt withIntermediateDirectories:NO attributes:nil error:nil];
    [savePDFAt stringByAppendingPathComponent:"test.pdf"];

    if([pdfData writeToFile:savePDFAt options:0 error:&error])
        NSLog(@"PDF download complete");

    PDFPage *pdfPage = [PDFPage new];
    pdfPage.alpha = 0.0f;
    pdfPage.delegate = self;
    pdfPage.frame = [pdfPage setPdf:pdfPath];
然后将此PDF页面添加到self视图或滚动条中。

此页面很简单

您必须访问webView的滚动视图,而不是webView本身:

CGFloat totalHeight = self.webView.scrollView.contentSize.height;

帕蒂尔的回答是正确的。我把你的代码和他的代码结合起来,效果很好。只需将以下行粘贴到项目中,并亲自查看即可:

UIWebView *web_large = [[UIWebView alloc]init];
[self.view addSubview:web_large];

NSURL *url = [NSURL URLWithString:@"https://www.truthinadvertising.org/wp-content/uploads/2014/09/App-Store-Review-Guidelines.pdf"];
NSURLRequest * request = [NSURLRequest requestWithURL:url];
[web_large loadRequest:request];
[web_large setScalesPageToFit:YES];

CGPDFDocumentRef pdfDocumentRef = CGPDFDocumentCreateWithURL((CFURLRef)url);
CGPDFPageRef pdfPageRef = CGPDFDocumentGetPage(pdfDocumentRef, 1);

CGRect pdfPageRect = CGPDFPageGetBoxRect(pdfPageRef, kCGPDFMediaBox);

float width = pdfPageRect.size.width;
float height = pdfPageRect.size.height;

CGRect screenRect = [[UIScreen mainScreen]bounds];
web_large.frame = CGRectMake(0, 0, screenRect.size.width, height*screenRect.size.width/width);
在Swift中,您可以执行以下操作:

let documents = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first
let writePath = documents?.appending("/myPdf.pdf")
let pdf: CGPDFDocument! = CGPDFDocument(URL.init(fileURLWithPath: writePath!) as CFURL)`enter code here`
let firstPage: CGPDFPage = pdf.page(at: 1)!
let heightFirstPage :CGRect =  firstPage.getBoxRect(.mediaBox) 
var heightPagePdf : CGFloat = heightFirstPage.height;
变量“pdf”还有一个属性“numberOfPages”,因此您可以知道pdf文档的整个高度(pdf.numberOfPages*heightPagePdf)


致以最诚挚的问候

谢谢您的回答!但它给出了错误的高度!e、 它给出了800像素的高度,但如果我为web视图设置了800像素的高度,那么它似乎有很多空白@user2526811 CGPDFPageGetBoxRect中的维度是文档的本机维度,您需要将web视图的高度设置为“web_large.frame.size.width*(height/width)”,因此高度是文档大小的一个比率,对于您的答案是K!等待我很抱歉!但它不起作用!如果我认为PDF只有1页,也是同样的答案。它给出的高度为800,但它显示了大量的空白^为什么不接受这个答案,这给出了webview中pdf内容的期望高度,非常正确。该人明确表示“但我不知道pdf文档的高度”,问题的标题是:“在iOS webview中获取pdf文档高度”我认为我的答案在这些情况下是非常好的@SwiftArchitect我认为其他回答这个问题的人都有不同的看法。我可以发誓事情不是这样开始的。那么这不是或的复制品吗?@SwiftArchitect我同意这是复制品。但我只是在赶时髦,想廉价地赢得一些声誉;)只是honest@SahebRoy评估这一点不是你的工作。但是,如果你觉得答案有用,你应该把它投上一票。如果你只需要文件的高度,那么这个问题是重复的。如果您需要给定页面中占用的空间量,您可能需要重新考虑您的方法,因为PDF本质上是一种可移植的文档格式,超越了平台和浏览器。如果那个空白是由文档的创建者打算的,当PDF文档被创建为页面时,它可能是,你可以考虑显示它。CGPDFDocumentCreateWithURL是否异步?@turingtested在加载“https”url时不适用于我。请尽快提供帮助possible@YogendraPatel,此代码仍然适用于我(但是UIWebView现在已被弃用)。我刚刚用https更新了URL(见上文se)测试了代码。所以这不是代码,也许你在应用程序中通过Info.plist?@turingtested中的应用程序传输安全设置有限制。你能建议Info.plist?@turingtested在哪个版本中测试的代码吗?因为此问题仅在以下ios12中发生。它在ios12中工作得非常好。在如何使用示例中,没有声明“pdfPath”变量
let documents = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first
let writePath = documents?.appending("/myPdf.pdf")
let pdf: CGPDFDocument! = CGPDFDocument(URL.init(fileURLWithPath: writePath!) as CFURL)`enter code here`
let firstPage: CGPDFPage = pdf.page(at: 1)!
let heightFirstPage :CGRect =  firstPage.getBoxRect(.mediaBox) 
var heightPagePdf : CGFloat = heightFirstPage.height;