Is it possible to combine multiple pdf files into a single pdf file programmatically in iphone.I have created individual pages from different parts of my program and now nee
Here is a routine that I wrote to take a url for a pdf and append it to a context that you've already created (so that you can call it for multiple files and append them all):
- (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);
}
}
Try Quartz 2D APIs. It has very nice support for reading writing PDF files.
Please note that PDF page drawing code should be between begin page and end page calls
CGPDFDocumentCreateWithURL
CGContextBeginPage
CGPDFDocumentGetPage
CGPDFContextCreateWithURL
CGContextDrawPDFPage
CGContextEndPage
Check out Apple docs for
[CGContext][1]
[CGPDFDocument][2]
[CGPDFPage][3]
Check out if it helps, or let me know if you need some sample pseudo code.
Yes you can do that. Please follow the below code
//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();
So you have to write necessary condition of taking appropriate pages from appropriate pdf before draw the page. You have created a pdf from multiple sources!