У меня проблема с поворотом документа PDF. Я могу повернуть документ, но после некоторых поворотов размер документа уменьшается. Я не могу найти что не так. Вот мой код:
//////// Creating document and pdfContext related code //////
-(IBAction)rotate
{
BOOL ok = [self CopyFileIfNotExists:@"Untitled.pdf"];
fileLoc = [self PathForFile:@"Untitled.pdf"];
angle = angle + 90;
pdfDoc = [self MyGetPDFDocumentRef:[fileLoc UTF8String]];
CGPDFPageRef page = CGPDFDocumentGetPage (pdfDoc, 1);
//CGRect pageRect = CGPDFPageGetBoxRect(page, kCGPDFMediaBox);
CGRect pageRect = CGRectMake(0, 0, 612, 792);
[self MyCreatePDFFile:pageRect :[fileLoc UTF8String]];
}
-(void) MyCreatePDFFile :(CGRect)pageRect :(const char *)filename
{
CFStringRef path;
CFURLRef url;
CFMutableDictionaryRef myDictionary = NULL;
path = CFStringCreateWithCString (NULL, filename, kCFStringEncodingUTF8);
url = CFURLCreateWithFileSystemPath (NULL, path,
kCFURLPOSIXPathStyle, 0);
CFRelease (path);
myDictionary = CFDictionaryCreateMutable(NULL, 0,
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);
CFDictionarySetValue(myDictionary, kCGPDFContextTitle, CFSTR("My PDF File"));
CFDictionarySetValue(myDictionary, kCGPDFContextCreator, CFSTR("My Name"));
pdfContext = CGPDFContextCreateWithURL (url, &pageRect, myDictionary);
CFRelease(myDictionary);
CFRelease(url);
CGContextBeginPage (pdfContext, &pageRect);
[self myDrawContent:pdfContext];
CGContextEndPage (pdfContext);
CGContextRelease (pdfContext);
}
-(void)myDrawContent:(CGContextRef )context
{
int noOfPages = CGPDFDocumentGetNumberOfPages(pdfDoc);
CGRect pageRect = CGRectMake(0, 0, 612, 792);
for( int i = 1 ; i <= noOfPages ; i++ )
{
CGPDFPageRef page = CGPDFDocumentGetPage (pdfDoc, i);
//pageRect = CGPDFPageGetBoxRect(page, kCGPDFMediaBox);
//[self MyDisplayPDFPage:pdfContext :i :[fileLoc UTF8String]];
[self MyDrawPDFPageInRect:pdfContext :page :kCGPDFMediaBox :pageRect :angle :true];
}
}
-(void) MyDrawPDFPageInRect:(CGContextRef)context :(CGPDFPageRef)page :(CGPDFBox)box :(CGRect)rect :(int)rotation :(bool)preserveAspectRatio
{
//////// this is rotating code of PDF ///
CGAffineTransform m;
m = CGPDFPageGetDrawingTransform (page, box, rect, rotation, preserveAspectRatio);
CGContextSaveGState (context);
CGContextConcatCTM (context, m);
CGRect pageframe = CGPDFPageGetBoxRect (page, box);
CGContextClipToRect (context,pageframe);
CGContextDrawPDFPage (context, page);
CGContextRestoreGState (context);
}
-(CGPDFDocumentRef) MyGetPDFDocumentRef: (const char *)filename
{
CFStringRef path;
CFURLRef url;
CGPDFDocumentRef document;
path = CFStringCreateWithCString (NULL, filename,kCFStringEncodingUTF8);
url = CFURLCreateWithFileSystemPath (NULL, path, kCFURLPOSIXPathStyle, 0);
CFRelease (path);
document = CGPDFDocumentCreateWithURL (url);
CFRelease(url);
int count = CGPDFDocumentGetNumberOfPages (document);
if (count == 0) {
printf("`%s' needs at least one page!", filename);
return NULL;
}
return document;
}