2010-01-27 71 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