Thursday, April 22, 2010

How to show PDF document/file on iphone / ipad.

Hi Guys.
Simple way to show pdf file on UIView. You just use the following class and alloc your UIView with PDFView class.
PDFView.h
#import
@interface PDFView : UIView {
CGPDFDocumentRef pdf1;
int pageNo;
}
-(void)drawInContext:(CGContextRef)context pno:(int)pageNo;
-(int)numberOfPages;
@end

PDFView.m

#import "PDFView.h"

@implementation PDFView

- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
CFURLRef pdfURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), CFSTR("iphoneos.pdf"), NULL, NULL);
pdf1 = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL);
CFRelease(pdfURL);
self.backgroundColor = [UIColor whiteColor];
self.opaque = YES;
}
return self;
}

- (void)drawRect:(CGRect)rect {
[self drawInContext:UIGraphicsGetCurrentContext()];
}
-(int)incrementPage :(int)no{
pageNo =no;
return pageNo;
}

- (void)dealloc {
[super dealloc];
}
-(void)drawInContext:(CGContextRef)context{
// before we start drawing.
CGContextTranslateCTM(context, 0.0, self.bounds.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
// Grab the first PDF page
CGPDFPageRef page = CGPDFDocumentGetPage(pdf1, pageNo);
// We're about to modify the context CTM to draw the PDF page where we want it, so save the graphics state in case we want to do more drawing
CGContextSaveGState(context);
// CGPDFPageGetDrawingTransform provides an easy way to get the transform for a PDF page. It will scale down to fit, including any
// base rotations necessary to display the PDF page correctly.
CGAffineTransform pdfTransform = CGPDFPageGetDrawingTransform(page, kCGPDFCropBox, self.bounds, 0, true);
// And apply the transform.
CGContextConcatCTM(context, pdfTransform);
// Finally, we draw the page and restore the graphics state for further manipulations!
CGContextDrawPDFPage(context, page);
CGContextRestoreGState(context);
}
//Returns Number of Pages
-(int)numberOfPages{
CFURLRef pdfURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), CFSTR("iphoneos.pdf"), NULL, NULL);
pdf1 = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL);
int noOfPage =CGPDFDocumentGetNumberOfPages((CFURLRef)pdf1);
return noOfPage;
}
@end
And your Viewcontroller class, just implement the following code

UIView *myView=[[PDFView alloc]initWithFrame:rect];
int totalpage = [myView numberOfPages];
[myView drawRect:CGRectMake(0, 0, rect.size.width, rect.size.height)];
[self.view addSubView:myView];

Monday, April 5, 2010

Transparent Background on iPhone UIWebView

Simple solution to creating a UIWebView with a transparent background:

  • set your background color of the UIWebView to clear
  • set the css for the body to background-color: transparent
  • here's the bugger... set the opaque property of the UIWebView to NO
Code
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 10, 310,450)]; self.bounds.size.height-60)];
[webView setBackgroundColor:[UIColor clearColor]];
[webView setOpaque:NO];
[self.view addSubview:webView];
cheers

Japji Sahib on iphone

Past week, i upload new application on the app store. Japji Sahib reader.






http://itunes.apple.com/in/app/japji-sahib-punjabi/id364602780?mt=8