iPad上可以计算PowerPoint演示文稿的宽度/高度吗
我有一个 ipad 应用程序,它在 UIWebView 中显示来自 NSData 的 powerpoint 演示文稿,并且我需要知道单张幻灯片的宽度/高度,以便我知道前进到下一张幻灯片时要滚动多远。
在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;
//now I can advance to the proper page by doing a scrollto pageHeight*currentPage
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
想通了。
在这种情况下,UIWebView 的行为就好像它是 HTML 页面上的 iFrame,其中包含某个 powerpoint 文件的源。因此,您可以对其执行某些操作,并公开某些属性。
我发现 document.all (一个数组)有元素 - 很多元素。我的 20 页 powerpoint 测试文件有 300 多个元素。查看它们的宽度/高度并没有太大帮助 - 有些是 iframe 本身的尺寸,有些是从幻灯片 1 的左上角像素到幻灯片 N 的右下角像素的尺寸,有些是 0x0,有些是(隐藏的)中间)是幻灯片的尺寸。我发现的模式是,您必须跳过 document.all 中的第一个元素,然后进行迭代,直到找到一个具有非零宽度/高度的元素,这些元素具有相同的 clientHeight、offsetHeight、scrollHeight 值以及相同的值客户端宽度、偏移宽度和滚动宽度。其中第一个将为您提供单张幻灯片的高度/宽度,您可以从那里开始。当然,因为您使用的是 UIWebView,并且只有 stringByEvaluatingJavaScriptFromString 可以使用,所以您需要在一行返回值的 javascript 中完成这一切。
因此,要提取 Powerpoint 演示文稿的宽度/高度,您可以编写:
并从该点开始使用宽度/高度执行您需要的任何操作。
哦,文档加载后 document.all 不会立即填充 - UIWebView 似乎需要几毫秒来收集它并使其可用。我在 webview 加载完成后运行 1 秒,它在 ppt 和 pptx 文件上都运行良好。但它不适用于 pdf。
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:
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.
其中,这些对象包含宽度和高度属性:
最适合您的似乎是最后一个。
PPT 中的结果
1680 x 1050 屏幕PPT 在全屏
大约 为 1680 x 1050 屏幕 PPT。屏幕的 1/2
amongst others, these objects contain width and height properties:
The most suitable for you seems to be the last one.
Results for a 1680 x 1050 screen
PPT in full-screen
PPT in ca. 1/2 of screen