如何使用 C# 將 RTF 轉換為 PDF | IronPDF

How to Convert Rich Text Format to PDF

This article was translated from English: Does it need improvement?
Translated
View the article in English

RTF 代表富文本格式,是由微軟開發的一種文檔文件格式。 它支持文本格式,如字體、樣式和圖像,但功能較其他如 DOCX 或 PDF 的文檔格式更少。

IronPDF 可用於將 RTF 從字符串和文件轉換為 PDF。 將 RTF 轉換為 PDF 提供了若干好處,如易於訪問、可壓縮且優化打印。 這確保您的文檔在所有平台上的外觀一致、安全且準備好打印。

快速入門:使用 IronPDF 轉換 RTF 為 PDF

使用 IronPDF 開始將您的 RTF 文件轉換為高質量的 PDF。 只需兩行代碼,您就可以將富文本格式文檔轉換為安全、可訪問的 PDF,準備分發和打印。 IronPDF 提供了一種簡單且高效的方法來處理 RTF 到 PDF 的轉換,確保無縫集成到您的 .NET C# 項目中。

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronPDF with NuGet Package Manager

    PM > Install-Package IronPdf

  2. Copy and run this code snippet.

    new IronPdf.ChromePdfRenderer()
         .RenderRtfFileAsPdf("input.rtf")
         .SaveAs("output.pdf");
  3. Deploy to test on your live environment

    Start using IronPDF in your project today with a free trial
    arrow pointer
class="hsg-featured-snippet">

最小工作流程(5 步)

  1. 下載用於將 RTF 轉換為 PDF 的 C# 庫
  2. 準備您希望轉換的 RTF 文件或字符串
  3. 使用 RenderRtfStringAsPdf 方法從 RTF 字符串轉換
  4. 使用 RenderRtfFileAsPdf 方法從 RTF 文件轉換
  5. 查看生成的 PDF 文檔


將 RTF 字符串轉換為 PDF 示例

使用 RenderRtfStringAsPdf 方法將 RTF 字符串轉換為 PDF 文檔。 The full range of RenderingOptions, such as applying text and HTML headers and footers, text and image stamping, page numbering, custom page sizes and orientations, is also applicable to this rendering method. After the PDF document has been generated, you can perform PDF page manipulations like merging, splitting, and rotating pages, as well as applying annotations and bookmarks.

:path=/static-assets/pdf/content-code-examples/how-to/rtf-to-pdf-from-string.cs
using IronPdf;

// Instantiate Renderer
ChromePdfRenderer renderer = new ChromePdfRenderer();

// RTF string
string rtf = @"{\rtf1\ansi\deff0{\fonttbl{\f0 Arial;}}{\colortbl;\red0\green0\blue0;}\cf0This is some \b bold \b0 and \i italic \i0 text.}";

// Render from RTF string
PdfDocument pdf = renderer.RenderRtfStringAsPdf(rtf);

// Save the PDF
pdf.SaveAs("pdfFromRtfString.pdf");
Imports IronPdf

' Instantiate Renderer
Private renderer As New ChromePdfRenderer()

' RTF string
Private rtf As String = "{\rtf1\ansi\deff0{\fonttbl{\f0 Arial;}}{\colortbl;\red0\green0\blue0;}\cf0This is some \b bold \b0 and \i italic \i0 text.}"

' Render from RTF string
Private pdf As PdfDocument = renderer.RenderRtfStringAsPdf(rtf)

' Save the PDF
pdf.SaveAs("pdfFromRtfString.pdf")
$vbLabelText   $csharpLabel

將 RTF 文件轉換為 PDF 示例

要將 RTF 文件轉換為 PDF,請使用 RenderRtfFileAsPdf 方法。 您可以從提供的鏈接下載示例 RTF 文件。 此示例將演示如何將該示例文件轉換為 PDF 文檔。

在 Microsoft Word 中預覽 RTF 文件

class="content-img-align-center">
class="center-image-wrapper"> 在 Microsoft Word 上打開 RTF 文件

代碼範例

:path=/static-assets/pdf/content-code-examples/how-to/rtf-to-pdf-from-file.cs
using IronPdf;

// Instantiate Renderer
ChromePdfRenderer renderer = new ChromePdfRenderer();

// Render from RTF file
PdfDocument pdf = renderer.RenderRtfFileAsPdf("sample.rtf");

// Save the PDF
pdf.SaveAs("pdfFromRtfFile.pdf");
Imports IronPdf

' Instantiate Renderer
Private renderer As New ChromePdfRenderer()

' Render from RTF file
Private pdf As PdfDocument = renderer.RenderRtfFileAsPdf("sample.rtf")

