问题
I have an ipad app, which displays a powerpoint presentation from an NSData, within a UIWebView, and I need to know the width/height of a single slide so I know how far to scroll when advancing to the next slide.
In PDF, I can calculate it using CGPDFDocumentRef, as below, but I can't find a corresponding API for powerpoint, and since it's a microsoft format, I doubt it even exists.
I can use javascript to determine the width/height of the ENTIRE presentation, but that doesn't help me know how far to advance to go one single slide,
Code to do what I need to for a PDF:
CFDataRef myPDFData = (CFDataRef)presentationContent;
CGDataProviderRef provider = CGDataProviderCreateWithCFData(myPDFData);
CGPDFDocumentRef pdf = CGPDFDocumentCreateWithProvider(provider);
CGPDFPageRef page = CGPDFDocumentGetPage(pdf, 1);
CGRect pageRect = CGPDFPageGetBoxRect(page, kCGPDFMediaBox);
int pageHeight=(int)pageRect.size.height;
int pageWidth=(int)pageRect.size.width;
//now I can advance to the proper page by doing a scrollto pageHeight*currentPage
回答1:
Figured it out.
In this case, the UIWebView acts as if it's an iFrame on an HTML page, which has a source of some powerpoint file. So, there are certain things you can do to it, and certain properties that are exposed.
I found that document.all (an array) has elements - LOTS of elements. My 20 page powerpoint test file had over 300 elements. Looking at width / height of them was not overly helpful - some were the dimensions of the iframe itself, some were the dimensions from top-left pixel of slide 1 to bottom-right pixel of slide N, some were 0x0, and some (burried in the middle) were the dimensions of a slide. The pattern I found was that you have to skip the first element in document.all, and iterate through until you found one with non-zero width/height, who have identical values for clientHeight, offsetHeight, scrollHeight, as well as identical values for clientWidth, offsetWidth, and scrollWidth. The first one of these will give you the height/width of a single slide, and you can go from there. And, of course, because you're using a UIWebView, and only have stringByEvaluatingJavaScriptFromString to work with, you need to do this all in one line of javascript that returns the value.
So, to extract width/height of a powerpoint presentation, you can write:
NSString *w=[webview stringByEvaluatingJavaScriptFromString:@"function x(){var rtn='';for (var i=1;i<document.all.length;i++){var a=document.all[i];if (((a.clientWidth>0)&&(a.clientHeight>0))&&(a.scrollHeight.toString()==a.offsetHeight.toString())&&(a.offsetHeight.toString()==a.clientHeight.toString())){return ''+a.offsetWidth; }}return rtn;};x();"];
NSString *h=[webview stringByEvaluatingJavaScriptFromString:@"function x(){var rtn='';for (var i=1;i<document.all.length;i++){var a=document.all[i];if (((a.clientWidth>0)&&(a.clientHeight>0))&&(a.scrollHeight.toString()==a.offsetHeight.toString())&&(a.offsetHeight.toString()==a.clientHeight.toString())){return ''+a.offsetHeight; }}return rtn;};x();"];
and do whatever you need to with width/height from that point on.
Oh, and document.all isn't populated immediately once your document is loaded - UIWebView seems to need a few milliseconds to gather that and make it available. I run this 1 second after my webview has finished loading, and it works great on both ppt and pptx files. it does NOT work on pdf, though.
回答2:
amongst others, these objects contain width and height properties:
Sub Test()
Dim A As Application
Set A = Application
Debug.Print A.Width, A.Height
Debug.Print A.Windows(1).Width, A.Windows(1).Height
Debug.Print A.SlideShowWindows(1).Width, A.SlideShowWindows(1).Height
End Sub
The most suitable for you seems to be the last one.
Results for a 1680 x 1050 screen
PPT in full-screen
1266 793,5
1245,75 699,75
1260 787,5
PPT in ca. 1/2 of screen
711,75 551,25
691,5 457,5
1260 787,5 <-- same as above as slideshows always go full-screan at the selected aspect ratio
来源:https://stackoverflow.com/questions/7230660/can-width-height-of-a-powerpoint-presentation-be-calculated-on-ipad