Iphone 如何在iPad中获得实际的pdf页面大小?
如何在iPad中获取PDF页面宽度和高度?有关于如何查找此信息的文档或建议吗?以下是代码;还可以将页面上的点从PDF坐标转换为iOS坐标。另见Iphone 如何在iPad中获得实际的pdf页面大小?,iphone,ipad,pdf,core-graphics,cgpdfdocument,Iphone,Ipad,Pdf,Core Graphics,Cgpdfdocument,如何在iPad中获取PDF页面宽度和高度?有关于如何查找此信息的文档或建议吗?以下是代码;还可以将页面上的点从PDF坐标转换为iOS坐标。另见 #导入 . . . . . . . . . . . NSString*pathToPdfDoc=[[NSBundle mainBundle]pathForResource:@“Test2”offType:@“pdf”]; NSURL*pdfUrl=[NSURL fileURLWithPath:pathToPdfDoc]; CGPDFDocumentRe
#导入
. . . . . . . . . . .
NSString*pathToPdfDoc=[[NSBundle mainBundle]pathForResource:@“Test2”offType:@“pdf”];
NSURL*pdfUrl=[NSURL fileURLWithPath:pathToPdfDoc];
CGPDFDocumentRef document=CGPDFDocumentCreateWithURL((CFURLRef)pdfUrl);
cgpdfageref page=CGPDFDocumentGetPage(文档,1);//假设所有页面大小相同!
//代码来自https://stackoverflow.com/questions/4080373/get-pdf-hyperlinks-on-ios-with-quartz,
//适当修改
CGPDFDictionaryRef pageDictionary=CGPDFPageGetDictionary(第页);
//*******获取页面大小
CGPDFArrayRef pageBoxArray;
if(!CGPDFDictionaryGetArray(pageDictionary、“MediaBox”和pageBoxArray)){
return;//我们这里出了点问题!!!
}
int pageBoxArrayCount=CGPDFArrayGetCount(pageBoxArray);
CGPDFReal pageCoords[4];
对于(int k=0;k
我接受了Donal O'Danachair的答案,并做了一些修改,因此rect大小也可以缩放到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
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=CGRect交叉点(cropBoxRect、mediaBoxRect);
CGFloat effectiveWidth=effectiveect.size.width;
CGFloat effectiveHeight=effectiveect.size.height;
CGFloat WITHSCALE=(边界宽度/有效宽度);
CGFloat heightScale=(边界高度/有效高度);
CGFloat pdfScale=(宽度刻度<高度刻度)?宽度刻度:高度刻度;
CGFloat x_offset=((边界宽度-(有效宽度*pdfScale))/2.0f);
CGFloat y_偏移=((边界高度-(有效高度*pdfScale))/2.0f);
y_偏移=(边界高度-y_偏移);//坐标系调整
//CGFloat x_translate=(x_offset-effectiveect.origin.x);
//CGFloat y_translate=(y_offset+effectiveect.origin.y);
CGPDFArrayRef输出阵列;
if(!CGPDFDictionaryGetArray(页面字典、注释和输出阵列)){
返回;
}
int arrayCount=CGPDFArrayGetCount(outputArray);
如果(!arrayCount){
//继续;
}
self.annotationrectary=[[NSMutableArray alloc]initWithCapacity:arrayCount];
对于(int j=0;jCGPDFDictionaryRef 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
-(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;
}
CGRect pageRect = CGPDFPageGetBoxRect(pdf, kCGPDFMediaBox);
// Which you can convert to size as
CGSize size = pageRect.size;
let pageRect = pdfPage.getBoxRect(CGPDFBox.mediaBox) // where pdfPage is a CGPDFPage
let pageSize = pageRect.size
/**
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")