2

My app creates a pdf page from app data, using Quartz and UIGraphics. Basically I define a CGRect docRect to fit on that page, then increment an NSInteger yOffset each time I draw something. If yOffset gets larger than docRect.size.height I do a UIGraphicsBeginPDFPage(). This works fine, BUT:

I would like to draw a page count at the bottom, like "Page X of Y".

X is obviously easy to tell, but at the moment when I create a new page, I don't know how large Y might be.

I see 2 possible solutions for this:

  1. After drawing all pages, reiterate through all pages and add the counter. Problem: As far as I can tell, after calling UIGraphicsBeginPDFPage() there is no way to return back to previous pages. Is anyone able to disconfirm that?

  2. Calculate all yOffset increments in advance to get the total page count. Possible, but IMHO not really an elegant solution.

Has anyone advice on this problem?

Thanks in advance, m

marimba
  • 3,116
  • 5
  • 26
  • 29
  • I am currently facing a similar problem. Please post if you found a work-around, or anyone who has any suggestions! – Chris May 01 '12 at 05:41

3 Answers3

4

If you don't want to pre-calculate the sizes of everything to determine in advance how many pages you'll have, one option is to render in two passes, much like you mention in your question. Render the first pass to temp.pdf and leave room for the page number counter.

If working through the source content is a memory or performance burden, the second pass can go to final.pdf, using temp.pdf as an input. Loop through all the pages of temp.pdf, using CGPDFDocumentGetNumberOfPages to get your page count. Draw each page from temp.pdf via CGContextDrawPDFPage, followed by drawing the page counter.

When you're done, delete temp.pdf as cleanup, and you're all set.

Update: Something along the lines of this untested code, summarized from some previous work:

...
CGPDFDocumentRef tempDocRef = CGPDFDocumentCreateWithURL((CFURLRef)tempPdfUrl);
size_t pageCount = CGPDFDocumentGetNumberOfPages(docRef);
CGContextRef finalPdfContext = CGPDFContextCreateWithURL((CFURLRef)finalPdfUrl, &defaultPageRect, nil);

for (int pageNum = 1; pageNum <= pageCount; pageNum++) {
  CGPDFPageRef tempPageRef = CGPDFDocumentGetPage(tempDocRef, pageNum+1);
  CGPDFContextBeginPage(finalPdfContext, nil);
  CGContextDrawPDFPage(finalPdfContext, tempPageRef);

  // do your page number rendering here, using pageNum and pageCount
  ...

  CGPDFContextEndPage(finalPdfContext);
  CGPDFPageRelease(tempPageRef);
}

CGPDFDocumentRelease(tempDocRef);
CGContextRelease(finalPdfContext);
...
MattP
  • 1,920
  • 1
  • 15
  • 22
  • I understand that's how it should be done in theory, but unless I'm missing something, there's no CoreGraphics API for opening an existing PDF file. There is just the UIGraphicsBeginPDFContextToData and UIGraphicsBeginPDFContextToFile methods. To do a 2-pass approach, we would need some kind of UIGraphicsOpenPDFContextFromFile method, or something similar. – Chris May 07 '12 at 04:19
  • I updated my post to include some code that shows how to work with a PDF from CoreGraphics, which offers more than UIKit. – MattP May 07 '12 at 19:00
0

Syncfusion's Essential PDF provides a solution to this problem. It provides solution for the problem in two situation

  1. While generating the PDF Document.
  2. After the generation of the PDF document

You can try the online sample in the link http://mvc.syncfusion.com/sfreportingmvcsamplebrowser/MVC/10.1.0.44/Pdf_MVC/Samples/4.0/Settings/HeadersFooters

-Suresh

suresh
  • 177
  • 2
  • 14
-1

Better late than never - If this is still something your looking for an answer to, I think you might be interested in checking out this link - http://developer.apple.com/library/ios/#documentation/2DDrawing/Conceptual/DrawingPrintingiOS/GeneratingPDF/GeneratingPDF.html#//apple_ref/doc/uid/TP40010156-CH10-SW1 - Apple provides a (void)drawPageNumber:(NSInteger)pageNum to accomplish this task.