---
title: "init(_:for:contentType:source:_:)"
framework: swiftui
role: symbol
role_heading: Initializer
path: "swiftui/newdocumentbutton/init(_:for:contenttype:source:_:)"
---

# init(_:for:contentType:source:_:)

Creates and opens new documents from a template picker.

## Declaration

```swift
nonisolated init<D>(_ label: Text? = nil, for documentType: D.Type = D.self, contentType: UTType? = nil, source: DocumentCreationSource, _ prepareDocument: @escaping () async throws -> D? = { nil }) where D : FileDocument
```

## Parameters

- `label`: A label for the button.
- `documentType`: The type of document to create.
- `contentType`: An optional content type of the document to create.
- `source`: A source for the document creation flow. When a document is created, you can retrieve its source from doc://com.apple.SwiftUI/documentation/SwiftUI/FileDocumentConfiguration or doc://com.apple.SwiftUI/documentation/SwiftUI/URLDocumentConfiguration.
- `prepareDocument`: Called when the user taps the button. Present a template picker or other UI, then return the prepared document, nil to request an empty document, or throw on cancellation.

## Discussion

Discussion NewDocumentButton(     Text("New from template"),     for: TextDocument.self,     source: .template ) {     try await withCheckedThrowingContinuation { continuation in         documentCreationContinuation = continuation         showTemplatePicker = true     } }
