2012-04-23 9 views
0

私はplistから複数ページのPDFデータを作成し、そのようなPDFを電子メールで送信しなければならないアプリケーションを開発しています。 PDFに変換されるコンテンツにはテキストと画像が含まれます。私はこれに関するいくつかの記事を見てきましたが、それからあまりにも多くを集めることはできませんでした。もし誰かがこれを手伝うことができれば幸いです。イメージでpdfを作成する

ここは私が行ったPDF作成のためのコードです。

// create some sample data. In a real application, this would come from the database or an API. 
NSString* path = [[NSBundle mainBundle] pathForResource:@"sampleData" ofType:@"plist"]; 
NSDictionary* data = [NSDictionary dictionaryWithContentsOfFile:path]; 

NSLog(@"collections %@",data); 
NSArray* students = [data objectForKey:@"Students"]; 
NSLog(@"collections students %@",students); 



// get a temprorary filename for this PDF 
path = NSTemporaryDirectory(); 

NSLog(@"path here %@",path); 

self.pdfFilePath = [path stringByAppendingPathComponent:[NSString stringWithFormat:@"%d.pdf", [[NSDate date] timeIntervalSince1970]]]; 

NSLog(@"pdf file path and Name %@",self.pdfFilePath); 

// Create the PDF context using the default page size of 612 x 792. 
// This default is spelled out in the iOS documentation for UIGraphicsBeginPDFContextToFile 
UIGraphicsBeginPDFContextToFile(self.pdfFilePath, CGRectZero, nil); 

// get the context reference so we can render to it. 
CGContextRef context = UIGraphicsGetCurrentContext(); 

int currentPage = 0; 

// maximum height and width of the content on the page, byt taking margins into account. 
CGFloat maxWidth = kDefaultPageWidth - kMargin * 2; 
CGFloat maxHeight = kDefaultPageHeight - kMargin * 2; 

// we're going to cap the name of the class to using half of the horizontal page, which is why we're dividing by 2 
CGFloat classNameMaxWidth = maxWidth/3; 

// the max width of the grade is also half, minus the margin 
CGFloat gradeMaxWidth = (maxWidth/3) - kColumnMargin; 
CGFloat yearMaxWidth = (maxWidth/3) - kColumnMargin; 


// only create the fonts once since it is a somewhat expensive operation 
UIFont* studentNameFont = [UIFont boldSystemFontOfSize:17]; 
UIFont* classFont = [UIFont systemFontOfSize:15]; 

CGFloat currentPageY = 0; 


UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, kDefaultPageWidth, kDefaultPageHeight), nil); 
currentPageY = kMargin; 

// iterate through out students, adding to the pdf each time. 
for (NSDictionary* student in students) 
{ 

    // every student gets their own page 
    // Mark the beginning of a new page. 


    NSString* title = [NSString stringWithFormat:@"Order List"]; 

    CGSize size2 = [title sizeWithFont:studentNameFont forWidth:maxWidth lineBreakMode:UILineBreakModeWordWrap]; 

    [title drawAtPoint:CGPointMake(kMargin, currentPageY) forWidth:maxWidth withFont:studentNameFont lineBreakMode:UILineBreakModeWordWrap]; 
    currentPageY += size2.height; 

    CGContextSetStrokeColorWithColor(context, [[UIColor blueColor] CGColor]); 
    CGContextMoveToPoint(context, kMargin, currentPageY); 
    CGContextAddLineToPoint(context, kDefaultPageWidth - kMargin, currentPageY); 
    CGContextStrokePath(context); 

    // draw the student's name at the top of the page. 
    NSString* name = [NSString stringWithFormat:@"%@ %@", 
         [student objectForKey:@"FirstName"], 
         [student objectForKey:@"LastName"]]; 

    CGSize size = [name sizeWithFont:studentNameFont forWidth:maxWidth lineBreakMode:UILineBreakModeWordWrap]; 
    [name drawAtPoint:CGPointMake(kMargin, currentPageY) forWidth:maxWidth withFont:studentNameFont lineBreakMode:UILineBreakModeWordWrap]; 
    currentPageY += size.height; 

    NSString* dob = [NSString stringWithFormat:@"Grade"]; 

    CGSize size1 = [name sizeWithFont:studentNameFont forWidth:maxWidth lineBreakMode:UILineBreakModeWordWrap]; 
    [dob drawAtPoint:CGPointMake(kMargin+ classNameMaxWidth + kColumnMargin, currentPageY) forWidth:maxWidth withFont:studentNameFont lineBreakMode:UILineBreakModeWordWrap]; 
    currentPageY += size1.height; 

    NSString* year = [NSString stringWithFormat:@"Years"]; 

    CGSize size3 = [dob sizeWithFont:studentNameFont forWidth:maxWidth lineBreakMode:UILineBreakModeWordWrap]; 
    [year drawAtPoint:CGPointMake(kMargin+ classNameMaxWidth +gradeMaxWidth+ kColumnMargin, currentPageY) forWidth:maxWidth withFont:studentNameFont lineBreakMode:UILineBreakModeWordWrap]; 
    currentPageY += size3.height; 

    // draw a one pixel line under the student's name 
    CGContextSetStrokeColorWithColor(context, [[UIColor blueColor] CGColor]); 
    CGContextMoveToPoint(context, kMargin, currentPageY); 
    CGContextAddLineToPoint(context, kDefaultPageWidth - kMargin, currentPageY); 
    CGContextStrokePath(context); 

    // iterate through the list of classes and add these to the PDF. 
    NSArray* classes = [student objectForKey:@"Classes"]; 
    for(NSDictionary* class in classes) 
    { 
     NSString* className = [class objectForKey:@"Name"]; 
     NSString* grade = [class objectForKey:@"Grade"]; 
     NSString *year=[class objectForKey:@"Year"]; 

     // before we render any text to the PDF, we need to measure it, so we'll know where to render the 
     // next line. 
     size = [className sizeWithFont:classFont constrainedToSize:CGSizeMake(classNameMaxWidth, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap]; 

     // if the current text would render beyond the bounds of the page, 
     // start a new page and render it there instead 
     if (size.height + currentPageY > maxHeight) { 
      // create a new page and reset the current page's Y value 
      UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, kDefaultPageWidth, kDefaultPageHeight), nil); 
      currentPageY = kMargin; 
     } 

     // render the text 
     [className drawInRect:CGRectMake(kMargin, currentPageY, classNameMaxWidth, maxHeight) withFont:classFont lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentLeft]; 

     // print the grade to the right of the class name 
     [grade drawInRect:CGRectMake(kMargin + classNameMaxWidth + kColumnMargin, currentPageY, gradeMaxWidth, maxHeight) withFont:classFont lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentLeft]; 
     [year drawInRect:CGRectMake(kMargin +classNameMaxWidth*2+ kColumnMargin , currentPageY, yearMaxWidth, maxHeight) withFont:classFont lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentLeft]; 


     currentPageY += size.height; 

    } 


    // increment the page number. 
    currentPage++; 

} 
+0

