2010-12-27 9 views
4

PDF를 표시하는 데 사용중인 코드 스 니펫을 첨부했습니다. 다음 코드는 PDF를 표시하지만, iPad 디스플레이의 전체 크기를 사용하지 않았거나 너무 작아서 페이지가 너무 작아 보입니다.iPad에서 PDF 전체 화면을 표시하려면 어떻게해야합니까?

iPad 디스플레이 나 확대/축소 상태의 경계에 맞는 PDF를 표시하려면 어떻게해야합니까? 나는 다른 접근법 (접근법 2)을 시도했지만 PDF가 90도 각도로 회전 된 상태에서 문제가 발생합니다.

접근-1 :

CGContextSaveGState(ctx); 

CGContextTranslateCTM(ctx, 0.0, [self.view bounds].size.height); 
CGContextScaleCTM(ctx, 1.0, -1.0); 
CGContextConcatCTM(ctx, 
        CGPDFPageGetDrawingTransform(page, kCGPDFCropBox, 
               [self.view bounds], 0, true)); 

CGContextDrawPDFPage(ctx, page);  
CGContextRestoreGState(ctx); 

접근-2 :

CGPDFPageRef page = CGPDFDocumentGetPage(pdfdocument, PageNo+1); 
if(page){ 
    CFRetain(page); 
} 
CGRect pageRect =CGPDFPageGetBoxRect(page, kCGPDFMediaBox); 
int angle= CGPDFPageGetRotationAngle(page); 
float pdfScale = self.bounds.size.width/pageRect.size.width; 
CGContextSetRGBFillColor(context, 1.0,1.0,1.0,1.0); 
CGContextFillRect(context,self.bounds); 
CGContextSaveGState(context); 
// Flip the context so that the PDF page is rendered 
// right side up. 
CGContextTranslateCTM(context, 0.0, self.bounds.size.height); 
CGContextScaleCTM(context, 1.0, -1.0); 

// Scale the context so that the PDF page is rendered 
// at the correct size for the zoom level. 
CGContextScaleCTM(context, pdfScale,pdfScale); 
CGContextDrawPDFPage(context, page); 
CGContextRestoreGState(context); 

이 사람이 나에게 어떤 크기 및 각도의 PDF 수있는 솔루션을 제안 할 수 는에 전체 화면을 표시 할 양쪽 방향의 iPad? 코드 스 니펫이나 의사 코드를 제공 할 수 있다면 좋을 것입니다. 감사합니다.

+0

안녕 idrish을 당신을하는 데 도움이 : //stackoverflow.com/questions/4538905/를 방법 - 할 - i - 디스플레이 - PDF - 전체 화면 - 온 - ipad/4655714 # 4655714 – ajay

답변

1

잘하면이 도움이됩니다. URL에서 참조하는 PDF의 첫 번째 페이지를 렌더링하는 방법을 보여줍니다.

이 코드는 내 코드베이스 스 니펫 (snippet) 모음이므로 1 파일에 붙여 넣기를 복사하여 빌드하고 실행하지 마십시오. 나는 당신이 어디에 속해 있는지를 알 수 있도록 몇 가지 코멘트를 달았으며, 그것을 작동시키기 위해 일부 ivars를 선언해야 할 것입니다. 이것은 당신의 PDF 전체 화면을 표시합니다

// helper function 
CGRect CGRectScaleAspectFit(CGRect sourceRect,CGRect fitRect) 
{ 
    if (sourceRect.size.width > fitRect.size.width) 
    { 
     float scale = fitRect.size.width/sourceRect.size.width; 
     sourceRect.size.width = fitRect.size.width; 
     sourceRect.size.height = (int)(sourceRect.size.height * scale); 
    } 

    if (sourceRect.size.height > fitRect.size.height) 
    { 
     float scale = fitRect.size.height/sourceRect.size.height; 
     sourceRect.size.height = fitRect.size.height; 
     sourceRect.size.width = (int)(sourceRect.size.width * scale); 
    } 

    return sourceRect; 
} 


