2010-07-20 11 views
6

Sono bloccato con l'analisi di un file PDF. Per favore guidami come fare questo.Come analizzare il PDF in Objective C per iPad

File di intestazione.

//PDFViewer.h 
@interface PDFViewer : UIView 
{ 
CGPDFDocumentRef pdf; 
} 

-(void)drawInContext:(CGContextRef)context; 

@end 

file di implementazione

//PDFViewer.m 
@implementation PDFViewer 


- (id)initWithFrame:(CGRect)frame 
{ 

if ((self = [super initWithFrame:frame])) 
{ 
     // Initialization code 
    if(self != nil) 
    { 
    CFURLRef pdfURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), CFSTR("WR1MayJun1S08.pdf"), NULL, NULL); 
    pdf = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL); 
    CFRelease(pdfURL); 
    } 
    } 
    return self; 
} 


-(void)drawInContext:(CGContextRef)context 
{ 
// PDF page drawing expects a Lower-Left coordinate system, so we flip the coordinate system 
// before we start drawing. 
CGContextTranslateCTM(context, 0.0, self.bounds.size.height); 
CGContextScaleCTM(context, 1.0, -1.0); 

// Grab the first PDF page 
CGPDFPageRef page = CGPDFDocumentGetPage(pdf, 1); 
// We're about to modify the context CTM to draw the PDF page where we want it, so save the graphics state in case we want to do more drawing 
CGContextSaveGState(context); 
// CGPDFPageGetDrawingTransform provides an easy way to get the transform for a PDF page. It will scale down to fit, including any 
// base rotations necessary to display the PDF page correctly. 
CGAffineTransform pdfTransform = CGPDFPageGetDrawingTransform(page, kCGPDFCropBox, self.bounds, 0, true); 
// And apply the transform. 
CGContextConcatCTM(context, pdfTransform); 
// Finally, we draw the page and restore the graphics state for further manipulations! 
CGContextDrawPDFPage(context, page); 
CGContextRestoreGState(context); 
} 

/* 
// Only override drawRect: if you perform custom drawing. 
// An empty implementation adversely affects performance during animation. 
- (void)drawRect:(CGRect)rect { 
    // Drawing code 
} 
*/ 

- (void)dealloc 
{ 
    CGPDFDocumentRelease(pdf); 
[super dealloc]; 
} 


@end 

Ora sto aggiungendo questa classe (PDFViewer.h) al mio MainViewController.

//MainViewController.m 

CGRect frame = CGRectMake(0, 200, 300, 500); 

PDFViewer *pdfViewer = [[PDFViewer alloc] initWithFrame:frame]; 
CGContextRef context = UIGraphicsGetCurrentContext(); 
[pdfViewer drawInContext:context]; 
[self.view addSubview:pdfViewer]; 

Non mostra nulla. Ottengo i seguenti errori/avvertenze:

local MultiView[2850] <Error>: CGContextTranslateCTM: invalid context 
local MultiView[2850] <Error>: CGContextScaleCTM: invalid context 
local MultiView[2850] <Error>: CGContextSaveGState: invalid context 
local MultiView[2850] <Error>: CGContextConcatCTM: invalid context 
local MultiView[2850] <Error>: CGContextRestoreGState: invalid context 

Cosa mi manca?

Saluti.

risposta

4

UIGraphicsGetCurrentContext non restituisce un contesto se non ce n'è uno, ovviamente.

Si tenta di ottenere l'inizializzazione del contesto in vista, in quel momento non è disponibile alcun contesto. Un contesto valido viene inserito nello stack poco prima che venga chiamato lo -[UIView drawRect:]. Questo dovrebbe funzionare:

//PDFViewer.m 
@implementation PDFViewer 

- (void)drawRect:(CGRect)rect { 
    [self drawInContext:UIGraphicsGetCurrentContext()]; 
} 

@end 

EDIT Benche 'non mi piace dare a nessuno copia-e-incolla-ready-code, non credo ci sia un'altra opzione a sinistra se non hai capito il mio ultimo commento Non so che cosa hai provato, ma se si cerca di capire quello che sto veramente dicendo, questo è l'unica cosa che si può trovare con:

//PDFViewer.m 
@implementation PDFViewer 

- (id)initWithFrame:(CGRect)frame 
{ 

    if (self = [super initWithFrame:frame]) 
    { 
     CFURLRef pdfURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), CFSTR("WR1MayJun1S08.pdf"), NULL, NULL); 
     pdf = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL); 
     CFRelease(pdfURL); 
    } 
    return self; 
} 

-(void)drawInContext:(CGContextRef)context 
{ 
    // PDF page drawing expects a Lower-Left coordinate system, so we flip the coordinate system 
    // before we start drawing. 
    CGContextTranslateCTM(context, 0.0, self.bounds.size.height); 
    CGContextScaleCTM(context, 1.0, -1.0); 

    // Grab the first PDF page 
    CGPDFPageRef page = CGPDFDocumentGetPage(pdf, 1); 
    // We're about to modify the context CTM to draw the PDF page where we want it, so save the graphics state in case we want to do more drawing 
    CGContextSaveGState(context); 
    // CGPDFPageGetDrawingTransform provides an easy way to get the transform for a PDF page. It will scale down to fit, including any 
    // base rotations necessary to display the PDF page correctly. 
    CGAffineTransform pdfTransform = CGPDFPageGetDrawingTransform(page, kCGPDFCropBox, self.bounds, 0, true); 
    // And apply the transform. 
    CGContextConcatCTM(context, pdfTransform); 
    // Finally, we draw the page and restore the graphics state for further manipulations! 
    CGContextDrawPDFPage(context, page); 
    CGContextRestoreGState(context); 
} 

- (void)drawRect:(CGRect)rect { 
    [self drawInContext:UIGraphicsGetCurrentContext()]; 
} 

- (void)dealloc 
{ 
    CGPDFDocumentRelease(pdf); 
    [super dealloc]; 
} 

@end 

-

//MainViewController.m 

CGRect frame = CGRectMake(0, 200, 300, 500); 

PDFViewer *pdfViewer = [[PDFViewer alloc] initWithFrame:frame]; 
[self.view addSubview:pdfViewer]; 
+0

ho fatto il seguito : - (void) drawRect: (CGRect) rect { \t CGRect frame = CG RectMake (0, 200, 400, 400); \t \t pdfViewer = [[PDFViewer alloc] initWithFrame: frame]; \t \t [pdfViewer drawInContext: UIGraphicsGetCurrentContext()]; \t [self.view addSubview: pdfViewer]; } Non mostra nulla. – TechBee

+0

No, certo che non fa nulla. Basta mantenere la parte di inizializzazione in 'MainViewController', tranne dal' CGContextRef context = ...; [pdfViewer drawInContext: context]; 'lines.Questo è il codice di disegno che chiamate all'inizializzazione, che dovrebbe essere chiamato quando è necessario disegnare la vista, che è nel metodo '-drawRect:' di 'PDFViewer'. – Joost

+0

k Potete darmi un codice di esempio. Sono dannatamente confuso :( Saluti – TechBee

3

Ho ottenuto un altro modo semplice per analizzare PDF per iPhone/iPad:
1. Prendere uno UIwebView (nome: pdfView).

2.Give IBoutlet connessione ad esso & delegarla a FilesOwner

3.In Viewdidload

[self.pdfView loadRequest:[NSURLRequest requestWithURL:[NSURL 
             fileURLWithPath:[[NSBundle mainBundle] 
             pathForResource:@"ObjC" ofType:@"pdf"]]]]; 

4.ObjC.pdf dovrebbe essere nella cartella delle risorse ..