如何在iOS中以编程方式生成包含多个图像阵列的多页pdf文件?

时间:2014-12-08 05:36:27

标签: ios iphone pdf-generation

嗨我需要生成一个包含多个图像阵列的多个页面的pdf文件,任何人都可以帮我提供示例代码吗?

3 个答案:

答案 0 :(得分:4)

我略微改变了之前的解决方案。此方法接受新文件的名称和图像数组,并返回pdf的绝对路径。

- (NSString *)createPdfWithName: (NSString *)name array:(NSArray*)images {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docspath = [paths objectAtIndex:0];
    NSString *pdfFileName = [docspath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.pdf",name]];
    UIGraphicsBeginPDFContextToFile(pdfFileName, CGRectZero, nil);
    for (int index = 0; index <[images count] ; index++) {
        UIImage *pngImage=[images objectAtIndex:index];;
        UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, (pngImage.size.width), (pngImage.size.height)), nil);
        [pngImage drawInRect:CGRectMake(0, 0, (pngImage.size.width), (pngImage.size.height))];
    }
    UIGraphicsEndPDFContext();
    return pdfFileName;
}

答案 1 :(得分:3)

您可以使用这些方法生成包含图像的PDF文件:

- (void) generatePdfWithFilePath: (NSString *)thefilePath
{
    NSArray *pathsImage = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docspath = [pathsImage objectAtIndex:0];
    NSString *dataPath = [docspath stringByAppendingPathComponent:folder];
    NSString *imagepath=[dataPath stringByAppendingPathComponent:imageName];

    UIGraphicsBeginPDFContextToFile(thefilePath, CGRectZero, nil);
    NSInteger currentPage = 0;
    BOOL done = NO;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *folder=[NSString stringWithFormat:@"ImageFolder"];
    NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:folder];

    UIImage *imgBrush = [UIImage imageWithContentsOfFile:imagepath];

    for (int i=0; i<directoryContent.count; i++) {

        [self drawImage:imgBrush];

    }
}


- (void) drawImage :(UIImage*) imgBrush
{
    if (imgBrush.size.width > pdfpageSize.width || imgBrush.size.height > pdfpageSize.height) {
        [imgBrush drawInRect:CGRectMake((pdfpageSize.width/2)-(imgBrush.size.width/4), (pdfpageSize.height/2)-(imgBrush.size.height/4), imgBrush.size.width/2, imgBrush.size.height/2)];
    } else {
        [imgBrush drawInRect:CGRectMake((pdfpageSize.width/2)-(imgBrush.size.width/2), (pdfpageSize.height/2)-(imgBrush.size.height/2), imgBrush.size.width, imgBrush.size.height)];
    }

}

嘿,这是另一种方法,您可以使用它通过代码在PDF中呈现图像:

-(void)drawImagesToPdf:(UIImageView *)button

{
    CGSize pageSize = CGSizeMake(button.frame.size.width*5, button.frame.size.height*5+30);
    NSLog(@"page size %@",NSStringFromCGSize(pageSize));


    NSString *fileName = @"Demo.pdf";
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *pdfFileName = [documentsDirectory stringByAppendingPathComponent:fileName];
    UIGraphicsBeginPDFContextToFile(pdfFileName, CGRectMake(0, 0, button.frame.size.width, button.frame.size.height*3), nil);

    UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0.0, pageSize.width, pageSize.height), nil);

    NSArray *arrImages = [NSArray arrayWithObjects:@"3.png", @"4.png", @"5.png", @"6.png", nil];
    float y = 220.0;

    for (int i=0; i<arrImages.count; i++) {
        UIImage * myPNG = [UIImage imageNamed:[arrImages objectAtIndex:i]];

        [myPNG drawInRect:CGRectMake(50.0, y, myPNG.size.width, myPNG.size.height)];

        y += myPNG.size.height + 20;
    }

    UIGraphicsEndPDFContext();
}

试试我的更新代码......

希望这对你有帮助... 一切顺利.. !!!

答案 2 :(得分:0)

制作一系列实际图像而非名称。

NSMutableArray *arr=[[NSMutableArray alloc]initWithObjects:[UIImage imageNamed:@"image1.png"],[UIImage imageNamed:@"image2.png"],nil];

使用此选项并确保您的应用程序包中包含名称为image1.png和image2.png

的图像