2012-06-12 83 views
0

我試圖將一些文本文件轉換爲pdf。現在,世界上最簡單的事情就是簡單地將文件中的所有文本合併爲一個字符串,然後使用iOS文檔here將其渲染爲單個pdf。麻煩的是,這些都是大文本文件;他們在一起可以超過90頁。因此,我需要添加一些超鏈接,以便我可以在頂部創建一個目錄,並且用戶可以快速移動到每個文本文件的開頭,而不必滾動60頁才能到達他們想要的位置。iOS - 從多個文件生成pdf

問題是,如果我將txt文件合併爲一個字符串,我無法知道每個文件在分頁時何時結束,因此我希望在最終發佈文檔之前將這些文件單獨添加到pdf中。問題是,最好只有最後一個txt文件纔會呈現,很可能是因爲它覆蓋了以前的文件。以下是我的代碼,有什麼想法?

- (void)savePDFFile:(NSString *)file_Name 
{ 

// NSArray *filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
NSString *homeDir = NSHomeDirectory(); 
NSString *saveDirectory = [NSString stringWithFormat: @"%@/%@", homeDir, @"Documents/"]; 


NSArray *fileAr = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:saveDirectory error:nil]; 
// NSString *text = @""; 
// NSMutableArray *textArray = [[NSMutableArray alloc] init]; 
NSInteger currentPage = 0; 
NSString *completeString = @""; 
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]; 
    } 
} 
for (NSString *string in fileAr) { 
    if([string hasSuffix:@"txt"]){ 
     NSString *file = [NSString stringWithFormat: @"%@/%@", saveDirectory, string]; 
     NSString *text =[NSString stringWithContentsOfFile:file encoding:NSUTF8StringEncoding error:nil]; 

// Prepare the text using a Core Text Framesetter 
CFAttributedStringRef currentText = CFAttributedStringCreate(NULL, (CFStringRef)text, NULL); 
if (currentText) { 

    CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)currentText); 
//  CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(currentText); 
    if (framesetter) { 

     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); 
     BOOL done = NO; 

     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); 


     // Release the framewetter. 
     CFRelease(framesetter); 
       CFRelease(currentText); 
    } 
} 
     // Close the PDF context and write the contents out. 
     UIGraphicsEndPDFContext(); 


    } else { 
     NSLog(@"Could not create the framesetter needed to lay out the atrributed string."); 
    } 
    // Release the attributed string. 

} 
} 


// 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, 468, 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

我所取得的進展和遇到了另一個問題,所以我問了一個額外的問題here

最終,通過我自己的測試和一點點的幫助,我解決了這個問題。