如何在 iPad 中获取 PDF 页面的宽度和高度?关于如何找到这些信息的任何文件或建议?
问问题
9769 次
5 回答
9
我在这里使用答案,直到我意识到一个单行
CGRect pageRect = CGPDFPageGetBoxRect(pdf, kCGPDFMediaBox);
// Which you can convert to size as
CGSize size = pageRect.size;
在 Apple 的示例 ZoomingPDFViewer 应用程序中使用
于 2013-04-03T08:08:36.437 回答
8
这是执行此操作的代码;还可以将页面上的点从 PDF 坐标转换为 iOS 坐标。另请参阅使用 Quartz 在 iOS 上获取 PDF 超链接
#import <CoreGraphics/CoreGraphics.h>
. . . . . . . . . . .
NSString *pathToPdfDoc = [[NSBundle mainBundle] pathForResource:@"Test2" offType:@"pdf"];
NSURL *pdfUrl = [NSURL fileURLWithPath:pathToPdfDoc];
CGPDFDocumentRef document = CGPDFDocumentCreateWithURL((CFURLRef)pdfUrl);
CGPDFPageRef page = CGPDFDocumentGetPage(document, 1); // assuming all the pages are the same size!
// code from https://stackoverflow.com/questions/4080373/get-pdf-hyperlinks-on-ios-with-quartz,
// suitably amended
CGPDFDictionaryRef pageDictionary = CGPDFPageGetDictionary(page);
//******* getting the page size
CGPDFArrayRef pageBoxArray;
if(!CGPDFDictionaryGetArray(pageDictionary, "MediaBox", &pageBoxArray)) {
return; // we've got something wrong here!!!
}
int pageBoxArrayCount = CGPDFArrayGetCount( pageBoxArray );
CGPDFReal pageCoords[4];
for( int k = 0; k < pageBoxArrayCount; ++k )
{
CGPDFObjectRef pageRectObj;
if(!CGPDFArrayGetObject(pageBoxArray, k, &pageRectObj))
{
return;
}
CGPDFReal pageCoord;
if(!CGPDFObjectGetValue(pageRectObj, kCGPDFObjectTypeReal, &pageCoord)) {
return;
}
pageCoords[k] = pageCoord;
}
NSLog(@"PDF coordinates -- bottom left x %f ",pageCoords[0]); // should be 0
NSLog(@"PDF coordinates -- bottom left y %f ",pageCoords[1]); // should be 0
NSLog(@"PDF coordinates -- top right x %f ",pageCoords[2]);
NSLog(@"PDF coordinates -- top right y %f ",pageCoords[3]);
NSLog(@"-- i.e. PDF page is %f wide and %f high",pageCoords[2],pageCoords[3]);
// **** now to convert a point on the page from PDF coordinates to iOS coordinates.
double PDFHeight, PDFWidth;
PDFWidth = pageCoords[2];
PDFHeight = pageCoords[3];
// the size of your iOS view or image into which you have rendered your PDF page
// in this example full screen iPad in portrait orientation
double iOSWidth = 768.0;
double iOSHeight = 1024.0;
// the PDF co-ordinate values you want to convert
double PDFxval = 89; // or whatever
double PDFyval = 520; // or whatever
// the iOS coordinate values
int iOSxval, iOSyval;
iOSxval = (int) PDFxval * (iOSWidth/PDFWidth);
iOSyval = (int) (PDFHeight - PDFyval) * (iOSHeight/PDFHeight);
NSLog(@"PDF: %f %f",PDFxval,PDFyval);
NSLog(@"iOS: %i %i",iOSxval,iOSyval);
于 2011-01-19T16:47:51.200 回答
2
以下方法返回根据 webview 缩放的第一个 pdf 页面的高度:
-(NSInteger) pdfPageSize:(NSData*) pdfData{
CGPDFDocumentRef document = CGPDFDocumentCreateWithProvider(CGDataProviderCreateWithCFData((__bridge CFDataRef)pdfData));
CGPDFPageRef page = CGPDFDocumentGetPage(document, 1);
CGRect pageRect = CGPDFPageGetBoxRect(page, kCGPDFMediaBox);
double pdfHeight, pdfWidth;
pdfWidth = pageRect.size.width;
pdfHeight = pageRect.size.height;
double iOSWidth = _webView.frame.size.width;
double iOSHeight = _webView.frame.size.height;
double scaleWidth = iOSWidth/pdfWidth;
double scaleHeight = iOSHeight/pdfHeight;
double scale = scaleWidth > scaleHeight ? scaleWidth : scaleHeight;
NSInteger finalHeight = pdfHeight * scale;
NSLog(@"MediaRect %f, %f, %f, %f", pageRect.origin.x, pageRect.origin.y, pageRect.size.width, pageRect.size.height);
NSLog(@"Scale: %f",scale);
NSLog(@"Final Height: %i", finalHeight);
return finalHeight;
}
它比 Donal O'Danachair 小很多,而且做的事情基本相同。您可以轻松地将其调整为您的视图大小并返回宽度和高度。
于 2012-12-27T05:43:05.660 回答
1
Swift 3.x(基于 Ege Akpinar 提供的答案)
let pageRect = pdfPage.getBoxRect(CGPDFBox.mediaBox) // where pdfPage is a CGPDFPage
let pageSize = pageRect.size
一个更完整的例子展示了如何在 PDF 文件中提取页面的尺寸:
/**
Returns the dimensions of a PDF page.
- Parameter pdfURL: The URL of the PDF file.
- Parameter pageNumber: The number of the page to obtain dimensions (Note: page numbers start from `1`, not `0`)
*/
func pageDimension(pdfURL: URL, pageNumber: Int) -> CGSize? {
// convert URL to NSURL which is toll-free-briged with CFURL
let url = pdfURL as NSURL
guard let pdf = CGPDFDocument(url) else { return nil }
guard let pdfPage = pdf.page(at: pageNumber) else { return nil }
let pageRect = pdfPage.getBoxRect(CGPDFBox.mediaBox)
let pageSize = pageRect.size
return pageSize
}
let url = Bundle.main.url(forResource: "file", withExtension: "pdf")!
let dimensions = pageDimension(pdfURL: url, pageNumber: 1)
print(dimensions ?? "Cannot get dimensions")
于 2017-07-02T00:51:59.027 回答
0
我接受了 Donal O'Danachair 的回答并进行了一些修改,因此矩形大小也被缩放到 pdf 的大小。这段代码实际上从 pdf 页面中获取了所有注释,并从 PDF rect 创建了 CGRect。部分代码是对 Donal 评论他的问题的答案。
CGPDFDictionaryRef pageDictionary = CGPDFPageGetDictionary(pageRef);
CGFloat boundsWidth = pdfView.bounds.size.width;
CGFloat boundsHeight = pdfView.bounds.size.height;
CGRect cropBoxRect = CGPDFPageGetBoxRect(pageRef, kCGPDFCropBox);
CGRect mediaBoxRect = CGPDFPageGetBoxRect(pageRef, kCGPDFMediaBox);
CGRect effectiveRect = CGRectIntersection(cropBoxRect, mediaBoxRect);
CGFloat effectiveWidth = effectiveRect.size.width;
CGFloat effectiveHeight = effectiveRect.size.height;
CGFloat widthScale = (boundsWidth / effectiveWidth);
CGFloat heightScale = (boundsHeight / effectiveHeight);
CGFloat pdfScale = (widthScale < heightScale) ? widthScale : heightScale;
CGFloat x_offset = ((boundsWidth - (effectiveWidth * pdfScale)) / 2.0f);
CGFloat y_offset = ((boundsHeight - (effectiveHeight * pdfScale)) / 2.0f);
y_offset = (boundsHeight - y_offset); // Co-ordinate system adjust
//CGFloat x_translate = (x_offset - effectiveRect.origin.x);
//CGFloat y_translate = (y_offset + effectiveRect.origin.y);
CGPDFArrayRef outputArray;
if(!CGPDFDictionaryGetArray(pageDictionary, "Annots", &outputArray)) {
return;
}
int arrayCount = CGPDFArrayGetCount( outputArray );
if(!arrayCount) {
//continue;
}
self.annotationRectArray = [[NSMutableArray alloc] initWithCapacity:arrayCount];
for( int j = 0; j < arrayCount; ++j ) {
CGPDFObjectRef aDictObj;
if(!CGPDFArrayGetObject(outputArray, j, &aDictObj)) {
return;
}
CGPDFDictionaryRef annotDict;
if(!CGPDFObjectGetValue(aDictObj, kCGPDFObjectTypeDictionary, &annotDict)) {
return;
}
CGPDFDictionaryRef aDict;
if(!CGPDFDictionaryGetDictionary(annotDict, "A", &aDict)) {
return;
}
CGPDFStringRef uriStringRef;
if(!CGPDFDictionaryGetString(aDict, "URI", &uriStringRef)) {
return;
}
CGPDFArrayRef rectArray;
if(!CGPDFDictionaryGetArray(annotDict, "Rect", &rectArray)) {
return;
}
int arrayCount = CGPDFArrayGetCount( rectArray );
CGPDFReal coords[4];
for( int k = 0; k < arrayCount; ++k ) {
CGPDFObjectRef rectObj;
if(!CGPDFArrayGetObject(rectArray, k, &rectObj)) {
return;
}
CGPDFReal coord;
if(!CGPDFObjectGetValue(rectObj, kCGPDFObjectTypeReal, &coord)) {
return;
}
coords[k] = coord;
}
char *uriString = (char *)CGPDFStringGetBytePtr(uriStringRef);
//******* getting the page size
CGPDFArrayRef pageBoxArray;
if(!CGPDFDictionaryGetArray(pageDictionary, "MediaBox", &pageBoxArray)) {
return; // we've got something wrong here!!!
}
int pageBoxArrayCount = CGPDFArrayGetCount( pageBoxArray );
CGPDFReal pageCoords[4];
for( int k = 0; k < pageBoxArrayCount; ++k )
{
CGPDFObjectRef pageRectObj;
if(!CGPDFArrayGetObject(pageBoxArray, k, &pageRectObj))
{
return;
}
CGPDFReal pageCoord;
if(!CGPDFObjectGetValue(pageRectObj, kCGPDFObjectTypeReal, &pageCoord)) {
return;
}
pageCoords[k] = pageCoord;
}
#if DEBUG
NSLog(@"PDF coordinates -- bottom left x %f ",pageCoords[0]); // should be 0
NSLog(@"PDF coordinates -- bottom left y %f ",pageCoords[1]); // should be 0
NSLog(@"PDF coordinates -- top right x %f ",pageCoords[2]);
NSLog(@"PDF coordinates -- top right y %f ",pageCoords[3]);
NSLog(@"-- i.e. PDF page is %f wide and %f high",pageCoords[2],pageCoords[3]);
#endif
// **** now to convert a point on the page from PDF coordinates to iOS coordinates.
double PDFHeight, PDFWidth;
PDFWidth = pageCoords[2];
PDFHeight = pageCoords[3];
// the size of your iOS view or image into which you have rendered your PDF page
// in this example full screen iPad in portrait orientation
double iOSWidth = 768.0;
double iOSHeight = 1024.0;
// the PDF co-ordinate values you want to convert
double PDFxval = coords[0]; // or whatever
double PDFyval = coords[3]; // or whatever
double PDFhval = (coords[3]-coords[1]);
double PDFwVal = coords[2]-coords[0];
// the iOS coordinate values
CGFloat iOSxval, iOSyval,iOShval,iOSwval;
iOSxval = PDFxval * (iOSWidth/PDFWidth);
iOSyval = (PDFHeight - PDFyval) * (iOSHeight/PDFHeight);
iOShval = PDFhval *(iOSHeight/PDFHeight);// here I scale the width and height
iOSwval = PDFwVal *(iOSWidth/PDFWidth);
#if DEBUG
NSLog(@"PDF: { {%f %f }, { %f %f } }",PDFxval,PDFyval,PDFwVal,PDFhval);
NSLog(@"iOS: { {%f %f }, { %f %f } }",iOSxval,iOSyval,iOSwval,iOShval);
#endif
NSString *uri = [NSString stringWithCString:uriString encoding:NSUTF8StringEncoding];
CGRect rect = CGRectMake(iOSxval,iOSyval,iOSwval,iOShval);// create the rect and use it as you wish
于 2011-02-03T22:41:26.110 回答