可以在iPad上计算powerpoint演示文稿的宽度/高度 [英] Can width/height of a powerpoint presentation be calculated on iPad

查看:226
本文介绍了可以在iPad上计算powerpoint演示文稿的宽度/高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ipad应用程序,它在UIWebView中显示来自NSData的powerpoint演示文稿,我需要知道单张幻灯片的宽度/高度,所以我知道在前进到下一张幻灯片时要滚动多远。 / p>

在PDF中,我可以使用CGPDFDocumentRef计算它,如下所示,但我找不到相应的powerpoint API,因为它是微软格式,我甚至怀疑它存在。



我可以使用javascript来确定整个演示文稿的宽度/高度,但这并不能帮助我知道推进单张幻灯片的进度,



执行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;
//现在我可以通过滚动页面进入正确的页面高度* currentPage


解决方案

想出来。



在这种情况下,UIWebView就好像它是一个HTML页面上的iFrame,它有一个源代码一些powerpoint文件。所以,你可以做一些事情,以及暴露的某些属性。



我发现document.all(一个数组)有元素 - 很多元素。我的20页powerpoint测试文件有300多个元素。看它们的宽度/高度并没有太大帮助 - 有些是iframe本身的尺寸,有些是从幻灯片1的左上角像素到幻灯片N的右下角像素的尺寸,有些是0x0,有些是(埋葬)在中间)是幻灯片的尺寸。我发现的模式是你必须跳过document.all中的第一个元素,然后迭代直到你找到一个宽度/高度非零的,它们具有相同的clientHeight,offsetHeight,scrollHeight值以及相同的值。 clientWidth,offsetWidth和scrollWidth。这些中的第一个将为您提供单张幻灯片的高度/宽度,您可以从那里开始。当然,因为您使用的是UIWebView,并且只能使用stringByEvaluatingJavaScriptFromString,所以您需要在一行返回值的javascript中执行此操作。



因此,要提取powerpoint演示文稿的宽度/高度,您可以写:

  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()==一个.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();];

并根据此时的宽度/高度做任何你需要的事情。



哦,文档加载后没有立即填充document.all - UIWebView似乎需要几毫秒才能收集并使其可用。我在webview完成加载后运行了1秒,并且它在ppt和pptx文件上运行良好。但它不适用于pdf。


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

解决方案

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.

这篇关于可以在iPad上计算powerpoint演示文稿的宽度/高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