0

I am trying to create a PDF file programmatically in Objective-C and as my data keeps getting larger and larger I need to continue drawing it on the second page. By using the next code I am able to create the necessary number of pages but the content is drawed only on the first one. Any suggestions on what should I change?

+(void)drawPDF:(NSString*)fileName
{
    // Create the PDF context using the default page size of 612 x 792.
    UIGraphicsBeginPDFContextToFile(fileName, CGRectZero, nil);
    // Mark the beginning of a new page.
    UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, 612, 792), nil);

    int currentPage = 1;

    for (int count = 30; count < 3000;  count = count + 100) {

        //add new PDF page if the content doesn't fits on the curent one
        if (currentPage * 792 < count + 30) {
            currentPage ++;
            UIGraphicsBeginPDFPage();
        }

        [self drawCellFromStartingPoint:count];
    }

    // Close the PDF context and write the contents out.
    UIGraphicsEndPDFContext();
}
Laur Stefan
  • 1,519
  • 5
  • 23
  • 50

2 Answers2

0
  UIGraphicsBeginPDFContextToFile(...); 
  UIGraphicsBeginPDFPageWithInfo(...); // start first page

  UIGraphicsBeginPDFPageWithInfo(...); // start second page

  UIGraphicsEndPDFContext();

refer this answer for more details.

Hope this will help :)

Community
  • 1
  • 1
Ketan Parmar
  • 27,092
  • 9
  • 50
  • 75
  • As I said the problem is not that I can not add the pages, but that, the data is not switching the pages when it draws. – Laur Stefan May 25 '16 at 13:36
0

The issue was the I was continuing drawing the data fro the current calculated Y position(count value from the for-loop), so in order for it to work properly I had to reset the Y value on each page, so in the end I adjusted the line:

[self drawCellFromStartingPoint:count];

to work as:

[self drawCellFromStartingPoint:count - ((currentPage - 1) * 792)];
Laur Stefan
  • 1,519
  • 5
  • 23
  • 50