# 匯入匯出

- Human documentation: [https://docs.univer.ai/zh-TW/guides/pdfs/features/import-export](https://docs.univer.ai/zh-TW/guides/pdfs/features/import-export)

- Agent Markdown: [https://docs.univer.ai/zh-TW/guides/pdfs/features/import-export.md](https://docs.univer.ai/zh-TW/guides/pdfs/features/import-export.md)

- Requested language: `zh-TW`

- Content language: `zh-TW`

- Documentation version: `1.2.0`

- Source: [pdfs/features/import-export.zh-TW.mdx](https://github.com/dream-num/documentation/blob/dev/content/guides/pdfs/features/import-export.zh-TW.mdx)

---

使用下方瀏覽器 API 前，請先完成[匯入匯出整合](https://docs.univer.ai/zh-TW/server/import-export.md)。應用程式需要實作與用戶端匹配的上傳、轉換、任務查詢和受權限保護的下載契約。快照 API 仍需要轉換後端，只是不要求持久化協同 Unit。[Exchange 範例](https://docs.univer.ai/zh-TW/server/collaboration/examples.md#exchange)示範 Sheets；其他產品和格式需核對目標版本的支援範圍。

#### Package metadata

```json
{
  "preset": [],
  "plugins": [
    {
      "client": "@univerjs-pro/exchange-client",
      "facade": "@univerjs-pro/exchange-client/facade"
    },
    {
      "client": "@univerjs-pro/pdfs-exchange-client",
      "facade": "@univerjs-pro/pdfs-exchange-client/facade"
    }
  ],
  "license": true,
  "server": true
}
```

## 支援的格式

下表列出本頁前端 API 支援的格式。Office 檔案轉換還需要後端支援對應的文件類型和格式。

| 匯入     | 匯出     |
| ------ | ------ |
| `.pdf` | `.pdf` |

## 選擇資料路徑

| 路徑    | 是否持久化伺服器單元 | 是否需要協作 | 轉換伺服器 |
| ----- | :--------: | :----: | :---: |
| 單元 ID |      是     |    是   |   必要  |
| 快照    |      否     |    否   |   必要  |

### 匯入為持久化單元

輸入可以是瀏覽器 `File` 或 URL。透過已設定的協作客戶端載入傳回的單元：

```ts
const unitId = await univerAPI.importPdfToUnitIdAsync(file)

if (unitId) {
  const pdf = await univerAPI.getCollaboration().loadPdfAsync(unitId)
  console.log(pdf?.getName())
}
```

### 匯入為快照

對於不應作為轉換工作一部分持久化的本機 PDF 單元，請使用快照匯入：

```ts
const snapshot = await univerAPI.importPdfToSnapshotAsync(file)

if (snapshot) {
  univerAPI.createPdf(snapshot)
}
```

### 匯出並下載

依 ID 匯出持久化單元：

```ts
const file = await univerAPI.exportPdfByUnitIdAsync(unitId)

if (file) {
  univerAPI.downloadFile(file, 'project-brief', 'pdf')
}
```

不必先持久化，即可匯出目前的本機快照：

```ts
const pdf = univerAPI.getActivePdf()

if (pdf) {
  const file = await univerAPI.exportPdfBySnapshotAsync(pdf.save())

  if (file) {
    univerAPI.downloadFile(file, pdf.getName(), 'pdf')
  }
}
```

這些 API 傳回 `File`。瀏覽器列印是獨立的工作流程，會開啟系統列印對話方塊；請參閱[列印](https://docs.univer.ai/zh-TW/guides/pdfs/features/print.md)。

## 移除內建匯入匯出選單和 UI 互動

Univer 移除了內建的匯入匯出選單和 UI 互動，底層服務及 Facade API 繼續保留。

如果你的應用程式需要保留原有的匯入匯出選單和 UI 互動，可以參考[內建匯入匯出選單和 UI 互動遷移方案](https://office.univer.ai/showcase/pdfs/custom-import-export)，透過自訂外掛完成遷移。範例提供完整原始碼，支援線上檢視和下載。
