以编程方式在xcode / ios中编辑RTF / DOC文件
我有一个RTF文件,我可以在iphone上查看,现在我需要编程编辑文件,请让我知道我该怎么做。 示例RTF文件内容“我是StackOverflow的新手”现在我需要通过编程切换到下面的行。 “我经常访问StackOverflow”并保存为PDF文件。 请帮助我:)
提前致谢 :)
在.h文件中添加一些对象并进行定义
#define kBorderInset 20.0 #define kBorderWidth 1.0 #define kMarginInset 10.0 @interface ViewController : UIViewController { CGSize pageSize; NSString *pdfFileName; NSString *contents_for_pdf; } - (void)generatePdfWithFilePath:(NSString *)thefilePath;
在.m文件中添加这行一些在button上单击以获取rtf文件的内容在string中
NSString *rtf_path = [[NSBundle mainBundle] pathForResource:@"example" ofType:@"rtf"]; contents_for_pdf = [[NSString alloc] initWithContentsOfFile:rtf_path encoding:NSUTF8StringEncoding error:nil]; NSLog(@"%@",contents_for_pdf); contents_for_pdf = [contents_for_pdf stringByReplacingOccurrencesOfString:@"I am new to StackOverflow" withString:@"I regularly visit StackOverflow"]; pageSize = CGSizeMake(612, 792); NSString *fileName = @"Demo.pdf"; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; pdfFileName = [documentsDirectory stringByAppendingPathComponent:fileName]; [self generatePdfWithFilePath:pdfFileName];
我没有修改rtf文件的文本,我只是在rtf文件中使用相同的文本。你应该做你想做的(我注意到在文本中的一些编码字符)和function,将生成PDF格式在这里。
- (void) generatePdfWithFilePath: (NSString *)thefilePath { UIGraphicsBeginPDFContextToFile(thefilePath, CGRectZero, nil); //Start a new page. UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, pageSize.width, pageSize.height), nil); //Draw text fo our header. CGContextRef currentContext = UIGraphicsGetCurrentContext(); CGContextSetRGBFillColor(currentContext, 0.0, 0.0, 0.0, 1.0); UIFont *font = [UIFont systemFontOfSize:14.0]; CGSize stringSize = [contents_for_pdf sizeWithFont:font constrainedToSize:CGSizeMake(pageSize.width - 2*kBorderInset-2*kMarginInset, pageSize.height - 2*kBorderInset - 2*kMarginInset) lineBreakMode:UILineBreakModeWordWrap]; CGRect renderingRect = CGRectMake(kBorderInset + kMarginInset, kBorderInset + kMarginInset + 50.0, pageSize.width - 2*kBorderInset - 2*kMarginInset, stringSize.height); [contents_for_pdf drawInRect:renderingRect withFont:font lineBreakMode:UILineBreakModeWordWrap alignment:UITextAlignmentLeft]; UIGraphicsEndPDFContext(); UIAlertView *Alert = [[UIAlertView alloc] initWithTitle:@"PDF Created" message:@"Sucessfull" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil]; [Alert show]; }
- 像Objective-C一样,如何在swift中编写“valueForKey”?
- 从重用的自定义单元格中的按钮传递数据
- iOS:使用insertRowsAtIndexPaths在UITableview中插入多行
- CoreAnimation – 不透明度淡入和淡出animation不工作
- Cordova 5 build命令是删除iOS设备方向设置
- NSKeyedArchiver / NSKeyedUnarchiver在不同iOS版本上的兼容性
- 找不到归档的Three20 / Three20.h文件
- 获取NSString中所有大写字母的NSRange对象数组的最快方法?
- Facebook – 由app_scoped_user_id打开个人资料