开发者

PDF Page Drawing issue with iPhone sdk

开发者 https://www.devze.com 2022-12-18 18:45 出处:网络
I herewith a problem that i am facing while implementing PDF file in iPhone. What I want is simply display PDF files. and also Giving few Facilities like Zoom-in and Zoom-Out, Next Page navigation, P

I herewith a problem that i am facing while implementing PDF file in iPhone.

What I want is simply display PDF files. and also Giving few Facilities like Zoom-in and Zoom-Out, Next Page navigation, Previous Page Navigation etc.

I am very clear with Reading PDF Documents using url, i also get the number of pages and other property, But When i try to display it in View or WebView, i mean to say when i try to draw a pdf page, I am not getting page, just get simply blank View.

To Display page, I tried 5 different approaches, but none of them leads me to Success. Hence i have to approach You Guys.

I have attached here snippet of Code with 5 different approach i used.

Kindly go through it and guide me!! Your Suggestions are welcomed.

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

- (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. /////////

Every Thing is fine here!! i am Getting all Values from Functions. Now Bellow i have added 5 different ways Those i Follow for Displaying or Drawing a Page That i can see in 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 = CGP开发者_JAVA百科DFDocumentGetRotationAngle(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 /////////////////////////////

Among above 5 diffent way not single one lead me to Result.


You can't draw your view in -viewDidLoad. There's no CGContext at this point. All your drawing code needs to go into -drawRect. You want to read the Drawing Guide.

0

精彩评论

暂无评论...
验证码 换一张
取 消