Ipad 使用图像创建pdf

Ipad 使用图像创建pdf,ipad,pdf,Ipad,Pdf,我正在开发一个应用程序,其中我必须从plist创建多页PDF数据,并通过电子邮件发送此类PDF。要转换为PDF的内容包含文本和一些图像。我看过一些关于这方面的帖子,但从中收集不到太多。如果有人能在这方面提供指导,那就太好了 这是我创建pdf的代码 // create some sample data. In a real application, this would come from the database or an API. NSString* path = [[NSBundle m

我正在开发一个应用程序,其中我必须从plist创建多页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++;

}
您可以使用FDPF。

请看一下手册。如果你在路上遇到任何问题,请发帖

很高兴帮助您:-)

CGPDFDocumentRef PDFfile;
CFURLRef pdfURL=CFBundleCopyResourceURL(CFBundleGetMainBundle(),CFSTR(“iPhoneAppProgrammingGuide.pdf”),NULL,NULL);
PDFfile=CGPDFDocumentCreateWithURL((CFURLRef)pdfURL);
CFR释放(pdfURL);
CGContextRef上下文;
totalpagenumper=CGPDFDocumentGetNumberOfPages(PDFfile);

对于(i=1;如果您可以先做一些研究,并获得一些代码来显示,这将是理想的。然后有人可以建议改进它的方法
:)
来自plist的数据我已经创建了pdf,但我需要在该pdf中添加图像。我需要为iPad应用程序创建pdf
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