【发布时间】:2015-10-29 18:37:37
【问题描述】:
我在连接 PDF 时遇到问题。 我必须连接 2 个 pdf 文件。让我们称第一个 pdf 文件为“pdf1”,第二个为“pdf2”,它总是只有一页。 为了连接它们,我使用了下面的函数,但是下面的函数为了连接 pdf1 和 pdf2 创建了一个新的 pdf 文件(让我们称之为 pdf3)并继续在 pdf3 上添加来自 pdf1 和 pdf2 的所有页面。 我想知道的是,有没有一种方法可以在不创建 pdf3 的情况下在 pdf1 上连接 pdf2。
-(void) concatenaPDF:(NSNumber*)paginaSendoBaixada{
NSString *cacheDir = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
// File paths
cacheDir = [cacheDir stringByAppendingPathComponent:@"PDFs"];
NSString *pdfPathOutput = [cacheDir stringByAppendingPathComponent:@"out.pdf"];
// File URLs - bridge casting for ARC
NSURL *pdfURL1 = [[NSURL alloc] initFileURLWithPath:self.filePath];
NSURL *pdfURL2 = [[NSURL alloc] initFileURLWithPath:self.filePathPagina];
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);
//se pagina do pdfbaxado for 0 e pq deu erro na baixa e vou ter que baixar denovo.
if (numberOfPages2 == 0) {
self.modeloParaItemSendoBaixado.model.paginaBaixando--;
//vou apagar o pdf da pagina
[self deleteFileAtPath:self.filePathPagina];
return;
}
if (numberOfPages1 > [paginaSendoBaixada integerValue]) {
[self clearState];
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"Erro"
message:@"Falha na conexão.\nNão foi possível baixar a edição!"
delegate:nil
cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
});
}
// 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 (%li)...", (long)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 (%li)...", (long)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
CGPDFDocumentRelease(pdfRef1);
CGPDFDocumentRelease(pdfRef2);
CGContextRelease(writeContext);
[self deleteFileAtPathWithoutImage:self.filePath];
NSFileManager *fileMan = [NSFileManager defaultManager];
NSError *error = nil;
if (self.filePath != nil) {
if (![fileMan moveItemAtPath:pdfPathOutput toPath:self.filePath error:&error])
{
[self clearState];
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"Erro"
message:@"Falha na conexão.\nNão foi possível baixar a edição!"
delegate:nil
cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
});
NSLog(@"Failed to move '%@' to '%@': %@", pdfPathOutput, self.filePath, [error localizedDescription]);
}
}
}
【问题讨论】:
标签: ios objective-c pdf