La lecture des fichiers PDF au format de la chaîne par le biais d'une application iPhone
Je suis confronté à un problème dans le développement d'une application iPhone pour la "Lecture PDF".
J'ai essayé le code suivant. Je sais que j'ai mal utilisé des méthodes d'analyse - analyse de méthodes sont utilisés uniquement pour la recherche de but. Mais je veux convertir fichier pdf entier de texte dans une chaîne de caractères. Disons, par exemple, Apple MobileHIG.pdf - j'ai utilisé dans le présent code.
@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];
}
}
Mais je n'ai pas eu de succès à même de lire une seule ligne dans le document pdf.
Ce qui manque encore?
- Voir ce lien iphonedevsdk.com/forum/iphone-sdk-development/... - elle a des détails qui lire fichier pdf & extraits de chaîne à partir d'elle. Lien a donné les détails de l'Extraction de la Table des matières
Vous devez vous connecter pour publier un commentaire.
j'ai une bibliothèque qui peut faire cette chose précise liée par ici : https://bitbucket.org/zachron/pdfiphone/overview
Si vous voulez extraire le contenu d'un fichier pdf, vous pouvez lire la suite de:
L'analyse de Contenu PDF
de Quartz 2D guide de programmation.
Fondamentalement, vous utilisez un
CGPDFScanner
objet à analyser le contenu, qui fonctionne comme suit. Vous vous inscrivez en quelques rappels qui sera automatiquement appelée par Quartz 2D lors de la rencontre de certains pdf opérateurs dans le pdf flux. Après cette première étape, vous avez alors le fait de commencer l'analyse de la pdf flux.Un bref regard à votre code, il semble que vous ne suivez pas les étapes nécessaires pour analyser le contenu du fichier pdf de la page que vous obtenez par le biais de
CGPDFDocumentGetPage()
. Vous devez d'abord configurer les rappels à l'aide deCGPDFOperatorTableCreate()
etCGPDFOperatorTableSetCallback()
, alors vous obtenez la page, vous devez créer un flux de contenu à l'aide de cette page (à l'aide deCGPDFContentStreamCreateWithPage()
) et puis instancier uneCGPDFScanner
parCGPDFScannerCreate()
et fait démarrer le balayage deCGPDFScannerScan()
.L' "Analyse de Contenu PDF" section du document pointé par l'URL ci-dessus vous donne toutes les informations nécessaires pour mettre en œuvre pdf de l'analyse.
Espère que cette aide.
Regarder la façon dont les QuartzDemo exemple d'application ne présente, plus précisément le QuartzPDFView classe dans le QuartzImages.h et QuartzImages.m fichiers. Il montre un exemple de chargement d'un fichier PDF via le Quartz.