0

I am trying to create a RTFD file that would contain text (NSString) and images (JPG). I have not found an easy to follow example that explains this process. If my application has some text and some JPG images, and I'd like to create an editable document containing both of these items, RTFD seems like the best option for output (I want to preserve quality of the image).

If someone could present a simple example showing how to do the text and images that would be helpful. I am also open to doing RTF, as long as image quality is preserved. This is for a Mac OS application.

I've been trying to follow the Attributed String Programming Guide, but am unclear on how I can output my NSAttributedString and get the bundle created. And secondly, how I get the image references into not only the RTF file, but the bundle where they need to be.

cj.lange
  • 21
  • 5
  • Have you checked [TextEdit sample code](https://developer.apple.com/library/mac/samplecode/TextEdit/Introduction/Intro.html)? TextEdit.app can create rich text document with attachments. – Volodymyr Sapsai May 14 '14 at 13:59
  • Are you referring to the TextEdit application? If so, I am aware that this can do what I want, but I want to do it programmatically so that my application can just create the equivalent of opening TextEdit, typing text and inserting images without the user doing anything in TextEdit. – cj.lange May 14 '14 at 14:18
  • Yes, I am talking about TextEdit application. It's entire source code is available as sample code. If you are looking for an example of how to work with RTFD files, it is reasonable to start with TextEdit source code. – Volodymyr Sapsai May 14 '14 at 15:44

1 Answers1

1

Quite old question, but maybe the solution helps others...

The RTFD format doesn't describe a file, but a a file bundle (which is in fact a directory modified by the Mac/iOS UI to appear to the user as a single file).

But fortunately NSFileWrapper is capable of creating such RTFD bundles:

NSAttributedString* attrString = ...some attributed string with images ...;
NSDictionary*       documentAttributes = @{
                                           NSDocumentTypeDocumentAttribute: NSRTFDTextDocumentType
                                           };
NSError*            error = nil;
NSFileWrapper*      fileWrapper = [attrString fileWrapperFromRange:NSMakeRange(0, attrString.length)
                                            documentAttributes:documentAttributes
                                                         error:&error];

NSString*           documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString*           filePath = [documentsDirectory stringByAppendingPathComponent:@"example.rtfd"];
NSLog(@"Writing rtfd doc to: %@", filePath);

[NSFileManager.defaultManager removeItemAtPath:filePath error:&error];
BOOL    result = [fileWrapper writeToURL:[NSURL fileURLWithPath:filePath]
                              options:NSFileWrapperWritingAtomic
                  originalContentsURL:nil
                                error:&error];

Done!

As RTFD nearly is a Mac/iOS only format, it might be more interesting to create a RTF file with included images.

Unfortunately Apple's RTF implementation (NSDocumentTypeDocumentAttribute: NSRTFTextDocumentType) doesn't support images.

See my answer to another question to solve this: https://stackoverflow.com/a/29181130/2778898

Community
  • 1
  • 1
LaborEtArs
  • 1,938
  • 23
  • 27