---
title: "rtf(from:documentAttributes:)"
framework: foundation
role: symbol
role_heading: Instance Method
path: "foundation/nsattributedstring/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.

## Declaration

```swift
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 doc://com.apple.foundation/documentation/Foundation/NSAttributedString/DocumentAttributeKey/documentType.

## Return Value

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

## Discussion

Discussion Writes the document-level attributes in docAttributes, as explained in RTF Files and Attributed Strings. Raises an 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 NSPasteboard method 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  rtf(from:), on the other hand, does strip attachment characters when extracting RTF.

## See Also

### Exporting the string as data

- [data(from:documentAttributes:)](foundation/nsattributedstring/data(from:documentattributes:).md)
- [fileWrapper(from:documentAttributes:)](foundation/nsattributedstring/filewrapper(from:documentattributes:).md)
- [docFormat(from:documentAttributes:)](foundation/nsattributedstring/docformat(from:documentattributes:).md)
- [rtfd(from:documentAttributes:)](foundation/nsattributedstring/rtfd(from:documentattributes:).md)
- [rtfdFileWrapper(from:documentAttributes:)](foundation/nsattributedstring/rtfdfilewrapper(from:documentattributes:).md)
