2010-03-02 13 views
19

Sono di fronte ad alcuni problemi nello sviluppo di applicazioni iPhone per "Lettura PDF". Ho provato il seguente codice. So di aver usato metodi sbagliati per l'analisi - i metodi di analisi sono solo usati per scopi di ricerca. Ma voglio convertire tutto il testo pdf in una stringa. Dì ad esempio MobileHIG.pdf di Apple - ho usato in questo codice.Lettura di file PDF come stringa tramite l'applicazione iPhone

@implementation NetPDFViewController 

size_t totalPages; // a variable to store total pages 

// a method to get the pdf ref 
CGPDFDocumentRef MyGetPDFDocumentRef (const char *filename) { 
    CFStringRef path; 
    CFURLRef url; 
    CGPDFDocumentRef document; 
    path = CFStringCreateWithCString (NULL, filename,kCFStringEncodingUTF8); 
    url = CFURLCreateWithFileSystemPath (NULL, path, kCFURLPOSIXPathStyle, 0); 
    CFRelease (path); 
    document = CGPDFDocumentCreateWithURL (url);// 2 
    CFRelease(url); 
    int count = CGPDFDocumentGetNumberOfPages (document);// 3 
    if (count == 0) { 
     printf("`%s' needs at least one page!", filename); 
     return NULL; 
    } 
    return document; 
} 

// table methods to parse pdf 
static void op_MP (CGPDFScannerRef s, void *info) { 
    const char *name; 
    if (!CGPDFScannerPopName(s, &name)) 
     return; 
    printf("MP /%s\n", name); 
} 

static void op_DP (CGPDFScannerRef s, void *info) { 
    const char *name; 
    if (!CGPDFScannerPopName(s, &name)) 
     return; 
    printf("DP /%s\n", name); 
} 

static void op_BMC (CGPDFScannerRef s, void *info) { 
    const char *name; 
    if (!CGPDFScannerPopName(s, &name)) 
     return; 
    printf("BMC /%s\n", name); 
} 

static void op_BDC (CGPDFScannerRef s, void *info) { 
    const char *name; 
    if (!CGPDFScannerPopName(s, &name)) 
     return; 
    printf("BDC /%s\n", name); 
} 

static void op_EMC (CGPDFScannerRef s, void *info) { 
    const char *name; 
    if (!CGPDFScannerPopName(s, &name)) 
     return; 
    printf("EMC /%s\n", name); 
} 

// a method to display pdf page. 

void MyDisplayPDFPage (CGContextRef myContext,size_t pageNumber,const char *filename) { 
    CGPDFDocumentRef document; 
    CGPDFPageRef page; 
    document = MyGetPDFDocumentRef (filename);// 1 
    totalPages=CGPDFDocumentGetNumberOfPages(document); 
    page = CGPDFDocumentGetPage (document, pageNumber);// 2 

    CGPDFDictionaryRef d; 

    d = CGPDFPageGetDictionary(page); 

// ----- edit problem here - CGPDFDictionary is completely unknown 
// ----- as we don't know keys & values of it. 
    CGPDFScannerRef myScanner; 
    CGPDFOperatorTableRef myTable; 
    myTable = CGPDFOperatorTableCreate(); 
    CGPDFOperatorTableSetCallback (myTable, "MP", &op_MP); 
    CGPDFOperatorTableSetCallback (myTable, "DP", &op_DP); 
    CGPDFOperatorTableSetCallback (myTable, "BMC", &op_BMC); 
    CGPDFOperatorTableSetCallback (myTable, "BDC", &op_BDC); 
    CGPDFOperatorTableSetCallback (myTable, "EMC", &op_EMC); 

    CGPDFContentStreamRef myContentStream = CGPDFContentStreamCreateWithPage (page);// 3 
    myScanner = CGPDFScannerCreate (myContentStream, myTable, NULL);// 4 

    CGPDFScannerScan (myScanner);// 5 

// CGPDFDictionaryRef d; 

    CGPDFStringRef str; // represents a sequence of bytes 

    d = CGPDFPageGetDictionary(page); 

    if (CGPDFDictionaryGetString(d, "Thumb", &str)){ 
     CFStringRef s; 
     s = CGPDFStringCopyTextString(str); 
     if (s != NULL) { 
      //need something in here in case it cant find anything 
      NSLog(@"%@ testing it", s); 
     } 
     CFRelease(s);  
//  CFDataRef data = CGPDFStreamCopyData (stream, CGPDFDataFormatRaw); 
    } 

// ----------------------------------- 

    CGContextDrawPDFPage (myContext, page);// 3 
    CGContextTranslateCTM(myContext, 0, 20); 
    CGContextScaleCTM(myContext, 1.0, -1.0); 
    CGPDFDocumentRelease (document);// 4 
} 

