Ios 如何将多个PDF文件合并为一个PDF文件

Ios 如何将多个PDF文件合并为一个PDF文件,ios,file,pdf,merge,Ios,File,Pdf,Merge,我有个大麻烦。我想将iphone应用程序中的多个PDF文件合并到tro-One PDF中。但我不能。你能帮助我吗?非常感谢如果我正确理解了这个问题,您正在尝试将一组独立的PDF文件添加到一个PDF文件中 我使用下面的代码将一个pdf文件添加到另一个文件的末尾,以创建一个pdf文件 //NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *cacheDir = [NSSearchPathForDirectoriesInD

我有个大麻烦。我想将iphone应用程序中的多个PDF文件合并到tro-One PDF中。但我不能。你能帮助我吗?非常感谢

如果我正确理解了这个问题,您正在尝试将一组独立的PDF文件添加到一个PDF文件中

我使用下面的代码将一个pdf文件添加到另一个文件的末尾,以创建一个pdf文件

//NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *cacheDir = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];

// File paths
NSString *pdfPath1 = [[NSBundle mainBundle] pathForResource:@"pdf1" ofType:@"pdf"];
NSString *pdfPath2 = [cacheDir stringByAppendingPathComponent:@"temp.pdf"];
NSString *pdfPathOutput = [cacheDir stringByAppendingPathComponent:@"out.pdf"];

// File URLs - bridge casting for ARC
CFURLRef pdfURL1 = (__bridge_retained CFURLRef)[[NSURL alloc] initFileURLWithPath:(NSString *)pdfPath1];//(CFURLRef) NSURL
CFURLRef pdfURL2 = (__bridge_retained CFURLRef)[[NSURL alloc] initFileURLWithPath:(NSString *)pdfPath2];//(CFURLRef)
CFURLRef pdfURLOutput =(__bridge_retained CFURLRef) [[NSURL alloc] initFileURLWithPath:(NSString *)pdfPathOutput];//(CFURLRef)

// File references
CGPDFDocumentRef pdfRef1 = CGPDFDocumentCreateWithURL((CFURLRef) pdfURL1);
CGPDFDocumentRef pdfRef2 = CGPDFDocumentCreateWithURL((CFURLRef) pdfURL2);

// Number of pages
NSInteger numberOfPages1 = CGPDFDocumentGetNumberOfPages(pdfRef1);
NSInteger numberOfPages2 = CGPDFDocumentGetNumberOfPages(pdfRef2);

// Create the output context
CGContextRef writeContext = CGPDFContextCreateWithURL(pdfURLOutput, NULL, NULL);

// Loop variables
CGPDFPageRef page;
CGRect mediaBox;

// Read the first PDF and generate the output pages
NSLog(@"GENERATING PAGES FROM PDF 1 (%i)...", numberOfPages1);
for (int i=1; i<=numberOfPages1; i++) {
    page = CGPDFDocumentGetPage(pdfRef1, i);
    mediaBox = CGPDFPageGetBoxRect(page, kCGPDFMediaBox);
    CGContextBeginPage(writeContext, &mediaBox);
    CGContextDrawPDFPage(writeContext, page);
    CGContextEndPage(writeContext);
}

// Read the second PDF and generate the output pages
NSLog(@"GENERATING PAGES FROM PDF 2 (%i)...", numberOfPages2);
for (int i=1; i<=numberOfPages2; i++) {
    page = CGPDFDocumentGetPage(pdfRef2, i);
    mediaBox = CGPDFPageGetBoxRect(page, kCGPDFMediaBox);
    CGContextBeginPage(writeContext, &mediaBox);
    CGContextDrawPDFPage(writeContext, page);
    CGContextEndPage(writeContext);      
}
NSLog(@"DONE!");

// Finalize the output file
CGPDFContextClose(writeContext);

// Release from memory
CFRelease(pdfURL1);
CFRelease(pdfURL2);
CFRelease(pdfURLOutput);
CGPDFDocumentRelease(pdfRef1);
CGPDFDocumentRelease(pdfRef2);
CGContextRelease(writeContext);
//NSString*documentsDirectory=[path objectAtIndex:0];
NSString*cacheDir=[NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES)lastObject];
//文件路径
NSString*pdfPath1=[[NSBundle mainBundle]pathForResource:@“pdf1”类型:@“pdf”];
NSString*pdfPath2=[cacheDir stringByAppendingPathComponent:@“temp.pdf”];
NSString*pdfPathOutput=[cacheDir stringByAppendingPathComponent:@“out.pdf”];
//文件URL-弧的桥接转换
CFURLRef pdfURL1=(uu_桥_保留CFURLRef)[[NSURL alloc]initFileURLWithPath:(NSString*)pdfPath1]//(CFURLRef)NSURL
CFURLRef pdfURL2=(uu_桥_保留的CFURLRef)[[NSURL alloc]initFileURLWithPath:(NSString*)pdfPath2]//(CFURLRef)
CFURLRef Pdforloutput=(uu_桥_保留CFURLRef)[[NSURL alloc]initFileURLWithPath:(NSString*)pdfPathOutput]//(CFURLRef)
//文件引用
CGPDFDocumentRef pdfRef1=CGPDFDocumentCreateWithURL((CFURLRef)pdfURL1);
CGPDFDocumentRef pdfRef2=CGPDFDocumentCreateWithURL((CFURLRef)pdfURL2);
//页数
NSInteger numberOfPages1=CGPDFDocumentGetNumberOfPages(pdfRef1);
NSInteger numberOfPages2=CGPDFDocumentGetNumberOfPages(pdfRef2);
//创建输出上下文
CGContextRef writeContext=CGPDFContextCreateWithURL(pdfURLOutput,NULL,NULL);
//循环变量
CGPDFPageRef页面;
CGRect媒体盒;
//阅读第一份PDF并生成输出页面
NSLog(@“从PDF 1(%i)…生成页面”,numberOfPages1);

对于(int i=1;i您想使用现有的应用程序将多个pdf文件合并为一个pdf文件,还是想创建一个新的应用程序?使用
cat
命令。@Orbling:在iOS上,嗯。:/@badcat:您会注意到一个编辑非常接近我的评论时间,该编辑添加了iOS标记,我加载问题时大概没有这个标记。PDFtk是酸的ce是可用的,它建立在OS X上,难道它不能被改编吗?@badcat:另外,有点跛脚,在一个17个月前的问题评论中提出了错误。你能检查一下这个链接吗?这样我就遇到了同样的问题
$mergedPath = '/tmp/bulkinvoice_merge_'.$vendor_id.'_'.$randomno.'.pdf';
$filepath = 'http://xyz/'.$vendor_id.'/'.$shipmentId.'_invoice_shipment_label.pdf'; 
$current = file_get_contents($filepath);
$tempPath = '/tmp/bulkinvoice_'.$vendor_id.'_'.$shipmentId.'.pdf';
chmod($tempPath, 777);
file_put_contents($tempPath,$current);
$pdf = Zend_Pdf::load($tempPath);

foreach($pdf->pages as $page){
    $pdfExtract = $ex->clonePage($page);
    $pdf2show->pages[] = $pdfExtract;
}

//$pdf2show->pages[] = $ex->clonePage($pdf->pages[0]);

echo "\n [->]Added in merged PDF : ".$shipmentId;
$pdf2show->save($mergedPath);
unlink($tempPath);