IronPDF C#でRTFをPDFに変換する方法
IronPDFはPdfDocumentを返します。これを保存したり、セキュリティを強化したり、結合したりすることができます。
RTF(リッチテキスト形式)は、Microsoftが1987年に導入したドキュメント形式です。RTFはフォント、スタイル、色、箇条書きリストなどの書式設定コマンドをテキストと共にプレーンテキストとして保存するため、異なるエディタ間での互換性が高いです。 RTFをPDFに変換することで、どのデバイスでも同じように表示され、印刷やアーカイブに適した固定レイアウトのファイルを作成できます。また、パスワード保護を設定することも可能です。
IronPDFはRTFを文書変換パスを通じてレンダリングします。これはRTFマークアップを直接解釈し、HTMLからPDFへの変換に使用するChromeのHTMLエンジンを介しません。 太字、斜体、下線、色、フォント、シンプルな箇条書きなどの標準的なフォーマットは、確実に変換されます。 RTFのコントロールワード、ネストされた表、埋め込まれたOLEオブジェクトなどの、あまり一般的でないものは、完全には往復できない可能性がありますので、ドキュメントが高度な機能を使用している場合、出力を確認してください。
クイックスタート: IronPDFを使用して RTF を PDF に変換するリッチテキストフォーマットファイルをPDFに一度でレンダリングします。 RTFのパスにRenderRtfFileAsPdfを指定し、返されたドキュメントを保存してください。
-
1Install IronPDF with NuGet Package Manager
-
2このコード スニペットをコピーして実行します。
new IronPdf.ChromePdfRenderer() .RenderRtfFileAsPdf("input.rtf") .SaveAs("output.pdf");C# -
3実際の環境でテストするためにデプロイする
今日プロジェクトで IronPDF を使い始めましょう無料トライアル
最小限のワークフロー(5ステップ)
- RTFをPDFに変換するC#ライブラリをダウンロードする。
- 変換したいRTFファイルまたは文字列を準備する
- RTF文字列から変換するには、
RenderRtfStringAsPdfメソッドを使用してください。 - RTFファイルから変換するには、
RenderRtfFileAsPdfメソッドを使用してください。 - 生成されたPDFドキュメントを確認する
RTF文字列をPDFに変換する方法
RTF文字列をPDFドキュメントに変換するためにRenderRtfStringAsPdfメソッドを使用してください。 文字列入力は、ランタイムで構築するコンテンツに適しています:データベースの列から取り出したRTF、APIによって返されたもの、またはコードで組み立てられたものです。書き込みや掃除が必要な一時ファイルはありません。このメソッドはPdfDocumentを返すため、結果に作業を続け、ヘッダーとフッターの適用、ページの結合、注釈やブックマークの追加を行えます。
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")出力
追加の書式オプションを適用するにはどうすればよいですか?
HTMLレンダリングで使用するのと同じRenderingOptionsがここでも適用されます。 RenderRtfStringAsPdfを呼び出す前に、レンダラーに用紙の向き、余白、およびページサイズを設定してください。
using IronPdf;
using IronPdf.Rendering;
// Create renderer with custom options
ChromePdfRenderer renderer = new ChromePdfRenderer();
renderer.RenderingOptions.PaperOrientation = PdfPaperOrientation.Landscape;
renderer.RenderingOptions.MarginTop = 50;
renderer.RenderingOptions.MarginBottom = 50;
renderer.RenderingOptions.MarginLeft = 25;
renderer.RenderingOptions.MarginRight = 25;
// RTF string with formatting
string rtf = @"{\rtf1\ansi\deff0{\fonttbl{\f0 Times New Roman;}{\f1 Arial;}}
{\colortbl;\red255\green0\blue0;\red0\green0\blue255;}
\f0\fs24 This is a heading\line
\f1\fs20\cf1 This text is red\line
\cf2 This text is blue\line
\b Bold text\b0 and \i italic text\i0}";
// Convert with custom options
PdfDocument pdf = renderer.RenderRtfStringAsPdf(rtf);
pdf.SaveAs("formattedRtfToPdf.pdf");Imports IronPdf
Imports IronPdf.Rendering
' Create renderer with custom options
Dim renderer As New ChromePdfRenderer()
renderer.RenderingOptions.PaperOrientation = PdfPaperOrientation.Landscape
renderer.RenderingOptions.MarginTop = 50
renderer.RenderingOptions.MarginBottom = 50
renderer.RenderingOptions.MarginLeft = 25
renderer.RenderingOptions.MarginRight = 25
' RTF string with formatting
Dim rtf As String = "{\rtf1\ansi\deff0{\fonttbl{\f0 Times New Roman;}{\f1 Arial;}}" & _
"{\colortbl;\red255\green0\blue0;\red0\green0\blue255;}" & _
"\f0\fs24 This is a heading\line" & _
"\f1\fs20\cf1 This text is red\line" & _
"\cf2 This text is blue\line" & _
"\b Bold text\b0 and \i italic text\i0}"
' Convert with custom options
Dim pdf As PdfDocument = renderer.RenderRtfStringAsPdf(rtf)
pdf.SaveAs("formattedRtfToPdf.pdf")ページの形状をより細かく制御するには、カスタム余白とカスタム用紙サイズに関するガイドをご覧ください。
出力
RTFファイルをPDFに変換するにはどうすればよいですか?
ドキュメントのパスをRenderRtfFileAsPdfに渡します。 このメソッドは絶対パスと相対パスの両方を受け付けます。 サンプルRTFファイルをダウンロードして一緒に進めてください: sample.rtf。 以下の例では、そのファイルをPDFにレンダリングします。
入力