あなたを助けて幸せ。誰かがそれを改善する方法を提案することができます '): – halfer

+0

plistのデータ私はpdfを作成しましたが、私はそのpdfの画像を追加する必要があります。 – ganesh

答えて

1

FDPFを使用できます。 http://www.fpdf.org/

マニュアルを参照してください。途中で何か問題があったら、投稿してください。

あなたが最初の研究のビットを行うと、表示するコードのビットを得ることができればそれは理想的である:-)

+0

iPadアプリケーション用にpdfを作成する必要があります – ganesh

0
CGPDFDocumentRef PDFfile; 
CFURLRef pdfURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), CFSTR("iPhoneAppProgrammingGuide.pdf"), NULL, NULL); 
    PDFfile = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL); 
    CFRelease(pdfURL); 
    CGContextRef context; 
    totalpagenumper=CGPDFDocumentGetNumberOfPages(PDFfile); 

for(i=1;i<=totalpagenumper;i++) 
    { 

     imageview=[[UIImageView alloc]initWithFrame:CGRectMake((self.frame.size.width/3),30+n,self.frame.size.width/3,160)]; 

     UILabel *lable=[[UILabel alloc]initWithFrame:CGRectMake(self.frame.size.width/1.5,n+20+self.frame.size.height/6 , 30, 20)]; 
     [scrollview addSubview:lable]; 
     lable.text=[NSString stringWithFormat:@"%i", i]; 
     lable.backgroundColor=[UIColor clearColor]; 
     lable.textAlignment=UITextAlignmentCenter; 
     n=n+30+self.frame.size.height/6; 
     imageview.userInteractionEnabled=YES; 
     //_________________________________***************************_______________**************************_____________________________ 
     totalpagenumper=CGPDFDocumentGetNumberOfPages(PDFfile); 

     CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 
     context = CGBitmapContextCreate(NULL,self.bounds.size.width,self.bounds.size.height,8,self.bounds.size.width * 4,colorSpace,kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big); 
     CGColorSpaceRelease(colorSpace); 
     CGContextClipToRect(context, CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)); 

     CGPDFPageRef page = CGPDFDocumentGetPage(PDFfile,i); 
     context = UIGraphicsGetCurrentContext(); 
     CGContextSaveGState(context); 
     CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 1.0); 
     CGContextFillRect(context,[self bounds]); 
     CGContextTranslateCTM(context, -1.0, [self bounds].size.height); 
     CGContextScaleCTM(context, 1.0, -1.0); 
     CGAffineTransform transform = aspectFit(CGPDFPageGetBoxRect(page, kCGPDFMediaBox), 
               CGContextGetClipBoundingBox(context)); 

     CGContextConcatCTM(context, transform); 

     CGContextDrawPDFPage(context, page); 

     CGContextRestoreGState(context); 
     UIGraphicsBeginImageContext(CGSizeMake(self.bounds.size.width, self.bounds.size.height)); 


     UIGraphicsEndImageContext(); 

     //_________________________________***************************_______________**************************_____________________________ 
     CGImageRef imgref; 
     imgref=CGBitmapContextCreateImage(context);  

     imageview.tag=i; 

     image= [[UIImage alloc]initWithCGImage:imgref ]; 
     imageview.image=image; 

     scrollview.backgroundColor=[UIColor orangeColor]; 
     [scrollview addSubview:imageview]; 
     tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(returnpage:)]; 
     [imageview addGestureRecognizer:tapGR];     

     [scrollview addSubview:imageview1]; 
     imageview1.frame=CGRectMake(self.frame.size.width/1.5,30, 30, 30); 
     arrow=[UIImage imageNamed:@"selectarrow.png"]; 
     imageview1.image=arrow; 

    } 

i am use this source cod to convert pdf file in to image,to change the imageview size to get large images in image view