2011-07-29 38 views
5

Witam wszystkich pracuję nad Objective-C. moje poprzednie pytanie było How can I edit PDF files in an iOS application? po dużo googling dowiedziałem się następujące. wyświetl pdf w UIWebView, wyodrębnij dane za pomocą C/javascript i edytuj je. Nadal nie jestem pewien co do tej procedury. teraz co mam zaplanowane jestKonwersja pliku PDF do pliku tekstowego

1) wyświetlić pdf

2), gdy użytkownik chce edytować pdf I konwertowanie pdf do tekstu i pozwolić mu ją

3 edycji) próbują uratować Wil przekonwertuj treść z powrotem na pdf.

czy to jest dobry sposób na kontynuowanie? im k z krokiem 1. teraz jak konwertować pdf -> tekst i tekst -> pdf.

góry dzięki

Odpowiedz

2

Podczas ładowania niestandardowego typu dokumentów (doc, ppt, pdf, itp) do UIWebView The WebView zwraca nil HTML ciąg, nawet za pośrednictwem javascript. Istnieje kilka sugestii dotyczących wyodrębniania tekstu w formacie PDF: here.

Ale zamianę ciągu znaków w plik PDF jest inny. Jeśli chcesz zachować formatowanie oryginalnego pliku PDF, jestem raczej pewien, że to niemożliwe, ponieważ NSAttributedString na iOS nie robi wiele. Ale to zadziała w przypadku zwykłego tekstu lub NSAttributString, jeśli jest to możliwe:

NSData *PDFDataFromString(NSString *str) { 
    NSMutableData *data = [NSMutableData data]; 

    //Create an NSAttributedString for CoreText. If you find a way to translate 
    //PDF into an NSAttributedString, you can skip this step and simply use an 
    //NSAttributedString for this method's argument. 

    NSAttributedString* string = [[[NSAttributedString alloc] initWithString:str] autorelease]; 

    //612 and 792 are the dimensions of the paper in pixels. (8.5" x 11") 
    CGRect paperRect = CGRectMake(0.0, 0.0, 612, 792); 

    CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef) string); 
    CGSize requiredSize = CTFramesetterSuggestFrameSizeWithConstraints(framesetter, CFRangeMake(0, [string length]), NULL, CGSizeMake(paperRect.size.width - 144, 1e40), NULL); 

    //Subtract the top and bottom margins (72 and 72), so they aren't factored in page count calculations. 
    NSUInteger pageCount = ceill(requiredSize.height/(paperRect.size.height - 144)); 
    CFIndex resumePageIndex = 0; 
    UIGraphicsBeginPDFContextToData(data, paperRect, nil); 

    for(NSUInteger i = 0; i < pageCount; i++) 
    { 

    //After calculating the required number of pages, break up the string and 
    //draw them into sequential pages. 

     UIGraphicsBeginPDFPage(); 
     CGContextRef currentContext = UIGraphicsGetCurrentContext(); 
     CGContextSaveGState (currentContext); 
     CGContextSetTextMatrix(currentContext, CGAffineTransformIdentity); 
     CGMutablePathRef framePath = CGPathCreateMutable(); 

     //72 and 72 are the X and Y margins of the page in pixels. 
     CGPathAddRect(framePath, NULL, CGRectInset(paperRect, 72.0, 72.0)); 

     CTFrameRef frameRef = CTFramesetterCreateFrame(framesetter, CFRangeMake(resumePageIndex, 0), framePath, NULL); 
     resumePageIndex += CTFrameGetVisibleStringRange(frameRef).length; 
     CGPathRelease(framePath); 
     CGContextTranslateCTM(currentContext, 0, paperRect.size.height); 
     CGContextScaleCTM(currentContext, 1.0, -1.0); 
     CTFrameDraw(frameRef, currentContext); 
     CFRelease(frameRef);  
     CGContextRestoreGState (currentContext); 
    } 
    CFRelease(framesetter); 
    UIGraphicsEndPDFContext(); 
    return data; 
}