Search code examples
objective-cioscore-graphicscore-text

Multi page PDF with CoreText


I'm using CoreText to produce a PDF document which contains a series of NSAttributedString's so I create a CTFrameSetterRef and give it a frame covering the whole page and then loop through using CTFrameDraw to draw the text checking the result of CTFrameGetVisibleStringRange to detect when to start a new page. This works great but how can I tell where the text ended? For example the text may stop halfway down the final page and I want to draw some images below. How can I get the position the text ended at?

CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)notesAttrString);
do {
    CTFrameRef frame = CTFramesetterCreateFrame(framesetter, currentRange, path, NULL);
    if( frame ) {
        CTFrameDraw(frame, context);

        if( currentRange.location < CFAttributedStringGetLength((CFAttributedStringRef)notesAttrString) ) {
             UIGraphicsBeginPDFPage();
        } else {
            complete = YES;
        }
    }
} while( !complete );

(Above code is cut down for illustration of the process and not complete)


Solution

  • You can either use CTFrameGetLineOrigins and check the origin of the last line, or CTFramesetterSuggestFrameSizeWithConstraints to get the size of the entire frame.