0

Can you please help me to convert the particular PDF file page to a text file or provide me link to edit the pdf file iOS application.

Thanks in advance

Ranjith Kumar Nagiri
  • 865
  • 3
  • 18
  • 42
  • possible duplicate of [How can I get the text from PDF page?](http://stackoverflow.com/questions/9427634/how-can-i-get-the-text-from-pdf-page) – Tirth Jan 23 '14 at 07:14
  • sorry dude i dont want the text from PDF page.i need a seperate text file. – J.Chandrasekhar Reddy Jan 23 '14 at 07:28
  • Dude it give you text from pdf file and you need to write it down on text file using simple I/O operation of string read and write. I have to understand what you need initially without depending on 3rd party library or API. – Tirth Jan 23 '14 at 07:44
  • actually my requirement is to edit the PDF file is there any solution to ake a user to edit his pdf file??? – J.Chandrasekhar Reddy Jan 23 '14 at 07:45

1 Answers1

1

When you load a custom document type (doc, ppt, pdf, etc) into a UIWebView, the webview returns a nil HTML string, even via javascript. There's a few suggestions for extracting PDF text here.

But turning the string back into a PDF is different. If you want to retain the formatting of the original PDF, I'm rather sure that's impossible because NSAttributedString on iOS doesn't do much. But this will work for plain text or NSAttributedString, if its possible:

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

Happy Coding!!!

Community
  • 1
  • 1
kagmanoj
  • 5,038
  • 5
  • 19
  • 21