2010-10-11 80 views
7

我使用的代碼CGContext上PDF頁面方面適合

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)context 
{ 
    CGContextSetRGBFillColor(ctx, 1.0, 1.0, 1.0, 1.0); 
    CGContextFillRect(ctx, layer.bounds); 
    CGContextTranslateCTM(ctx, 0.0, layer.bounds.size.height); 
    CGContextScaleCTM(ctx, 1.0, -1.0); 
    CGContextConcatCTM(ctx, CGPDFPageGetDrawingTransform(myPageRef, kCGPDFBleedBox, layer.bounds, 0, true)); 
    CGContextDrawPDFPage(ctx, myPageRef); 
} 

的問題是,PDF頁面越做越離開邊境四面頁面的中心畫在CGContext上顯示PDF頁面。有沒有什麼辦法讓頁面適合屏幕顯示。

回答

15

擴大Tia的答案;內置方法CGPDFPageGetDrawingTransform將縮小,但不會增大。如果你想擴大規模,那麼你需要通過比較CGPDFGetBoxRect和你的內容區域的結果來計算出你自己的變換。鍵入即席:

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)context 
{ 
    CGContextSetRGBFillColor(ctx, 1.0, 1.0, 1.0, 1.0); 
    CGContextFillRect(ctx, layer.bounds); 
    CGContextTranslateCTM(ctx, 0.0, layer.bounds.size.height); 
    CGContextScaleCTM(ctx, 1.0, -1.0); 

    CGRect cropBox = CGPDFGetBoxRect(myPageRef, kCGPDFCropBox); 
    CGRect targetRect = layer.bounds; 
    CGFloat xScale = targetRect.size.width/cropBox.size.width; 
    CGFloat yScale = targetRect.size.height/cropBox.size.height; 
    CGFloat scaleToApply = xScale < yScale ? xScale : yScale; 
    CGContextConcatCTM(ctx, CGAffineTransformMakeScale(scaleToApply, scaleToApply));  

    CGContextDrawPDFPage(ctx, myPageRef); 
} 

所以:工作,你就必須有多少規模由它佔據視圖的整個寬度的文檔,多少佔據整個高度,然後實際上是由規模較小這兩個值。

+0

你可以請回答這個問題我也嘗試相同的方式,但我不能得到確切的適合http://stackoverflow.com/questions/3908624/cgcontext-pdf-page-aspect-fit – ajay 2011-01-14 05:27:57

+0

這是一個鏈接到這個問題;可能你的意思是http://stackoverflow.com/questions/4321681/how-to-fit-pdf-page-in-entire-view?我會盡快看看,只要我有機會,雖然我現在很遺憾地出去...... – Tommy 2011-01-14 13:26:17

+0

微小的更新,CGPDFGetBoxRect似乎現在是CGPDFPageGetBoxRect,但非常有幫助!這個大小讓我瘋狂。你也可以找到包括行CGContextConcatCTM(ctx,CGPDFPageGetDrawingTransform(myPageRef,kCGPDFCropBox,layer.bounds,0,true));在CGContextDrawPDFPage幫助任何旋轉了90張紙張的PDF之前使用 – sradforth 2012-08-23 22:24:49

1

粗略讀取您的代碼表明使用kCGPDFBleedBox應替換爲另一個設置,或許kCGPDFMediaBox。有關更多信息,請參閱here

+0

fbrereto,我試過kCGPDFMediaBox和所有其他可用選項。但顯示屏沒有變化。 – 2010-10-11 18:29:34

2

如果PDF頁矩形小於參數rect,則CGPDFPageGetDrawingTransform不會返回放大轉換。

+0

tia,我在其他一些pdf閱讀器應用程序中嘗試了相同的頁面,並且正在適當地顯示在屏幕上。那麼有沒有其他的方式來繪製它以適應方面。 – 2010-10-12 06:41:59

0

