我正在构建一个应用程序,它需要几个不同的文本文件并将它们串在一起,然后用它们构建一个 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;
}