iphone - 在 xcode/ios 中以编程方式编辑 RTF/DOC 文件

标签 iphone ios xcode ipad

我有一个可以在 iPhone 上查看的 RTF 文件,现在我需要以编程方式编辑该文件,请告诉我该怎么做。 例子 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 文件中添加此行,单击按钮即可获取字符串中 rtf 文件的内容

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的函数在这里。

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

关于iphone - 在 xcode/ios 中以编程方式编辑 RTF/DOC 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10994432/

相关文章:

swift - 如何在 xcode 中为每个属性只存储一个值

ios - ld : warning: Could not find auto-linked library 'swiftObjectiveC'

php - Apple 推送通知中的表情符号支持

ios - Sqlite.swift 过滤/查找

iphone - 如何使用 Flash Builder 4.6 在 iPhone 4 上启用高分辨率显示

ios - 在 UIPageViewControllers 中预加载所有 UIViewControllers (Swift)

ios - removeFromSuperview() 几个 subview

ios - navigationItem.TitleView 不适用于 iOS 10

iphone - 如何正确清空 NSMutableArray

iphone - 在gdata集成中从uiweb View 重定向返回