Extrahieren von Bildern aus PDF
Ich bin eine kleine Anfrage bezüglich Extrahieren, speziell Bilder ( nur Bilder ) aus einem angegebenen PDF-Dokument in der iPhone-Anwendung.
Ich durchgemacht habe in der Dokumentation von apple - Aber ich bin Fehler zu finden.
Ich getan habe folgenden Bemühungen um das Bild aus dem PDF-Dokument.
-(IBAction)btnTappedImages:(id)sender{
//MyGetPDFDocumentRef is custom c method
//& filePath is path to pdf document.
CGPDFDocumentRef document = MyGetPDFDocumentRef ([filePath UTF8String]);
int pgcnt = CGPDFDocumentGetNumberOfPages( document );
for( int i1 = 0; i1 < pgcnt; ++i1 ) {
//1. Open Document page
CGPDFPageRef pg = CGPDFDocumentGetPage (document, i1+1);
if( !pg ) {
NSLog(@"Couldn't open page.");
}
//2. get page dictionary
CGPDFDictionaryRef dict = CGPDFPageGetDictionary( pg );
if( !dict ) {
NSLog(@"Couldn't open page dictionary.");
}
//3. get page contents stream
CGPDFStreamRef cont;
if( !CGPDFDictionaryGetStream( dict, "Contents", &cont ) ) {
NSLog(@"Couldn't open page stream.");
}
//4. copy page contents steam
// CFDataRef contdata = CGPDFStreamCopyData( cont, NULL );
//5. get the media array from stream
CGPDFArrayRef media;
if( !CGPDFDictionaryGetArray( dict, "MediaBox", &media ) ) {
NSLog(@"Couldn't open page Media.");
}
//6. open media & get it's size
CGPDFInteger mediatop, medialeft;
CGPDFReal mediaright, mediabottom;
if( !CGPDFArrayGetInteger( media, 0, &mediatop ) || !CGPDFArrayGetInteger( media, 1, &medialeft ) || !CGPDFArrayGetNumber( media, 2, &mediaright ) || !CGPDFArrayGetNumber( media, 3, &mediabottom ) ) {
NSLog(@"Couldn't open page Media Box.");
}
//7. set media size
double mediawidth = mediaright - medialeft, mediaheight = mediabottom - mediatop;
//8. get media resources
CGPDFDictionaryRef res;
if( !CGPDFDictionaryGetDictionary( dict, "Resources", &res ) ) {
NSLog(@"Couldn't Open Page Media Reopsources.");
}
//9. get xObject from media resources
CGPDFDictionaryRef xobj;
if( !CGPDFDictionaryGetDictionary( res, "XObject", &xobj ) ) {
NSLog(@"Couldn't load page Xobjects.");
}
char imagestr[16];
sprintf( imagestr, "Im%d", i1 );
//10. get x object stream
CGPDFStreamRef strm;
if( !CGPDFDictionaryGetStream( xobj, imagestr, &strm ) ) {
NSLog(@"Couldn't load stream for xObject");
}
//11. get dictionary from xObject Stream
CGPDFDictionaryRef strmdict = CGPDFStreamGetDictionary( strm );
if( !strmdict ) {
NSLog(@"Failed to load dictionary for xObject");
}
//12. get type of xObject
const char * type;
if( !CGPDFDictionaryGetName( strmdict, "Type", &type ) || strcmp(type, "XObject" ) ) {
NSLog(@"Couldn't load xObject Type");
}
//13. Check weather subtype is image or not
const char * subtype;
if( !CGPDFDictionaryGetName( strmdict, "Subtype", &subtype ) || strcmp( subtype, "Image" ) ) {
NSLog(@"xObject is not image");
}
//14. Bits per component
CGPDFInteger bitsper;
if( !CGPDFDictionaryGetInteger( strmdict, "BitsPerComponent",&bitsper ) || bitsper != 1 ) {
NSLog(@"Bits per component not loaded");
}
//15. Type of filter of image
const char * filter;
if( !CGPDFDictionaryGetName( strmdict, "Filter", &filter ) || strcmp( filter, "FlateDecode" ) ) {
NSLog(@"Filter not loaded");
}
//16. Image height width
CGPDFInteger width, height;
if( !CGPDFDictionaryGetInteger( strmdict, "Width", &width ) || !CGPDFDictionaryGetInteger( strmdict, "Height", &height ) ) {
NSLog(@"Image Height - width not loaded.");
}
//17. Load image bytes & verify it
CGPDFDataFormat fmt = CGPDFDataFormatRaw;
CFDataRef data = CGPDFStreamCopyData( strm, &fmt );
int32_t len = CFDataGetLength( data );
const void * bytes = CFDataGetBytePtr( data );
//now I have bytes for images in "bytes" pointer the problem is how to covert it into UIImage
NSLog(@"Image bytes length - %i",len);
int32_t rowbytes = (width + 7) / 8;
if( rowbytes * height != len ) {
NSLog(@"Invalid Image");
}
double xres = width / mediawidth * 72.0, yres = height / mediaheight * 72.0;
xres = round( xres * 1000 ) / 1000;
yres = round( yres * 1000 ) / 1000;
}
}
InformationsquelleAutor Sagar R. Kothari | 2010-03-19
Du musst angemeldet sein, um einen Kommentar abzugeben.
Ja ! Ich habe es gefunden. Aber Es sieht sehr beängstigend aus - riesige code.
+1 für die tolle Antwort. Funktioniert Super in iOS6 aber seinen Absturz auf iOS7. Konsole sagen
Assertion failed: (color_components <= decode_components), function cgimage_set_decode_array, file Images/CGImage.c, line 233.
Können Sie bitte mir helfen, diese in iOS 7diese Antwort scheint zu sein, fehlt die kritische
pdfDictionaryFunction()
Umsetzung. Bin ich etwas fehlt?CGPDFDictionaryApplyFunction(xobj, pdfDictionaryFunction, NULL); bitte stellen Sie die Umsetzung von pdfDictionaryFunction .
Sie brauchen nicht alle diese Codes finden Sie ein Beispiel-Projekt für OS X, extrahiert alle JPEG -, JPEG 2000-und RAW (PNG) - Bilder aus einer PDF-Datei: github.com/swisspol/PDF2CBZ
InformationsquelleAutor Sagar R. Kothari
Umwandlung einer CGPDFStreamRef zu einem UIImage nicht wirklich zu den konzeptionellen Sinne; eine PDF-Datei repräsentiert ein Dokument mit potenziell mehreren Seiten und ohne erkennbare Zuordnung zu einem bitmap-Bild.
Selbst wenn Sie wissen, Ihre PDF-Datei enthält nur eine Seite, werden Sie noch brauchen, um einige zu Parsen und Tricks. Es ist eine sehr kurze Diskussion hier: http://lists.apple.com/archives/Cocoa-dev/2006/Sep/msg01407.html
t
nach AufrufCGPDFStreamCopyData
? Das sieht aus wie es wird Ihnen sagen, ob Bilddaten oder nicht.Übrigens, es gibt keinen Punkt in der Einstellung
t=CGPDFDataFormatJPEG2000
. Dieser parameter wird überschrieben, mit welchem format die Funktion entscheidet der Ausgabe.Ok. Aus deiner Antwort bin ich die Aktualisierung meiner Frage.
InformationsquelleAutor Tom
InformationsquelleAutor NANNAV