我正在构建一个应用程序,它接受一些不同的文本文件并将它们串在一起,然后从中构建一个pdf。我需要添加文件的文本添加单独的时间,以便我可以添加到每个文件的文本的超链接。我已经取得了一些进展并且几乎准确地显示了它,但看起来CTFramesetterCreateWithAttributedString没有正确调整大小。
例如,三个文件中的第一个显示太多,换句话说,在文本结束后,会有一个空白页面。第二个文本文件显示正常,但第三个文件从一开始就缺少一大块。我不相信它与文件本身有任何关系,因为如果我颠倒文件的呈现顺序,我会遇到同样的问题。第一个文件(无论哪个文件)末尾都有一个空白页面,第三个文件的第一个页面丢失了。有谁知道造成这种现象的原因是什么?
- (void)savePDFFile:(NSString *)file_Name
{
NSString *homeDir = NSHomeDirectory();
NSString *saveDirectory = [NSString stringWithFormat: @"%@/%@", homeDir, @"Documents/"];
NSArray *fileAr = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:saveDirectory error:nil];
NSMutableArray *textArray = [[NSMutableArray alloc] init];
NSInteger currentPage = 0;
NSInteger currentFile = 0;
for (NSString *string in fileAr) {
if([string hasSuffix:@"txt"]){
NSString *file = [NSString stringWithFormat: @"%@/%@", saveDirectory, string];
NSString *text =[NSString stringWithContentsOfFile:file encoding:NSUTF8StringEncoding error:nil];
completeString = [NSString stringWithFormat:@"%@%@", completeString, text];
}
}
NSString* pdfFileName = file_Name;
// Create the PDF context using the default page size of 612 x 792.
UIGraphicsBeginPDFContextToFile(pdfFileName, CGRectZero, nil);
CFRange currentRange = CFRangeMake(0, 0);
for (NSString *string in textArray){
BOOL done = NO;
NSString *thisText = [textArray objectAtIndex:currentFile];
CFAttributedStringRef currentText = CFAttributedStringCreate(NULL, (CFStringRef)thisText, NULL);
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)currentText);
currentPage = currentPage;
do {
// Mark the beginning of a new page.
UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, 612, 792), nil);
// 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:framesetter];
// If we're at the end of the text, exit the loop.
if (currentRange.location == CFAttributedStringGetLength((CFAttributedStringRef)currentText))
done = YES;
} while (!done);
currentFile ++;
CFRelease(framesetter);
CFRelease(currentText);
}
// Close the PDF context and write the contents out.
UIGraphicsEndPDFContext();
}
// Use Core Text to draw the text in a frame on the page.
- (CFRange)renderPage:(NSInteger)pageNum withTextRange:(CFRange)currentRange
andFramesetter:(CTFramesetterRef)framesetter
{
// Get the graphics context.
CGContextRef currentContext = UIGraphicsGetCurrentContext();
// Put the text matrix into a known state. This ensures
// that no old scaling factors are left in place.
CGContextSetTextMatrix(currentContext, CGAffineTransformIdentity);
// Create a path object to enclose the text. Use 72 point
// margins all around the text.
CGRect frameRect = CGRectMake(72, 72, 500, 648);
CGMutablePathRef framePath = CGPathCreateMutable();
CGPathAddRect(framePath, NULL, frameRect);
// Get the frame that will do the rendering.
// The currentRange variable specifies only the starting point. The framesetter
// lays out as much text as will fit into the frame.
CTFrameRef frameRef = CTFramesetterCreateFrame(framesetter, currentRange, framePath, NULL);
CGPathRelease(framePath);
// Core Text draws from the bottom-left corner up, so flip
// the current transform prior to drawing.
CGContextTranslateCTM(currentContext, 0, 792);
CGContextScaleCTM(currentContext, 1.0, -1.0);
// Draw the frame.
CTFrameDraw(frameRef, currentContext);
// Update the current range based on what was drawn.
currentRange = CTFrameGetVisibleStringRange(frameRef);
currentRange.location += currentRange.length;
currentRange.length = 0;
// CFRelease(frameRef);
return currentRange;
}
答案 0 :(得分:0)
我已经弄清楚了。
我放置了“currentPage = currentPage;”在我的代码中只是为了在框架设置之后设置一个断点以便我可以观看它。尚未掌握调试器。显然,它没有引起问题,但如果我用“currentRange.location = 0;”替换该行代码,则在每次循环后重置范围。问题解决了。奇怪的是,最初是由于导致问题的currentRange的重置。
无论如何,通过上面列出的更改,以及this问题的第二个答案(减去最后四行),问题就解决了!