Czytanie plików PDF jako ciąg przez aplikację iPhone
Stoję przed pewnym problemem w rozwoju aplikacji iPhone do "czytania PDF". Próbowałem postępować zgodnie z kodem. Wiem, że użyłem niewłaściwych metod do parsowania - metody parsowania są używane tylko w celu wyszukiwania. Ale chcę przekonwertować cały tekst pdf na ciąg znaków. Powiedzmy na przykład MobileHIG Apple.pdf-użyłem w tym kodzie.
@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];
}
}
Ale nie udało mi się odczytać nawet jednej linijki z dokumentu pdf. Czego jeszcze brakuje?
3 answers
Mam bibliotekę, która może to zrobić dokładnie tutaj: wyodrębnianie tekstu pdf w celu C
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 12:08:47
Jeśli chcesz wyodrębnić zawartość z pliku pdf, możesz przeczytać:
Z Quartz 2D programming guide.
Zasadniczo do analizy zawartości użyjesz obiektu CGPDFScanner
, co działa w następujący sposób. Rejestrujesz kilka wywołań zwrotnych, które będą automatycznie wywoływane przez Quartz 2D po napotkaniu niektórych operatorów pdf w strumieniu pdf. Po tym wstępnym kroku zaczniesz analizować strumień pdf.
Patrząc na Twój kod, wydaje się, że nie wykonujesz kroków wymaganych do analizy zawartości pdf strony, przez którą przechodzisz CGPDFDocumentGetPage()
. Najpierw musisz skonfigurować wywołania zwrotne za pomocą CGPDFOperatorTableCreate()
i CGPDFOperatorTableSetCallback()
, a następnie uzyskać Stronę, musisz utworzyć strumień treści za pomocą tej strony (używając CGPDFContentStreamCreateWithPage()
), a następnie utworzyć instancję CGPDFScanner
przez CGPDFScannerCreate()
i faktycznie rozpocząć skanowanie przez CGPDFScannerScan()
.
Sekcja "Analizowanie zawartości PDF" dokumentu wskazanego przez powyższy URL daje masz wszystkie informacje wymagane do wdrożenia parsowania pdf.
Mam nadzieję, że to pomoże.Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-03-04 11:55:54
Zobacz, jak przykładowa aplikacja QuartzDemo robi to, a konkretnie Klasa QuartzPDFView w QuartzImages.h I QuartzImages.pliki M. Pokazuje przykład ładowania pliku PDF przez Kwarc.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-03-03 18:06:15