2011-07-29 19 views
5

Hola a todos, estoy trabajando en Objective-C. mi pregunta anterior fue How can I edit PDF files in an iOS application? después de buscar en Google descubrí lo siguiente. muestre el pdf en UIWebView, extraiga los datos usando C/javascript y edítelo. Todavía no estoy seguro acerca de este procedimiento. Ahora lo que tengo planeado esConvertir archivo pdf a archivo de texto

1) visualizar el pdf

2) cuando el usuario quiere editar el pdf que convertir el PDF a texto y permitir que él para editarlo

3) Tratando de salvar a Wil convertir el contenido a pdf.

¿esta es una manera fácil de proceder? im k con el paso 1. ahora cómo convierto pdf -> texto y texto -> pdf.

gracias de antemano

Respuesta

2

Cuando se carga un tipo de documento personalizado (doc, ppt, pdf, etc) en un UIWebView, la vista web devuelve una cadena HTML nula, incluso a través de JavaScript. Hay algunas sugerencias para extraer texto en PDF here.

Pero volviendo a convertir la cadena en un PDF es diferente. Si desea conservar el formato del PDF original, estoy seguro de que es imposible porque NSAttributedString en iOS no hace mucho. Pero esto funcionará para texto sin formato o NSAttributedString, si es posible:

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