在xcode / ios中以编程方式编辑RTF / DOC文件 [英] Edit RTF/DOC file programmatically in xcode/ios

查看:195
本文介绍了在xcode / ios中以编程方式编辑RTF / DOC文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个RTF文件,我可以在iphone上查看,现在我需要以编程方式编辑文件,请让我知道我该怎么做。
示例
RTF文件内容
我是StackOverflow的新手
现在我需要以编程方式更改为以下行。
我经常访问StackOverflow并保存为PDF文件。
请帮助我:))

I have a RTF file which I can view on iphone , Now I need to edit the file programmatically , please let me know how can I do it. Example RTF file content "I am new to StackOverflow" Now I need to change to below line programmatically. "I regularly visit StackOverflow" and save as PDF file. Please do help me :)

提前致谢:)

推荐答案

在.h文件中添加一些对象并定义

In your .h file add some objects and defines

#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文件中添加此行一些按钮单击以获取字符串中的rtf文件的内容

in .m file add this lines some where on button click to get contents of rtf file in 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文件中使用了相同的文本。你应该做什么如你所愿(我注意到文本中有一些编码字符)和生成pdf的函数就在这里。

I didnt modify the text of rtf file,I just used same text in rtf file.you should do as you want(I noticed some encoding characters in text) and function that will generate pdf is here.

- (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];
}

这篇关于在xcode / ios中以编程方式编辑RTF / DOC文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