gpt4 book ai didi

iphone - 旋转PDF文档,一段时间后缩小

转载 作者:行者123 更新时间:2023-12-03 21:06:32 25 4
gpt4 key购买 nike

我在旋转 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;
}

最佳答案

经过大量搜寻,我找到了 Sorin (iPDFdev) rotation code成为最好的。它负责所有四个方向。

switch (rotate) {
case 0:
CGContextTranslateCTM(context, 0, cropBox.size.height);
CGContextScaleCTM(context, 1, -1);
break;
case 90:
CGContextScaleCTM(context, 1, -1);
CGContextRotateCTM(context, -M_PI / 2);
break;
case 180:
case -180:
CGContextScaleCTM(context, 1, -1);
CGContextTranslateCTM(context, cropBox.size.width, 0);
CGContextRotateCTM(context, M_PI);
break;
case 270:
case -90:
CGContextScaleCTM(context, 1, -1);
CGContextTranslateCTM(context, cropBox.size.height, cropBox.size.width);
CGContextRotateCTM(context, M_PI / 2);
break;
}

其中rotateCGPDFPageGetRotationAngle的结果,cropBox来自CGPDFPageGetBoxRect。您可以从博客文章中获取更多信息。

关于iphone - 旋转PDF文档,一段时间后缩小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6531889/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com