1

I have a PDF and I want to shrink it down to one page.

This does not work:

//pages
size_t pages = CGPDFDocumentGetNumberOfPages(document);
pageRect.size.height = pageRect.size.height*pages;
CGDataConsumerRef dataConsumer = CGDataConsumerCreateWithCFData(mutableData);
CGContextRef pdfContext = CGPDFContextCreate(dataConsumer, &pageRect, NULL);

CGPDFContextBeginPage(pdfContext, NULL);
for (int i =1; i<=pages; i++)
{
   CGPDFPageRef pageRef = CGPDFDocumentGetPage(document, i);
   CGContextDrawPDFPage(pdfContext, pageRef);
}
CGPDFContextEndPage(pdfContext);
Paresh Navadiya
  • 38,095
  • 11
  • 81
  • 132
  • 1
    What exactly does not work? – iPDFdev Jun 10 '13 at 14:11
  • thanks for your comment. iPDFdev as your solution in PDF matter are great. only last page is printed in whole PDF and i want to all pages of current PDF into one page. – Paresh Navadiya Jun 10 '13 at 14:14
  • This question is useful for other user as no question with these idea has been asked. there is no answer for this type of question. But credit goes to @iPDFDev. Please open this question. – Paresh Navadiya Jun 12 '13 at 13:45
  • please guide me on http://stackoverflow.com/questions/18396558/how-to-merge-pdf-file-with-annotation – kirti Chavda Sep 10 '13 at 06:41

1 Answers1

2

Your code prints all the pages at the same location, one on top of another. If by any chance the pages have an explicit white background then you will see only the last page.
The solution is to translate the coordinate system after the page is drawn with the height of the page that has been drawn.
UPDATE: This is the complete code. It assumes that all pages in the source file have the same size and rotation:

NSURL *pdfURL = [[NSBundle mainBundle] URLForResource:@"source.pdf" withExtension:nil];
pdf = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL);

int pageCount = CGPDFDocumentGetNumberOfPages(pdf);
CGPDFPageRef pageRef = CGPDFDocumentGetPage(pdf, 1);
CGRect pageRect = CGPDFPageGetBoxRect(pageRef, kCGPDFMediaBox);
float pageHeight = pageRect.size.height;
pageRect.size.height = pageRect.size.height * pageCount;

NSMutableData* pdfData = [[NSMutableData alloc] init];
CGDataConsumerRef pdfConsumer = CGDataConsumerCreateWithCFData((CFMutableDataRef)pdfData);
CGContextRef pdfContext = CGPDFContextCreate(pdfConsumer, &pageRect, NULL);

CGPDFContextBeginPage(pdfContext, NULL);
CGContextTranslateCTM(pdfContext, 0, pageRect.size.height);
for (int i = 1; i <= pageCount; i++) {
    pageRef = CGPDFDocumentGetPage(pdf, i);
    CGContextTranslateCTM(pdfContext, 0, -pageHeight);
    CGContextDrawPDFPage(pdfContext, pageRef);
}
CGPDFContextEndPage(pdfContext);
CGPDFContextClose(pdfContext);

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pdfFile = [documentsDirectory stringByAppendingPathComponent:@"destination.pdf"];

[pdfData writeToFile: pdfFile atomically: NO];
[pdfData release];
iPDFdev
  • 5,229
  • 2
  • 17
  • 18