' Save the PDF
pdf.SaveAs("pdfFromRtfFile.pdf")
$vbLabelText   $csharpLabel

輸出PDF

準備看看您還能做哪些其他事情嗎? 在這裡查看我們的教程頁面:轉換PDF

常見問題解答

什麼是富文本格式(RTF)?

RTF,即富文本格式,是由微軟開發的一種文件文件格式。它支援字體、樣式和圖像等文字格式,但缺乏DOCX或PDF等格式所具有的進階功能。

為什麼要將RTF文檔轉換為PDF?

將 RTF 文件轉換為 PDF 可確保文件易於存取、跨平台格式一致、針對列印進行了最佳化,並且更加安全。

如何在C#中將RTF檔案轉換為PDF?

您可以使用 C# 中的RenderRtfFileAsPdf方法將 RTF 檔案轉換為 PDF 檔案。此方法是 IronPDF 庫的一部分,該庫簡化了轉換過程。

我可以用 C# 將 RTF 字串轉換為 PDF 文件嗎?

是的,您可以使用 IronPDF 庫中提供的RenderRtfStringAsPdf方法將 RTF 字串轉換為 PDF 文件。

將 RTF 檔案轉換為 PDF 檔案時,有哪些渲染選項可用?

IronPDF 提供多種渲染選項,包括文字和 HTML 頁眉/頁腳、文字和圖像浮水印、頁碼以及自訂頁面大小和方向。

將 RTF 格式轉換為 PDF 格式後,如何操作 PDF 頁面?

將 RTF 轉換為 PDF 後,您可以使用 IronPDF 來操作 PDF 頁面,例如合併、分割、旋轉頁面,以及新增註解和書籤。

我使用 IronPDF 進行 RTF 轉 PDF 轉換需要許可證嗎?

是的,要使用 IronPDF 將 RTF 文件轉換為 PDF,需要試用版或完整版授權。

哪裡可以找到 RTF 轉 PDF 的 C# 程式碼範例?

IronPDF 網站上提供了使用 IronPDF 將 RTF 檔案和字串轉換為 PDF 的程式碼範例,並示範了RenderRtfFileAsPdfRenderRtfStringAsPdf方法的使用。

哪裡可以下載 IronPDF 庫?

您可以從 NuGet(.NET 的套件管理器)下載 IronPDF 庫。

PDF 相較於 RTF 有哪些優點?

PDF 格式檔案被廣泛支持,格式一致,易於共享,與 RTF 文件相比,通常提供更多的安全功能。

IronPDF 是否完全相容於 .NET 10,能夠將 RTF 檔案轉換為 PDF 檔案?

是的-IronPDF 完全相容 .NET 10。在 .NET 10 應用程式(包括 Web、桌面和微服務環境)中,使用RenderRtfFileAsPdfRenderRtfStringAsPdf等方法將 RTF 檔案或字串轉換為 PDF 的功能開箱即用。無需依賴舊版或已棄用的 API。

A PHP Error was encountered

Severity: Warning

Message: Illegal string offset 'name'

Filename: sections/author_component.php

Line Number: 18

Backtrace:

File: /var/www/ironpdf.com/application/views/main/sections/author_component.php
Line: 18
Function: _error_handler

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 63
Function: view

File: /var/www/ironpdf.com/application/views/products/sections/three_column_docs_page_structure.php
Line: 64
Function: main_view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/views/products/how-to/index.php
Line: 2
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 552
Function: view

File: /var/www/ironpdf.com/application/controllers/Products/Howto.php
Line: 31
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: Illegal string offset 'title'

Filename: sections/author_component.php

Line Number: 38

Backtrace:

File: /var/www/ironpdf.com/application/views/main/sections/author_component.php
Line: 38
Function: _error_handler

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 63
Function: view

File: /var/www/ironpdf.com/application/views/products/sections/three_column_docs_page_structure.php
Line: 64
Function: main_view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/views/products/how-to/index.php
Line: 2
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 552
Function: view

File: /var/www/ironpdf.com/application/controllers/Products/Howto.php
Line: 31
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: Illegal string offset 'comment'

Filename: sections/author_component.php

Line Number: 48

Backtrace:

File: /var/www/ironpdf.com/application/views/main/sections/author_component.php
Line: 48
Function: _error_handler

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 63
Function: view

File: /var/www/ironpdf.com/application/views/products/sections/three_column_docs_page_structure.php
Line: 64
Function: main_view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/views/products/how-to/index.php
Line: 2
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 552
Function: view

File: /var/www/ironpdf.com/application/controllers/Products/Howto.php
Line: 31
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

準備好開始了嗎?
Nuget 下載 16,154,058 | 版本: 2025.11 剛剛發布