- (void)viewDidLoad { 
    [super viewDidLoad]; 


// -------------------------------------------------------- 
// code for simple direct image from pdf docs. 
    UIGraphicsBeginImageContext(CGSizeMake(320, 460)); 
    initialPage=28; 
    MyDisplayPDFPage(UIGraphicsGetCurrentContext(), initialPage, [[[NSBundle mainBundle] pathForResource:@"MobileHIG" ofType:@"pdf"] UTF8String]); 
    imgV.image=UIGraphicsGetImageFromCurrentImageContext(); 
    imgV.image=[imgV.image rotate:UIImageOrientationDownMirrored]; 
} 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ 
    UITouch *touch = [touches anyObject]; 
    CGPoint LasttouchPoint = [touch locationInView:self.view]; 
    int LasttouchX = LasttouchPoint.x; 
    startpoint=LasttouchX; 
} 


- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ 

} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ 
    UITouch *touch = [touches anyObject]; 
    CGPoint LasttouchPoint = [touch locationInView:self.view]; 
    int LasttouchX = LasttouchPoint.x; 
    endpoint=LasttouchX; 
    if(startpoint>(endpoint+75)){ 
     initialPage++; 
     [self loadPage:initialPage nextOne:YES]; 
    } else if((startpoint+75)<endpoint){ 
     initialPage--; 
     [self loadPage:initialPage nextOne:NO]; 
    } 
} 


-(void)loadPage:(NSUInteger)page nextOne:(BOOL)yesOrNo{ 
    if(page<=totalPages && page>0){ 
     UIGraphicsBeginImageContext(CGSizeMake(720, 720)); 
     MyDisplayPDFPage(UIGraphicsGetCurrentContext(), page, [[[NSBundle mainBundle] pathForResource:@"MobileHIG" ofType:@"pdf"] UTF8String]); 

     CATransition *transition = [CATransition animation]; 
     transition.duration = 0.75; 
     transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; 
     transition.type=kCATransitionPush; 
     if(yesOrNo){ 
      transition.subtype=kCATransitionFromRight; 
     } else { 
      transition.subtype=kCATransitionFromLeft; 
     } 

     transition.delegate = self; 
     [imgV.layer addAnimation:transition forKey:nil]; 
     imgV.image=UIGraphicsGetImageFromCurrentImageContext(); 
     imgV.image=[imgV.image rotate:UIImageOrientationDownMirrored]; 
    } 
} 

Ma non ho avuto successo da leggere anche una sola riga dal documento pdf. Cosa manca ancora?

+0

Vedi questo link http://www.iphonedevsdk.com/forum/iphone-sdk-development/29770-pdf-title- keywords-label.html - contiene dettagli che leggono il file pdf e ne estrae una stringa. Link ha fornito i dettagli di - Estrazione del sommario –

+0

Se qualcuno ha bisogno di più aiuto su cosa esattamente voglio fare - può andare su questo link "http://www.random-ideas.net/posts/42" –

risposta

4

Osservare come fa l'esempio di applicazione QuartzDemo, in particolare la classe QuartzPDFView nei file QuartzImages.h e QuartzImages.m. Mostra un esempio di caricamento di un PDF tramite Quartz.

+0

Sì! Ho provato così, ho modificato di più nella mia domanda. Si prega di controllare. Voglio solo una stringa da pdf e Quartz sta dando l'immagine. –

14

Se si desidera estrarre alcuni contenuti da un file pdf, quindi si consiglia di leggere quanto segue:

Parsing PDF Content

dalla guida di programmazione Quartz 2D.

Fondamentalmente, si utilizzerà un oggetto CGPDFScanner per analizzare il contenuto, che funziona come segue. Registrate alcuni callback che verranno automaticamente richiamati da Quartz 2D quando incontrerete alcuni operatori pdf nello stream pdf. Dopo questo passaggio iniziale, inizierai effettivamente ad analizzare il flusso pdf.

Dando una breve occhiata al codice, sembra che non si stiano seguendo i passaggi necessari per analizzare il contenuto pdf della pagina che si ottiene tramite CGPDFDocumentGetPage(). È necessario prima impostare le richiamate utilizzando CGPDFOperatorTableCreate() e CGPDFOperatorTableSetCallback(), quindi si ottiene la pagina, è necessario creare un flusso di contenuti utilizzando tale pagina (utilizzando CGPDFContentStreamCreateWithPage()) e quindi creare un'istanza di CGPDFScanner tramite CGPDFScannerCreate() e avviare effettivamente la scansione tramite CGPDFScannerScan().

La sezione "Analisi del contenuto PDF" del documento indicato dall'URL sopra riportato fornisce tutte le informazioni necessarie per implementare l'analisi PDF.

Spero che questo aiuti.

+0

Ho modificato la mia domanda. - Vedi, ho già aggiunto dei metodi per questo. e anche io ho provato a scansionare ogni pagina quando si sta caricando. Ma le chiavi di CGPDFDictionary: come qualcuno può venire a conoscenza del runtime? –

+0

ho seguito il tuo consiglio ma come posso ottenere i dati scansionati? – jongbanaag

Problemi correlati