【发布时间】:2012-03-12 13:36:04
【问题描述】:
我正在尝试将 11 个 pdf 文件合并为一个 pdf 文件。我正在使用以下代码,但在最终的 pdf 中仅显示第一个 pdf ...我在循环中记录了 pdfurls 和 CGPDFDocumentRef,它们是不是一直为零(在循环中)。最终文档中仅显示第一页的原因可能是什么
-(void)mergeDocuments
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
NSString *oldFile=[documentsDirectory stringByAppendingPathComponent:@"finalPdf.pdf"];
NSMutableData *data=[[NSMutableData alloc] init];
CGRect paperSize=CGRectMake(0,0,kDefaultPageWidth,kDefaultPageHeight);
UIGraphicsBeginPDFContextToData(data, paperSize, nil);
for (int pageNumber = 1; pageNumber <= 11; pageNumber++)
{
NSString *pdfPath = [[documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"page_%d.pdf",pageNumber]] retain];
NSURL *pdfUrl = [[NSURL fileURLWithPath:pdfPath] retain];
UIGraphicsBeginPDFPageWithInfo(paperSize, nil);
CGContextRef currentContext = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(currentContext, 0, paperSize.size.height);
CGContextScaleCTM(currentContext, 1.0, -1.0);
CGPDFDocumentRef newDocument = CGPDFDocumentCreateWithURL ((CFURLRef) pdfUrl);
CGPDFPageRef newPage = CGPDFDocumentGetPage (newDocument, pageNumber);
CGContextDrawPDFPage (currentContext, newPage);
newPage = nil;
CGPDFDocumentRelease(newDocument);
newDocument = nil;
[pdfUrl release];
}
NSURL *finalUrl=[NSURL URLWithString:oldFile];
UIGraphicsEndPDFContext();
[data writeToURL:finalUrl atomically:YES];
}
【问题讨论】:
-
尝试使用 CGContextBeginPage 而不是 UIGraphicsBeginPDFPageWithInfo 创建页面
-
@lukya 试过了,但没有帮助
-
你用什么来查看pdf并确定它只有一页?
-
@Inafziger 我正在对我的应用程序的文档文件夹进行操作,并使用 adobe reader 打开 pdf 文档。
-
好的,我在适用于 iOS 的 pdf 阅读器上遇到了同样的问题,结果证明是一个错误,阅读器只显示第一页(即使它们都在那里)。