xcode - iOS - Generating pdf from multiple files -


i attempting convert text files pdf. easiest thing in world right combine of text files single string , use ios documentation here render single pdf. trouble is, these large text files; can equal on 90 pages. therefore need add in hyperlinks can create table of contents @ top , user can move beginning of each text file rather have scroll through 60 pages want go.

problem is, if combine txt files single string, have no way of knowing when each file end while paginating, therefore wanted add files separately pdf before publishing it. problem is, @ best last txt file show rendered, because overwriting previous ones. below code, ideas?

- (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 = @""; (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 *string in filear) {     if([string hassuffix:@"txt"]){         nsstring *file = [nsstring stringwithformat: @"%@/%@", savedirectory, string];         nsstring *text =[nsstring stringwithcontentsoffile:file encoding:nsutf8stringencoding error:nil];  // prepare text using 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 pdf context using default page size of 612 x 792.         uigraphicsbeginpdfcontexttofile(pdffilename, cgrectzero, nil);           cfrange currentrange = cfrangemake(0, 0);         bool done = no;          {             // mark beginning of new page.             uigraphicsbeginpdfpagewithinfo(cgrectmake(0, 0, 612, 792), nil);              // draw page number @ bottom of each page             currentpage++;             [self drawpagenumber:currentpage];              // render current page , update current range             // point beginning of next page.             currentrange = [self renderpage:currentpage withtextrange:currentrange andframesetter:framesetter];             // if we're @ end of text, exit loop.             if (currentrange.location == cfattributedstringgetlength((cfattributedstringref)currenttext))                 done = yes;          } while (!done);           // release framewetter.         cfrelease(framesetter);                 cfrelease(currenttext);     } }            // close pdf context , write contents out.         uigraphicsendpdfcontext();       } else {         nslog(@"could not create framesetter needed lay out atrributed string.");     }     // release attributed string.  }  }   // use core text draw text in frame on page. - (cfrange)renderpage:(nsinteger)pagenum withtextrange:(cfrange)currentrange    andframesetter:(ctframesetterref)framesetter { // graphics context. cgcontextref    currentcontext = uigraphicsgetcurrentcontext();  // put text matrix known state. ensures // no old scaling factors left in place. cgcontextsettextmatrix(currentcontext, cgaffinetransformidentity);  // create path object enclose text. use 72 point // margins around text. cgrect    framerect = cgrectmake(72, 72, 468, 648); cgmutablepathref framepath = cgpathcreatemutable(); cgpathaddrect(framepath, null, framerect);  // frame rendering. // currentrange variable specifies starting point. framesetter // lays out text fit frame. ctframeref frameref = ctframesettercreateframe(framesetter, currentrange, framepath, null); cgpathrelease(framepath);  // core text draws bottom-left corner up, flip // current transform prior drawing. cgcontexttranslatectm(currentcontext, 0, 792); cgcontextscalectm(currentcontext, 1.0, -1.0);  // draw frame. ctframedraw(frameref, currentcontext);  // update current range based on drawn. currentrange = ctframegetvisiblestringrange(frameref); currentrange.location += currentrange.length; currentrange.length = 0; cfrelease(frameref);  return currentrange; } 

i made progress , ran problem, asked additional question here.

eventually, own testing , little bit of help, solved issue.


Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -