Search code examples
iospdfcgcontextairprintcgcontextdrawpdfpage

Rotate all pages in a PDF graphics context


I'm creating a PDF file containige multiple pages:

NSMutableData *pdfData = [NSMutableData new];
CGRect rect = CGRectMake(0, 0, 300, 100);
UIGraphicsBeginPDFContextToData(pdfData, rect, nil);
for (NSInteger page = 0; page < 10; page++)
{
    UIGraphicsBeginPDFPage();
    // DRAW PAGE
}
UIGraphicsEndPDFContext();

The PDF will be printed, but always in along the longer page side. So at the end and before sending it to a printer, I want to rotate each page by 90°. How can this be done?


Solution

  • Instead of UIGraphicsBeginPDFPage, you can use UIGraphicsBeginPDFPageWithInfo(bounds, dict); In the dictionary, set a key "Rotate" and value (NSNumber) 90. EG:

    NSDictionary* dict = [NSDictionary dictionaryWithObject:[NSNumber numberWithInteger:90] forKey:@"Rotate"];
    UIGraphicsBeginPDFPageWithInfo(bounds, dict);