带石英的PDF页面

Inf*_*ies 1 iphone objective-c quartz-graphics

这是我从pdf文档中获取页面并使用每个页面创建pdf文档的代码:

- (void)getPages {
NSString *pathToPdfDoc = [[NSBundle mainBundle] pathForResource:@"security" ofType:@"pdf"];
NSURL *pdfUrl = [NSURL fileURLWithPath:pathToPdfDoc];

CGPDFDocumentRef document = CGPDFDocumentCreateWithURL((CFURLRef)pdfUrl);

size_t numberOfPages = CGPDFDocumentGetNumberOfPages(document);

for (size_t i = 0; i < numberOfPages; i++) {
    CGPDFPageRef page = CGPDFDocumentGetPage(document, 0);

    CGRect pageRect = CGPDFPageGetBoxRect(page, kCGPDFMediaBox);

    NSString *filename = [NSString stringWithFormat:@"./security%d.pdf", i];

    CFStringRef path = CFStringCreateWithCString (NULL, [filename UTF8String], kCFStringEncodingUTF8);
    CFURLRef url = CFURLCreateWithFileSystemPath (NULL, path, kCFURLPOSIXPathStyle, 0);
    CFRelease (path);

    CFMutableDictionaryRef myDictionary = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
    CFDictionarySetValue(myDictionary, kCGPDFContextTitle, CFSTR("My PDF File"));
    CFDictionarySetValue(myDictionary, kCGPDFContextCreator, CFSTR("My Name"));

    CGContextRef pdfContext = CGPDFContextCreateWithURL(url, &pageRect, myDictionary);
    CFRelease(myDictionary);
    CFRelease(url);

    CGContextBeginPage(pdfContext, &pageRect);
    MyDrawPDFPageInRect(pdfContext, page, kCGPDFMediaBox, pageRect, 0, true);
    CGContextEndPage (pdfContext);
    CGContextRelease (pdfContext);
}

CGPDFDocumentRelease(document);
}

void MyDrawPDFPageInRect(CGContextRef context, CGPDFPageRef page, CGPDFBox box, CGRect rect, int rotation, bool preserveAspectRatio) {
CGAffineTransform m;

m = CGPDFPageGetDrawingTransform(page, box, rect, rotation, preserveAspectRatio);
CGContextSaveGState(context);
CGContextConcatCTM(context, m);
CGContextClipToRect(context, CGPDFPageGetBoxRect (page, box));
CGContextDrawPDFPage(context, page);
CGContextRestoreGState(context);
}
Run Code Online (Sandbox Code Playgroud)

问题是页面出现在文件系统中,但它们是空的.知道为什么会这样吗?

wal*_*lky 7

PDF页面的编号为1.所以这一行:

    CGPDFPageRef page = CGPDFDocumentGetPage(document, 0);
Run Code Online (Sandbox Code Playgroud)

给你一个空页面,它反过来给你一个空的页面矩形,从那时起你就什么都没画了.而且,每次都没有相同的东西,因为你总是得到第0页而不是在页面中移动.

你想要的是使循环变量从1开始,并使用它来获取页面:

for ( size_t i = 1; i <= numberOfPages; i++ ) {
    CGPDFPageRef page = CGPDFDocumentGetPage(document, i);
Run Code Online (Sandbox Code Playgroud)

我认为其他一切都应该按原样运作.