2010-01-27 67 views
1

我這裏有一個問題,我正在面臨,同時在iPhone中實現PDF文件。PDF頁面繪圖問題與iPhone sdk

我想要的只是顯示PDF文件。並提供像放大和縮小,下一頁導航,上一頁導航等幾個設施。

我很清楚使用url閱讀PDF文檔,我也獲得頁數和其他屬性,但當我嘗試在視圖或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種不同的方式那些 我遵循顯示或繪製一個頁面,我可以看到在iPhone。

////////////////////////// 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 2016-04-11 17:39:23

回答

4

你不能在-viewDidLoad中繪製你的視圖。這裏沒有CGContext。你所有的繪圖代碼都需要進入-drawRect。你想閱讀Drawing Guide

+0

謝謝Rob!我做到了。 – 2010-02-19 13:13:21

+0

您引用的鏈接似乎不再正確 – binnyb 2010-12-20 18:43:41

+0

@binnyb謝謝。更新。 – 2010-12-22 19:50:02