<!--
{
  "availability" : [
    "macOS: 10.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/NSAttributedString/rtf(from:documentAttributes:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSAttributedString(im)RTFFromRange:documentAttributes:"
  },
  "title" : "rtf(from:documentAttributes:)"
}
-->

# rtf(from:documentAttributes:)

Returns a data object that contains an RTF stream corresponding to the characters and attributes within the specified range, omitting all attachment attributes.

```
func rtf(from range: NSRange, documentAttributes dict: [NSAttributedString.DocumentAttributeKey : Any] = [:]) -> Data?
```

## Parameters

`range`

The range.

`dict`

A required dictionary specifying the document attributes. The dictionary contains values from `Document Types` and must at least contain [`documentType`](/documentation/Foundation/NSAttributedString/DocumentAttributeKey/documentType).

## Return Value

A data object containing an RTF stream for the attributed string.

## Discussion

Writes the document-level attributes in `docAttributes`, as explained in [RTF Files and Attributed Strings](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/TextAttributes/RTFAndAttrStrings.html#//apple_ref/doc/uid/20000164).

Raises an [`rangeException`](/documentation/Foundation/NSExceptionName/rangeException) if any part of `aRange` lies beyond the end of the receiver’s characters.

When writing data to the pasteboard, you can use the `NSData` object as the first argument to the <doc://com.apple.documentation/documentation/AppKit/NSPasteboard> method <doc://com.apple.documentation/documentation/AppKit/NSPasteboard/setData(_:forType:)>, with a second argument of `NSRTFPboardType`. Although this method strips attachments, it leaves the attachment characters in the text itself. The `NSText` method  <doc://com.apple.documentation/documentation/AppKit/NSText/rtf(from:)>, on the other hand, does strip attachment characters when extracting RTF.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)