// in your UIView subclass init method 

    CGPDFDocumentRef pdf = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL); 
    pdfPage = CGPDFDocumentGetPage(pdf, 1); 

    CGRect fitrect = CGRectMake(0, 0, self.frame.size.width,self.frame.size.height); 
    CGRect pageRect = CGPDFPageGetBoxRect(pdfPage, kCGPDFMediaBox); 

    CGRect f; 

    f.origin.x=0; 
    f.origin.y=0; 

    f.size.height = self.frame.size.height; 
    f.size.width = self.frame.size.height * pageRect.size.width/pageRect.size.height; 

    f = CGRectScaleAspectFit(f,fitrect); // this is the actual pdf frame rectangle to fill the screen as much as possible 
    pdfScale = f.size.height/pageRect.size.height; 


// in your UIView subclass drawRect method 

    CGContextSetRGBFillColor(context, 1.0,1.0,1.0,1.0); 
    CGContextFillRect(context,self.bounds); 

    CGContextSaveGState(context); 

    CGContextTranslateCTM(context, 0.0, self.bounds.size.height); 
    CGContextScaleCTM(context, 1.0, -1.0); 

    CGContextScaleCTM(context, pdfScale,pdfScale); 
    CGContextDrawPDFPage(context, pdfPage); 
    CGContextRestoreGState(context); 
0

: 확실하지 이것은 당신이 위의 프로젝트 라이브러리로 예 을 표시 할 PDF하지만

- (void)drawRect:(CGRect)rect 
{ 
    CGContextRef ctx = UIGraphicsGetCurrentContext(); 

    //PDF might be transparent, assume white paper - set White Background 
    [[UIColor whiteColor] set]; 
    CGContextFillRect(ctx, rect); 

    //Flip coordinates 
    CGContextGetCTM(ctx); 
    CGContextScaleCTM(ctx, 1, -1); 
    CGContextTranslateCTM(ctx, 0, -rect.size.height); 

    //PDF File Path 
    NSURL *pdfURL = [[NSBundle mainBundle] URLForResource:@"TEST" withExtension:@"pdf"]; 
    CGPDFDocumentRef pdf = CGPDFDocumentCreateWithURL((__bridge CFURLRef)pdfURL); 
    CGPDFPageRef page1 = CGPDFDocumentGetPage(pdf, 1); 

    //Get the rectangle of the cropped inside 
    CGRect mediaRect = CGPDFPageGetBoxRect(page1, kCGPDFCropBox); 
    CGContextScaleCTM(ctx, rect.size.width/mediaRect.size.width, 
       rect.size.height/mediaRect.size.height); 

    //Draw PDF 
    CGContextDrawPDFPage(ctx, page1); 
    CGPDFDocumentRelease(pdf); 
} 

복사 PDF를 처리하는 가장 좋은 방법입니다 PDF 파일의 이름은 "TEST"입니다 : 당신이 당신의 파일이 UIView의 내 전체 화면으로 PDF를 표시하는 나를 위해 작동

의 이름으로 당신의 이름을 원하는,

,

이것이 최선의 선택인지 잘 모르겠습니다. 문제가 있습니다. 즉, 확대/축소를 처리해야하며, 그렇게 할 경우 이동을 처리해야합니다. 또한 방향은 큰 혼란은 (당신이 가로 모드로 장치를 뒤집어 경우) 파일의 종횡비를 잃고 (그리고 찌그러됩니다)

PDF 처리에 뛰어하지만 .....

을 그것으로 라운드를 플레이입니다 IOS에서 고통은 아무것도 아닙니다. . . .

희망이 난 당신이 대답을 얻을 you..http 감사합니다 경우에 당신이 나에게 답을 알려 주시기 바랍니다 수있는 동일한 문제에 직면하고, 조금

관련 문제