2010-01-27 4 views
1

아이폰에서 PDF 파일을 구현하는 동안 문제가 생겼습니다.PDF 페이지 드로잉 문제 iPhone sdk

내가 원하는 것은 단순히 PDF 파일을 표시하는 것입니다. 확대 및 축소, 다음 페이지 탐색, 이전 페이지 탐색 등과 같은 몇 가지 기능을 제공합니다.

URL을 사용하여 PDF 문서를 읽을 때 매우 명확합니다. 페이지 수와 기타 속성을 얻을 수도 있지만 내가 View 또는 WebView에 표시하려고하면 PDF 페이지를 그릴 때 페이지를 가져 오지 못하고 단순히 빈보기로 표시된다는 것을 말합니다.

페이지를 표시하려면 5 가지 방법을 시도했지만 그 중 아무 것도 성공으로 연결되지 않습니다. 그러므로 나는 너희들에게 접근해야한다.

나는 5 가지 접근 방식을 사용하여 여기에 코드 스 니펫을 첨부했습니다.

친절하게 안내하고 안내 해주세요 !! 귀하의 제안을 환영합니다.

////////////////////////////

- (void)viewDidLoad { 
    [super viewDidLoad]; 

const char *file = @"Pdf_first.pdf"; 

CGPDFDocumentRef myDocument; 
CGPDFPageRef page; 
CGRect mediaBox; 
    CFStringRef path; 
size_t noOfPages; 
    CFURLRef url; 
CGContextRef pdfContext; 



//////// Code to get Path and Url for the dictionary where our PDF file currently stored. ///////// 

NSFileManager *FileManager = [NSFileManager defaultManager]; 
NSArray *paths1 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths1 objectAtIndex:0]; 
documentsDirectory = [documentsDirectory stringByAppendingPathComponent:@"Pdf_first.pdf"]; 

path = documentsDirectory; 

    url = CFURLCreateWithFileSystemPath (NULL, path,kCFURLPOSIXPathStyle, 0); 
myDocument = CGPDFDocumentCreateWithURL(url); 
myDocument = CGPDFDocumentRetain(myDocument); 


CFMutableDictionaryRef myDictionary = NULL; 
myDictionary = CFDictionaryCreateMutable(NULL, 0,&kCFTypeDictionaryKeyCallBacks,&kCFTypeDictionaryValueCallBacks); 
CFDictionarySetValue(myDictionary, kCGPDFContextTitle, CFSTR("My PDF File")); 
CFDictionarySetValue(myDictionary, kCGPDFContextCreator, CFSTR("My Name")); 



page = CGPDFDocumentGetPage(myDocument, 1); 
noOfPages = CGPDFDocumentGetNumberOfPages(myDocument); 
pdfContext = CGPDFContextCreateWithURL(url, NULL, myDictionary); 
CGRect pageMediaBox = CGPDFPageGetBoxRect(page, kCGPDFMediaBox); 

//////// Code to get Path and Url for the dictionary where our PDF file currently stored. ///////// 

모든 것 여기 괜찮습니다! 나는 모든 가치를 함수로부터 얻는다. 이제 벨로우 이에 5 가지 다른 방법을 추가했습니다. i 아이폰에서 볼 수있는 페이지를 표시하거나 그리는 데 따라갑니다.

////////////////////////// Way 1 /////////////////////// 

CGContextTranslateCTM(pdfContext, 0.0, [webView bounds].size.height); 
CGContextScaleCTM(pdfContext, 1.0, -1.0); 
CGContextConcatCTM(pdfContext, CGPDFPageGetDrawingTransform(page, kCGPDFCropBox,[webView bounds], 0, true)); 
CGContextDrawPDFPage(pdfContext, page);  
CGContextRestoreGState(pdfContext); 

////////////////////////// Way 1 /////////////////////// 


////////////////////////// Way 2 /////////////////////// 

CGRect bounds = CGRectMake(10, 10, 300, 300); 

CGContextSaveGState(pdfContext); 
CGAffineTransform pdfTransform = CGPDFPageGetDrawingTransform(page, kCGPDFCropBox, bounds, 0, true); 
CGContextConcatCTM(pdfContext, pdfTransform); 
CGContextDrawPDFPage(pdfContext, page); 
CGContextRestoreGState(pdfContext); 

////////////////////////// Way 2 /////////////////////// 

///////////////////////////////////// Way 3 //////////////////// 


    for(int i = 1; i <= noOfPages; ++i) { 

     CGPDFPageRef pdfPage = CGPDFDocumentGetPage(myDocument, i); 

     CGRect pageMediaBox = CGPDFPageGetBoxRect(pdfPage, kCGPDFMediaBox); 

     CGContextBeginPage (pdfContext, &pageMediaBox); 

    CGContextStrokeRect(pdfContext, CGRectMake(50, 50, 500,700)); 
    CGContextShowTextAtPoint (pdfContext, 60, 699, text, strlen(text)); 

    CGContextDrawPDFPage(pdfContext, pdfPage); 

     CGContextEndPage (pdfContext); 

    } 

    ///////////////////////////////////// Way 3 //////////////////// 

/////////////////////////////// Way 4 //////////////////////////////// 

//mediaBox = CGPDFDocumentGetMediaBox(document, 1); 

CGPDFBox mediaBox = CGPDFDocumentGetMediaBox(document, 1); 

//mediaBox = CGRectMake(10,10,300,300); 
// int rotationAngle = CGPDFDocumentGetRotationAngle(document, 1); 

int rotationAngle = 30; 

//CGContextDrawPDFDocument(UIGraphicsGetCurrentContext(), CGRectMake(25,25,250,250), document, 1); 

//CGPDFPageGetDrawingTransform(<#CGPDFPageRef page#>, <#CGPDFBox box#>, <#CGRect rect#>, <#int rotate#>, <#_Bool preserveAspectRatio#>) 

CGAffineTransform transform = CGPDFPageGetDrawingTransform(page, mediaBox, CGRectMake(25, 25, 250,250), rotationAngle, TRUE); 


    /////////////////////////////// Way 4 //////////////////////////////// 

///////////////////////// Way 5 ///////////////////////////// 

CGContextTranslateCTM(pdfContext, 0.0, 320); 

CGContextScaleCTM(pdfContext, 1.0, -1.0); 

CGContextSaveGState(pdfContext); 

CGAffineTransform pdfTransform = CGPDFPageGetDrawingTransform(page, kCGPDFCropBox, CGRectMake(0, 0, 280, 320), 0, true); 

CGContextConcatCTM(pdfContext, pdfTransform); 

CGContextDrawPDFPage(pdfContext, page); 

CGContextRestoreGState(pdfContext); 

    ///////////////////////// Way 5 ///////////////////////////// 

위의 5 가지 중 하나가 아니라 하나의 결과가 결과로 이어집니다.

+0

나를 위해 컴파일되지 않는 kCGPDFCropBox가 무엇입니까? – c0d3Junk13

답변

4

-viewDidLoad에서보기를 그릴 수 없습니다. 이 시점에는 CGContext가 없습니다. 모든 드로잉 코드는 -drawRect로 이동해야합니다. Drawing Guide을 읽고 싶습니다.

+0

감사합니다. Rob !! 나는 그것을한다. –

+0

참조한 링크가 더 이상 정확하지 않은 것 같습니다 – binnyb

+0

@binnyb 고마워요. 업데이트 됨. –