是否可以在iphone中以编程方式将多个pdf文件合并为一个pdf文件.我已经从程序的不同部分创建了单独的页面,现在需要将它们合并到一个文件中
是的,你可以这样做.请按照以下代码
//Open a pdf context for the single file
UIGraphicsBeginPDFContextToFile(oldFile, paperSize, nil);
//Run a loop to the number of pages you want
for (pageNumber = 1; pageNumber <= count; pageNumber++)
{
//Open a pdf page context
UIGraphicsBeginPDFPageWithInfo(paperSize, nil);
//Get graphics context to draw the page
CGContextRef currentContext = UIGraphicsGetCurrentContext();
//Flip and scale context to draw the pdf correctly
CGContextTranslateCTM(currentContext, 0, paperSize.size.height);
CGContextScaleCTM(currentContext, 1.0, -1.0);
//Get document access of the pdf from which you want a page
CGPDFDocumentRef newDocument = CGPDFDocumentCreateWithURL ((CFURLRef) newUrl);
//Get the page you want
CGPDFPageRef newPage = CGPDFDocumentGetPage (newDocument, pageNumber);
//Drawing the page
CGContextDrawPDFPage (currentContext, newPage);
//Clean up
newPage = nil;
CGPDFDocumentRelease(newDocument);
newDocument = nil;
newUrl = nil;
}
UIGraphicsEndPDFContext();
Run Code Online (Sandbox Code Playgroud)
所以你必须在绘制页面之前写下从适当的pdf中获取适当页面的必要条件.您已从多个来源创建了PDF文件!
这是我编写的一个例程,用于获取pdf的URL并将其附加到您已经创建的上下文中(这样您就可以将其调用为多个文件并将它们全部追加):
- (void)appendPdfAtURL:(NSURL *)pdfURL toContext:(CGContextRef)pdfDestinationContext {
CGPDFDocumentRef pdfDoc = CGPDFDocumentCreateWithURL((__bridge_retained CFURLRef)pdfURL);
if (pdfDoc) {
size_t numPages = CGPDFDocumentGetNumberOfPages(pdfDoc);
if (numPages > 0) {
// Loop through each page in the source file
for (size_t i = 1; i <= numPages; i++) {
CGPDFPageRef pdfPage = CGPDFDocumentGetPage(pdfDoc, i);
if (pdfPage) {
// Get the page size
CGRect pdfCropBoxRect = CGPDFPageGetBoxRect(pdfPage, kCGPDFMediaBox);
// Copy the page from the source file to the context
CGContextBeginPage(pdfDestinationContext, &pdfCropBoxRect);
CGContextDrawPDFPage(pdfDestinationContext, pdfPage);
}
}
}
// Close the source file
CGPDFDocumentRelease(pdfDoc);
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4604 次 |
最近记录: |