旋转 PDF 图形上下文中的所有页面

Rotate all pages in a PDF graphics context

我正在创建一个包含多个页面的 PDF 文件:

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();

将打印 PDF,但始终沿较长页面的一侧打印。所以最后在将它发送到打印机之前,我想将每一页旋转 90°。如何做到这一点?

您可以以 UIImage 格式一页一页地拍摄并将图像旋转 90 度,然后将图像束转换为 PDF。

您可以使用 UIGraphicsBeginPDFPageWithInfo(bounds, dict) 而不是 UIGraphicsBeginPDFPage; 在字典中,设置一个键 "Rotate" 和值 (NSNumber) 90。 例如:

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