I try to read pdf file. I want to use this function
databuffer = [file readDataOfLength : 7] but i want read all the byte in the line .
It mean I use seekToFileOffset to find the byte but I want read the line.
NSMutableArray *nameArray = [[NSMutableArray alloc] initWithObjects:nil];
NSMutableArray *nameArrayDict = [[NSMutableArray alloc] initWithObjects:nil];
NSString *path = [[NSBundle mainBundle] pathForResource:#"testpdf" ofType:#"pdf"];
NSString *contents = [NSString stringWithContentsOfFile:path encoding:NSASCIIStringEncoding error:nil];
int var=[[nameArray objectAtIndex:[nameArray count]-2] intValue];
NSFileHandle *file;
NSData *databuffer;
file = [NSFileHandle fileHandleForReadingAtPath: appFile];
int i=0;
while (file!=nil ) {
[file seekToFileOffset: var+i];
databuffer = [file readDataOfLength : 7];
NSString* aStr;
aStr = [[NSString alloc] initWithData: databuffer encoding:NSASCIIStringEncoding];
NSLog(#"%#",aStr);
i=i+[databuffer length];
}
now i try your solution but i can't show nothing!!!
CGPDFPageRef page = CGPDFDocumentGetPage (myDocument, 1);// 2
CGPDFDictionaryRef d;
d = CGPDFPageGetDictionary(page);
CGPDFScannerRef myScanner;
CGPDFOperatorTableRef myTable;
myTable = CGPDFOperatorTableCreate();
CGPDFContentStreamRef myContentStream = CGPDFContentStreamCreateWithPage (page);// 3
myScanner = CGPDFScannerCreate (myContentStream, myTable, NULL);// 4
CGPDFScannerScan (myScanner);// 5
CGPDFOperatorTableSetCallback(myTable, "BT", &op_BT);//Begin text object
CGPDFOperatorTableSetCallback(myTable, "ET", &op_ET);//End text object
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);
I recommend not to parse your pdf in a way you like to do it.
Try to use CGPDFScanner (docs here)
As JeremyP says: lot's of zlib compressed stuff is in a PDF file. Do search for end of lines.
Use CGPDFScanner to extract font maps, images, etc.
But is not that easy. I can tell you. :)
Related
I'm loading the data from a plist and am trying to find out how to access data from each item. In the following code I'd like to be able to extract the value of text and whether it's checked = 0 or checked = 1
I've tried this:
NSString *dataArray1 = [[dataArray objectAtIndex:1] objectAtIndex:2];
but was wondering if that is the best approach
thanks for any help.
// load data from a plist file inside our app bundle
NSString *path = [[NSBundle mainBundle] pathForResource:#"Providers" ofType:#"plist"];
dataArray = [NSMutableArray arrayWithContentsOfFile:path];
NSLog(#"data array from offers %#", dataArray);
Here's the output:
2013-01-21 15:13:34.599 data array from offers (
{
checked = 1;
text = Provider1;
},
{
checked = 1;
text = Provider2;
},
{
checked = 1;
text = Provider3;
},
{
checked = 1;
text = Provider4;
}
)
So I'd like to be able to find out what the checked value of each item. That Provider4 is set to 1 and Provider3 is 0 etc... then use that to pass in parameters to a string.
NSString *path = [[NSBundle mainBundle] pathForResource:#"Providers" ofType:#"plist"];
dataArray = [NSMutableArray arrayWithContentsOfFile:path];
for (NSDictionary *dictionary in dataArray)
{
NSString *text = [dictionary valueForKey:#"text"];
NSNumber *checked = [dictionary valueForKey:#"checked"];
NSLog(#"%# checked value is: %#", text, checked);
}
something like this, i guess:
for (id dict in dataArray)
int checked = [[(NSDictionary *)dict objectForKey:#"checked"] intValue];
Hi Im really having a hard time saving in my NSDocumentDirectory.Im using AGImagePickerby the way. Yes I was able to save this in NSDocumentDirectory. But how to save them uniquely ( in terms of their own then converting their names into oneSlotImages) or save them with their unique IDs then load them back. Sorry Im kinda new to this UIImagePickerControllerMediaURL thing., I think that would be my solution to my other problem for not overlaping them when saving. How to save this using their unique ID, or UIImagePickerControllerMediaURL.
for (int i = 0; i < info.count; i++) {
NSLog(#"%#", [info objectAtIndex:i]);
NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask ,YES );
NSString *documentsDir = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDir stringByAppendingPathComponent:[NSString stringWithFormat:#"oneSlotImages%d.png", i]];
ALAssetRepresentation *rep = [[info objectAtIndex: i] defaultRepresentation];
UIImage *image = [UIImage imageWithCGImage:[rep fullResolutionImage]];
//----resize the images
image = [self imageByScalingAndCroppingForSize:image toSize:CGSizeMake(256,256*image.size.height/image.size.width)];
NSData *imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:savedImagePath atomically:YES];
Thanks for the help. Much Appreciated.
You could always keep a list of used names and do something like this
int i = 1;
while([listOfUsedNames containsObject:nextAvailableTile]) {
nextAvailableTitle = [kDefaultImageName stringByAppendingFormat:#" %d", i];
i++;
}
// found an unused name
The URL for each image is unique right? So we can make use of this. Convert the URL into MD5 string (which form a unique identifier for each image). And save with that name (like "MD5string.png").
Why can't we use this?
Hope this helps you.
For converting to MD5, please create a file named NSString+MD5.h and put the code
#import <Foundation/Foundation.h>
#import <CommonCrypto/CommonDigest.h>
#interface NSString(MD5)
- (NSString *)MD5;
#end
in it.
Then in the NSString+MD5.m,
#import "NSString+MD5.h"
#implementation NSString(MD5)
- (NSString*)MD5
{
const char *ptr = [self UTF8String];
unsigned char md5Buffer[CC_MD5_DIGEST_LENGTH];
CC_MD5(ptr, strlen(ptr), md5Buffer);
NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
[output appendFormat:#"%02x",md5Buffer[i]];
return output;
}
#end
Import the NSString+MD5.h class where ever you want to use the MD5 function with normal NSString object.
In you code if you have the UIImagePickerControllerMediaURL string, since it is unique for every file you can convert in to MD5 String like
NSString *imgURL = [NSString stringWithString: UIImagePickerControllerMediaURL];
NSString *MD5String = [imgURL MD5];
NSString *savedImagePath = [documentsDir stringByAppendingPathComponent:[NSString stringWithFormat:#"%#.png", MD5String]];
Use that path to save your image file.
On Loading,
Convert the UIImagePickerControllerMediaURL into MD5 and check in the Documents directory for the file
// In this the UIImagePickerControllerMediaURL is the URL of media file to load
NSString *imgURL = [NSString stringWithString: UIImagePickerControllerMediaURL];
NSString *MD5String = [imgURL MD5];
NSString *savedImagePath = [documentsDir stringByAppendingPathComponent:[NSString stringWithFormat:#"%#.png", MD5String]];
Then do the checking with the fileExistsAtPath method of NSFileManager and if exists then load the file from the path. Thats it.
Note: For this you want to keep the UIImagePickerControllerMediaURL of the images you saved in documents directory locally somewhere in your app (In DB or NSUserDefaults) for make use of then at the time of loading.
Try this:
NSMutableString *imageName = [[[NSMutableString alloc] initWithCapacity:0] autorelease];
CFUUIDRef theUUID = CFUUIDCreate(kCFAllocatorDefault);
if (theUUID) {
[imageName appendString:NSMakeCollectable(CFUUIDCreateString(kCFAllocatorDefault, theUUID))];
CFRelease(theUUID);
}
[imageName appendString:#".png"];
I am trying to write some data to a file in iphone sdk....I am using a for statement to write to explain the situation better I have some code:
for(int i = 0;i < 11;i++){
NSString *myFile = [documentsDirectory stringByAppendingPathComponent:#"hello.txt"];
[i writeToFile: myFile atomically:NO encoding:NSUTF8StringEncoding error:NULL];
}
now I expect 12345678910 to written in the file but only 10 is written in it which means file is being trncated everytime I write to file..so my question is what can I do to write 1 to 10 WITHOUT puttting everything in a string because I have alot of data..
thanks
The SDK documentation says this about -writeToFile:atomically:encoding:error: This method overwrites any existing file at path.
That method is intended for dumping a string out to a file. For your purposes, you can either build up a string, or open a file with [NSFileHandle fileHandleForWritingAtPath]:
NSString *myFile = [documentsDirectory stringByAppendingPathComponent:#"hello.txt"];
NSFileHandle *file = [NSFileHandle fileHandleForWritingAtPath:myFile];
for(int i = 0; i <= 10; i++) {
NSString *dataString = [NSString stringWithFormat:#"%i", i];
[file writeData:[dataString dataUsingEncoding:NSUTF8StringEncoding]];
}
[file closeFile];
If you're trying to write a series of integers to a file, I would go about it like this:
NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:FILE_PATH];
for(int i=0; i<11; i++)
{
NSString *str = [NSString stringWithFormat:#"%d",i];
[fileHandle writeData:[str dataUsingEncoding:NSUTF8StringEncoding]];
}
[fileHandle closeFile];
NSBundle *bundle = [NSBundle mainBundle];
NSString *pthpath = [bundle pathForResource:#"path" ofType:#"txt"];
NSString *content = [NSString stringWithContentsOfFile:pthpath encoding:NSUTF8StringEncoding error:nil];
array=[[NSArray alloc ]init];
array = [content componentsSeparatedByString:#"~"];
=====================================================================
here content is:
87,348~51,347~135,132~182,133~268,346~236,347~159,168~87,347#118,298~115,297~200,298~189,266~128,265~117,299#222,352~268,353~264,340~219,342~225,355#186,262~199,299~212,297~195,257~188,260
and array is:
"87,348",
"51,347",
"135,132",
"182,133",
"268,346",
"236,347",
"159,168",
"87,347#118,298",
"115,297",
"200,298",
"189,266",
"128,265",
"117,299#222,352",
"268,353",
"264,340",
"219,342",
"225,355#186,262",
"199,299",
"212,297",
"195,257",
"188,260"
But I want to again create an array by parsing with #. Please help me out...........
for (NSString *string in array) {
NSArray *subArray = [string componentsSeparatedByString:#"#"];
for (NSString *substring in subArray)
etc. etc.
(Next time try to have your question better formatted and articulated please.)
Instead of using componentsSeparatedByString:, use componentsSeparatedByCharactersInSet: and create a character set with the separators you want.
Also, you are creating an array there (array = [[NSArray alloc] init]) and when you do array = [content componentsSeparatedByString:#"#"] you are leaking the just allocated array. In general, seems like you should read more about how objects and references work.
I think from following code you may get some idea, if I understood your question correctly,
NSMutableArray *resultArray = [[NSMutableArray alloc] initWithCapacity:1];
NSArray *tempArray1 = nil;
NSArray *tempArray2 = nil;
NSString *content = #"87,348~51,347~135,132~182,133~268,346~236,347~159,168~87,347#118,298~115,297~200,298~189,266~128,265~117,299#222,352~268,353~264,340~219,342~225,355#186,262~199,299~212,297~195,257~188,260";
tempArray1 = [content componentsSeparatedByString:#"#"];
for(NSString *string in tempArray1)
{
tempArray2 = [string componentsSeparatedByString:#"~"];
[resultArray addObjectsFromArray:tempArray2];
}
NSLog(#"ResultArray :%#", resultArray);
I have a data source with about 2000 lines that look like the following:
6712,Anaktuvuk Pass Airport,Anaktuvuk Pass,United States,AKP,PAKP,68.1336,-151.743,2103,-9,A
What I am interested in is the 6th section of this string so I want to turn it into an array, then i want to check the 6th section [5] for an occurrance of that string "PAKP"
Code:
NSBundle *bundle = [NSBundle mainBundle];
NSString *airportsPath = [bundle pathForResource:#"airports" ofType:#"dat"];
NSData *data = [NSData dataWithContentsOfFile:airportsPath];
NSString *dataString = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
NSArray *dataArray = [dataString componentsSeparatedByString:#"\n"];
NSRange locationOfAirport;
NSString *workingString = [[NSString alloc]initWithFormat:#""];
NSString *searchedAirport = [[NSString alloc]initWithFormat:#""];
NSString *airportData = [[NSString alloc]initWithFormat:#""];
int d;
for (d=0; d < [dataArray count]; d=d+1) {
workingString = [dataArray objectAtIndex:d];
testTextBox = workingString; //works correctly
NSArray *workingArray = [workingString componentsSeparatedByString:#","];
testTextBox2 = [workingArray objectAtIndex: 0]; //correctly displays the first section "6712"
testTextBox3 = [workingArray objectAtIndex:1] //throws exception index beyond bounds
locationOfAirport = [[workingArray objectAtIndex:5] rangeOfString:#"PAKP"];
}
the problem is that when the workingArray populates, it only populates with a single object (the first component of the string which is "6712". If i have it display the workingString, it correctly displays the entire string, but for some reason, it isn't correctly making the array using the commas.
i tried it without using the data file and it worked fine, so the problem comes from how I am importing the data.
ideas?
You code works. You should run it with the debugger to see what's happening. At a guess, your input data isn't what you think it is - possibly a different encoding, or different line endings.
See sample:
NSString *dataString = #"6712,Anaktuvuk Pass Airport,Anaktuvuk Pass,United States,AKP,PAKP,68.1336,-151.743,2103,-9,A";
NSArray *dataArray = [dataString componentsSeparatedByString:#"\n"];
for (NSString *workingString in dataArray) {
NSString *testTextBox = workingString; //works correctly
NSArray *workingArray = [workingString componentsSeparatedByString:#","];
NSString *testTextBox2 = [workingArray objectAtIndex: 0]; //correctly displays the first section "6712"
NSString *testTextBox3 = [workingArray objectAtIndex:1]; //throws exception index beyond bounds
NSRange locationOfAirport = [[workingArray objectAtIndex:5] rangeOfString:#"PAKP"];
}
there was a problem in the data where there were a few "\"s that caused the errors.