其他答案是正確的,CGPDFPageGetDrawingTransform不會放大,但是您可以縮小矩形以捕獲所選框。使用CGPDFPageGetBoxRect()並按接受的答案中的Tommy建議計算比例尺,然後使用該比例縮小傳遞到CGPDFPageGetDrawingTransform()中的捕獲矩形。

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)context 
{ 
    CGContextSetRGBFillColor(ctx, 1.0, 1.0, 1.0, 1.0); 
    CGContextFillRect(ctx, layer.bounds); 
    CGContextTranslateCTM(ctx, 0.0, layer.bounds.size.height); 

    CGRect box = CGPDFGetBoxRect(myPageRef, kCGPDFBleedBox); 
    CGFloat xScale = layer.bounds.size.width/cropBox.size.width; 
    CGFloat yScale = layer.bounds.size.height/cropBox.size.height; 
    CGFloat scaleToApply = xScale < yScale ? xScale : yScale; 

    CGRect captureRect = CGRectMake(0, 0, 
            layer.bounds.size.width/scaleToApply, 
            layer.bounds.size.height/scaleToApply); 

    CGAffineTransform drawXfm = CGPDFPageGetDrawingTransform(myPageRef, 
                  kCGPDFBleedBox, 
                  captureRect, 
                  0, 
                  true); 

    CGContextScaleCTM(ctx, scaleToApply, -scaleToApply); 
    CGContextConcatCTM(ctx, drawXfm); 
    CGContextDrawPDFPage(ctx, myPageRef); 
} 

然後它很容易擴展到處理過的風景:

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)context 
{ 
    CGContextSetRGBFillColor(ctx, 1.0, 1.0, 1.0, 1.0); 
    CGContextFillRect(ctx, layer.bounds); 
    CGContextTranslateCTM(ctx, 0.0, layer.bounds.size.height); 

    CGRect box = CGPDFGetBoxRect(myPageRef, kCGPDFBleedBox); 
    int rotate; 
    CGFloat xScale, yScale; 
    if (box.size.width > box.size.height) { 
     // landscape 
     rotate = 270; 
     xScale = layer.bounds.size.height/cropBox.size.width; 
     yScale = layer.bounds.size.width/cropBox.size.height; 
    } else { 
     // portrait 
     rotate = 0; 
     xScale = layer.bounds.size.width/cropBox.size.width; 
     yScale = layer.bounds.size.height/cropBox.size.height; 
    } 
    CGFloat scaleToApply = xScale < yScale ? xScale : yScale; 

    CGRect captureRect = CGRectMake(0, 0, 
            layer.bounds.size.width/scaleToApply, 
            layer.bounds.size.height/scaleToApply); 

    CGAffineTransform drawXfm = CGPDFPageGetDrawingTransform(myPageRef, 
               kCGPDFBleedBox, 
               captureRect, 
               rotate, 
               true); 

    CGContextScaleCTM(ctx, scaleToApply, -scaleToApply); 
    CGContextConcatCTM(ctx, drawXfm); 
    CGContextDrawPDFPage(ctx, myPageRef); 
} 
0

這裏是我的解決方案,也可以用來處理旋轉。可能有用。

// Create transformation 
int rotation = CGPDFPageGetRotationAngle(pdfPage); 
CGRect rect = CGPDFPageGetBoxRect(pdfPage, kCGPDFCropBox); 
CGRect rotatedRect = rect; 
CGRectApplyAffineTransform(rotatedRect, CGAffineTransformMakeRotation(M_PI * rotation/180.0)); 

CGFloat scale = MIN(self.bounds.size.width/rotatedRect.size.width, self.bounds.size.height/rotatedRect.size.height); 
// Scale 
CGContextConcatCTM(context, CGAffineTransformMakeScale(scale, scale)); 
// Move left bottom cornet to 0, 0 
CGContextConcatCTM(context, CGAffineTransformMakeTranslation(rotatedRect.size.width * 0.5, rotatedRect.size.height * 0.5));  
// Rotate 
CGContextConcatCTM(context, CGAffineTransformMakeRotation(-M_PI * rotation/180.0));  
// Move center into 0, 0 
CGContextConcatCTM(context, CGAffineTransformMakeTranslation(-rect.origin.x - rect.size.width * 0.5, -rect.origin.y - rect.size.height * 0.5)); 

CGContextDrawPDFPage(context, pdfPage);