RTFファイルを変換するにはどのようなコードが必要ですか?
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")フォルダ内の文書を変換するには、1つのレンダラーを再利用し、ファイルをループ処理してください。
using IronPdf;
using System.IO;
// Create renderer once for efficiency
ChromePdfRenderer renderer = new ChromePdfRenderer();
// Get all RTF files in directory
string[] rtfFiles = Directory.GetFiles(@"C:\RTFDocuments", "*.rtf");
foreach (string rtfFile in rtfFiles)
{
// Convert each RTF file to PDF
PdfDocument pdf = renderer.RenderRtfFileAsPdf(rtfFile);
// Save with same name but PDF extension
string pdfPath = Path.ChangeExtension(rtfFile, ".pdf");
pdf.SaveAs(pdfPath);
}Imports IronPdf
Imports System.IO
' Create renderer once for efficiency
Dim renderer As New ChromePdfRenderer()
' Get all RTF files in directory
Dim rtfFiles As String() = Directory.GetFiles("C:\RTFDocuments", "*.rtf")
For Each rtfFile As String In rtfFiles
' Convert each RTF file to PDF
Dim pdf As PdfDocument = renderer.RenderRtfFileAsPdf(rtfFile)
' Save with same name but PDF extension
Dim pdfPath As String = Path.ChangeExtension(rtfFile, ".pdf")
pdf.SaveAs(pdfPath)
Next出力
RTFファイルを変換するときによくある問題は何ですか?
RTFファイルをレンダリングする際に注意が必要な状況がいくつかあります:
- フォント: RTFで指定されているフォントは、変換を実行するマシンにインストールされている必要があります。これは、特にヘッドレスサーバーやコンテナで重要です。 フォント管理ガイドでは、実行時にフォントを登録する方法について説明しています。
- 大きなドキュメント:アプリケーションが応答性を保つように、非同期APIを使用してリクエストスレッド外で長いファイルをレンダリングします。
- 国際文字: 非ラテン文字のスクリプトを含むドキュメントでは、ソースRTFの正しいエンコーディングと、それらのグリフをカバーするインストールされたフォントが必要です。
- 高度なマークアップ: 入れ子になったテーブルや埋め込みオブジェクトは正確に再現されない可能性があります。 ドキュメントがそれらに依存している場合、出力をソースと比較します。
変換したPDFにセキュリティを追加するにはどうすればよいですか?
レンダリング後、SecuritySettingsを通じてドキュメントを保護し、オーナーパスワード、ユーザーパスワード、およびコピーと印刷の許可を設定します。
using IronPdf;
using IronPdf.Security;
// Convert RTF to PDF
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderRtfFileAsPdf("confidential.rtf");
// Apply security settings
pdf.SecuritySettings.MakePdfDocumentReadOnly("owner_password");
pdf.SecuritySettings.UserPassword = "user_password";
pdf.SecuritySettings.AllowUserCopyPasteContent = false;
pdf.SecuritySettings.AllowUserPrinting = PdfPrintSecurity.NoPrint;
// Save secured PDF
pdf.SaveAs("secured_document.pdf");
PDF権限とパスワードガイドでは、制限オプションの全セットをカバーしています。
複数のRTFファイルを1つのPDFに結合するにはどうすればよいですか?
各RTFファイルをそれぞれのPdfDocument.Mergeを呼び出して結合します。 これは、レポートの章など、いくつかのソースドキュメントから1つの成果物を作成します。
using IronPdf;
using System.Collections.Generic;
// Create renderer
ChromePdfRenderer renderer = new ChromePdfRenderer();
// Convert multiple RTF files
List<PdfDocument> pdfs = new List<PdfDocument>();
pdfs.Add(renderer.RenderRtfFileAsPdf("chapter1.rtf"));
pdfs.Add(renderer.RenderRtfFileAsPdf("chapter2.rtf"));
pdfs.Add(renderer.RenderRtfFileAsPdf("chapter3.rtf"));
// Merge PDFs
PdfDocument mergedPdf = PdfDocument.Merge(pdfs);
// Add page numbers across the merged document
mergedPdf.AddTextFooters(new TextHeaderFooter
{
CenterText = "Page {page} of {total-pages}",
FontSize = 12
});
// Save combined document
mergedPdf.SaveAs("combined_document.pdf");
// Clean up individual PDFs
foreach (var pdf in pdfs)
{
pdf.Dispose();
}
フッター用のプレースホルダー{total-pages}は結合された結果全体のページ番号を付けます。
Webエンドポイントから変換されたPDFを返す方法
ASP.NETで、アクション内でRTFをレンダリングし、ドキュメントのバイトをファイル結果として返します。 Fileに引き渡します。
[HttpGet]
public IActionResult ConvertRtfToPdf(string rtfContent)
{
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderRtfStringAsPdf(rtfContent);
return File(pdf.BinaryData, "application/pdf", "converted_document.pdf");
}
結論
IronPDFはPdfDocumentはレイアウト、セキュリティ、および結合のためにライブラリの残りの部分に組み込まれます。 ここから、上記に示されたカスタム余白およびヘッダーとフッターを使用してRTF変換を行うか、完全なPDF変換のリストに進んでください。
よくある質問
C#でRTFファイルをPDFに変換するにはどうすればよいですか?
IronPDFを使えば、RenderRtfFileAsPdfメソッドを使って、たった2行のコードでRTFファイルをPDFに変換することができます。ChromePdfRendererインスタンスを作成し、入力ファイルのパスを指定してRenderRtfFileAsPdfを呼び出し、SaveAsメソッドで結果を保存するだけです。
RTF文字列をファイルとして保存せずに、直接PDFに変換できますか?
はい、IronPDFはRenderRtfStringAsPdfメソッドを提供し、文字列のRTFコンテンツを直接PDFドキュメントに変換することができます。これは動的に生成されたRTFコンテンツやデータベースのデータを扱うときに特に便利です。
RTFからPDFへの変換では、書式やスタイルは保持されますか?
IronPDFはドキュメント変換パスを通じてRTFをレンダリングしますので、フォント、スタイル、色、ボールド、イタリック、およびシンプルな箇条書きリストなどの標準フォーマットは確実に変換されます。あまり一般的でない制御ワード、ネストされた表、および埋め込みのOLEオブジェクトは正確にラウンドトリップしないかもしれませんので、ドキュメントが高度な機能を使用している場合には出力を確認してください。
RTFをPDFに変換するメリットは何ですか?
IronPDFでRTFをPDFに変換することで、プラットフォーム間での容易なアクセシビリティ、圧縮機能、印刷の最適化、表示デバイスに関係なく一貫したドキュメントの外観、強化されたセキュリティ機能、プロフェッショナルなドキュメントの配布準備など、いくつかの利点が得られます。
変換したPDFにヘッダー、フッター、ページ番号を追加できますか?
はい、IronPDFはRTFをPDFに変換する際にRenderingOptionsをフルサポートしています。テキストとHTMLのヘッダーとフッターの適用、ページ番号の追加、テキストと画像のスタンプの実装、ページサイズと向きのカスタマイズが可能です。
RTFから変換した後、PDFを操作することは可能ですか?
もちろんです!IronPDFを使ってRTFからPDFを生成した後、PDFの結合や分割、ページの回転、注釈の追加、しおりの実装など様々なページ操作を行うことができます。
How can I return a converted PDF from a web endpoint using IronPDF?
In ASP.NET, you can convert RTF to PDF and return it from a web endpoint by reading the `PdfDocument`'s `BinaryData` and returning it in the `File` result of an action.
What should I do if my RTF file uses fonts not installed on my conversion machine?
Ensure that the required fonts are installed on the machine performing the conversion or use IronPDF's guides to register fonts at runtime when converting RTF to PDF.
How does IronPDF handle international characters in RTF conversion?
International characters in RTF documents require proper encoding in the source and a suitable font that supports the required glyphs during the conversion process.
Is there a way to convert all RTF files in a directory to PDFs in IronPDF?
Yes, by using a loop to iterate over all RTF files in a directory, you can convert each file to a PDF with IronPDF, ensuring you manage a single `ChromePdfRenderer` instance for efficiency.

Curtis Chauは、カールトン大学でコンピュータサイエンスの学士号を取得し、Node.js、TypeScript、JavaScript、およびReactに精通したフロントエンド開発を専門としています。直感的で美しいユーザーインターフェースを作成することに情熱を持ち、Curtisは現代のフレームワークを用いた開発や、構造の良い視覚的に魅力的なマニュアルの作成を楽しんでいます。