帮助在Quartz中创建PDF

时间:2011-09-26 17:37:43

标签: iphone pdf quartz-2d

我正在阅读适用于iOS的绘图和打印指南(http://developer.apple.com/library/ios/#documentation/2DDrawing/Conceptual/DrawingPrintingiOS/GeneratingPDF/GeneratingPDF.html#//apple_ref/doc / UID / TP40010156-CH10-SW1)。我试图修改它以获得我需要的东西。我基本上试图绘制一个彩色矩形,NSString中的文本,另一个彩色矩形,NSString中的第二个文本块。我创建了两个框架集,每个字符串一个,并调用renderPage:方法两次。但是,它在屏幕底部以颠倒的方式绘制我的文字,我不知道为什么。 (我在//评论中提出了一些问题,以了解代码为何如此工作,以及在理解为什么它不起作用时我必须缺少的东西)。谢谢!

 CFAttributedStringRef overviewText = CFAttributedStringCreate(NULL, (CFStringRef)overview, NULL);
    CFAttributedStringRef resultText = CFAttributedStringCreate(NULL, (CFStringRef)result, NULL);
    if (overviewText != NULL || resultText != NULL) {
        CTFramesetterRef overviewFramesetter = CTFramesetterCreateWithAttributedString(overviewText);
        CTFramesetterRef resultFramesetter = CTFramesetterCreateWithAttributedString(resultText);
        if (overviewFramesetter != NULL  || resultFramesetter != NULL) {
            // Create the PDF context using the default page size of 612 x 792.
            UIGraphicsBeginPDFContextToFile(filePath, CGRectZero, nil);

            CFRange currentRange = CFRangeMake(0, 0);
            NSInteger currentPage = 0;
            BOOL done = NO;

            do {
                // Mark the beginning of a new page.
                UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, PDF_WIDTH, PDF_HEIGHT), nil);

                [self drawPDFTitle:fileName];
                [self drawRectangleAtPosition:CGPointMake(MARGIN, 50)];

                // Draw a page number at the bottom of each page
                currentPage++;
                [self drawPageNumber:currentPage];

                // Render the current page and update the current range to
                // point to the beginning of the next page.
                currentRange = [self renderPage:currentPage withTextRange:currentRange andFramesetter:overviewFramesetter];
                NSLog(@"%ld, %ld", currentRange.location, currentRange.length);

            // at first I tried doing this, but would get an error at the CFRelease in renderPage method.  I'm not sure as to why I get the error since the function renderPage: seems to be the method to write the data to the PDF context.
currentRange = [self renderPage:currentPage withTextRange:currentRange andFramesetter:resultFramesetter];

                // If we're at the end of the text, exit the loop.
                if (currentRange.location == CFAttributedStringGetLength((CFAttributedStringRef)overviewText))
                    done = YES;
            } 
            while (!done);

            do {
                // I do not know why I would need to flip the context again since the method renderPage: already does this for me right?
                //CGContextTranslateCTM(UIGraphicsGetCurrentContext(), 0, PDF_HEIGHT);
                //CGContextScaleCTM(UIGraphicsGetCurrentContext(), 1.0, -1.0);
                currentRange = [self renderPage:currentPage withTextRange:currentRange andFramesetter:resultFramesetter];
                NSLog(@"2nd loop: %ld, %ld", currentRange.location, currentRange.length);
                if (currentRange.location == CFAttributedStringGetLength((CFAttributedStringRef)overviewText))
                    done = YES;
            }
            while (!done);

            // Close the PDF context and write the contents out.
            UIGraphicsEndPDFContext();

            // Release the framewetter.
            CFRelease(overviewFramesetter);

        } 

1 个答案:

答案 0 :(得分:0)

您必须转换PDF坐标,因为PDF坐标系统与石英坐标系统不同(y轴被翻转)。有关详细信息,请查看http://developer.apple.com/library/mac/#documentation/graphicsimaging/conceptual/drawingwithquartz2d/dq_overview/dq_overview.html Quartz 2D坐标系)。

您可以使用以下代码进行翻译 -

CGContextTranslateCTM(pdfContext, 0.0, pageRect.size.height);
CGContextScaleCTM(pdfContext, 1.0, -1.0);