【问题标题】:Edit RTF/DOC file programmatically in xcode/ios在 xcode/ios 中以编程方式编辑 RTF/DOC 文件
【发布时间】:2012-06-12 10:06:29
【问题描述】:

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

提前致谢:)

【问题讨论】:

  • 无法在iphone中写入文件。它的工作在模拟器工作,因为它有写权限。但在 iphone 或 ipad 中,它不授予写入文件的权限。您必须使用数据库。
  • 嗨 Parag,感谢您的信息,但必须有某种方法可以设置文件的权限,然后编辑文件,所以请让我知道是否有任何参考/代码,再次感谢

标签: iphone ios xcode ipad


【解决方案1】:

在您的 .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];
}

【讨论】:

  • 嗨,感谢您的代码,此代码用于创建 PDF 文件 :) 但我主要关心的是编辑 rtf 文件或 doc 文件,所以必须有某种方式可以编辑以编程方式生成 RTF/DOC 文件。如果您有参考资料,请告诉我
  • 您询问了保存为 PDF 文件的问题。并且您想在 rtf 的内容之后更改该单词,然后进行字符串搜索并替换字符串中的单词并将其写入 PDF 文件,如您所问的那样。
  • 是的,是正确的,但您的代码显示了如何创建 pdf 文件,但我需要更改内容然后创建,就像在 NSLog(@"%@",contents_for_pdf);我想更改内容然后创建 PDF 文件,所以请帮忙,所以基本上我无法在 contents_for_pdf 中进行更改,当我尝试进行更改时它没有反映:(
  • @user1450901,我编辑了答案,检查 NSLog(@"%@",contents_for_pdf); 之后的行;
  • 当我在 ios {\rtf1\ansi\ansicpg1252\cocoartf1504 {\fonttbl\f0\fswiss\fcharset0 Helvetica;} {\colortbl;\red255\ green255\blue255;\red254\green209\blue100;} {*\expandedcolortbl;\csgray\c100000;\csgenericrgb\c99608\c81961\c39216;} \margl1440\margr1440\vieww10800\viewh8400\viewkind0 \pard40\tx721\tx \tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural\partightenfactor0 \f0\i\b\fs72 \cf2 \ul \ulc2 acc} 而不是字符串 acc 你能帮我看看如何将 rtf 转换为 pdf
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-11
  • 1970-01-01
  • 2013-09-12
  • 2023-03-11
相关资源
最近更新 更多