如何在iPad中获得实际的PDF页面大小?

use*_*799 5 pdf iphone core-graphics cgpdfdocument ipad

如何在iPad中获取PDF页面宽度和高度?有关如何查找此信息的任何文件或建议?

Ege*_*nar 9

我在这里使用答案,直到我意识到这是一个单行

CGRect pageRect = CGPDFPageGetBoxRect(pdf, kCGPDFMediaBox);
// Which you can convert to size as
CGSize size = pageRect.size;
Run Code Online (Sandbox Code Playgroud)

用于Apple的示例ZoomingPDFViewer应用程序


Don*_*air 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);
Run Code Online (Sandbox Code Playgroud)