0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

C#でRTFをWord、PDF、HTML、画像に変換する

Posted at

RTF文書は、そのクロスプラットフォーム互換性から広く使用されていますが、シナリオによって特定の文書フォーマットが必要になる場合もあります。例えば、Word文書は編集や共同作業に、PDF文書は印刷や配布に、HTML文書はオンラインプレゼンテーションに、画像フォーマットはソーシャルメディアでの共有に適しています。
そのため、文書をさまざまな環境で開いて使用できるように、RTFを他の形式に変換する必要があるかもしれません。この記事では、C#を通じてRTFをWord、PDF、HTML、画像形式に変換する方法を紹介するために、いくつかのコードサンプルを提供します。

無料の.NETライブラリをインストールする:

RTF文書の変換には、フリーライブラリFree Spire.Doc for .NETを使用できます。以下のリンクからダウンロードし、手動でdllをインポートするか、Nugetを通して直接インストールすることができます。

RTFを他の文書形式に変換する手順:

  1. 必要なクラスをインポートする
  2. LoadFromFile(string fileName, FileFormat.Rtf) メソッドでRTFファイルを読み込みます。
  3. SaveToFile(string fileName, FileFormat fileFomat) メソッドを呼び出して、RTFファイルを指定されたファイル形式に保存します。

C#でRTFをWord(Doc/Docx)に変換する

using Spire.Doc;
using System;

public class RtfToDocDocx
{

    public static void Main(String[] args)
    {
        // RTF文書を読み込む
        Document document = new Document();
        document.LoadFromFile("input.rtf", FileFormat.Rtf);

        // RTFをDoc形式に保存
        document.SaveToFile("toDoc.doc", FileFormat.Doc);
        // RTFをDocx形式に保存
        document.SaveToFile("toDocx.docx", FileFormat.Docx2013);
    }
}

C#でRTFをPDFファイルに変換する

using Spire.Doc;

namespace RTFtoPDF
{
    class Program
    {
        static void Main(string[] args)
        {
            // RTF文書を読み込む
            Document document = new Document();
            document.LoadFromFile("input.rtf", FileFormat.Rtf);

            // RTFをPDFファイルに保存
            document.SaveToFile("RTFtoPDF.pdf", FileFormat.PDF);
        }
    }
}

C#でRTFをHTML形式に変換する

using Spire.Doc;

namespace ConvertRtfToHtml
{
    class Program
    {
        static void Main(string[] args)
        {
            // RTF文書を読み込む
            Document document = new Document();
            document.LoadFromFile("input.rtf", FileFormat.Rtf);

            // RTFをHTML形式に保存
            document.SaveToFile("RtfToHtml.html", FileFormat.Html);
        }
    }
}

C#でRTFをJPG/PNG画像に変換する

Spire.Doc for .NETは、読み込んだRTFドキュメントを画像に変換するための SaveToImages() メソッドを提供し、これらの画像をJPEG、PNG、BMP、EMFやその他の一般的な画像フォーマットに保存することができます。

using Spire.Doc;
using System.Drawing.Imaging;
using System.Drawing;
using Spire.Doc.Documents;

namespace ConvertRtfToImage

{
    class Program
    {
        static void Main(string[] args)
        {
            // RTF文書を読み込む
            Document document = new Document();
            document.LoadFromFile("input.rtf", FileFormat.Rtf);

            // RTF文書を画像に変換する
            Image[] images = document.SaveToImages(ImageType.Bitmap);

            // 画像コレクションを反復処理する
            for (int i = 0; i < images.Length; i++)
            {
                // 画像をpng形式で保存する
                string outputfile = string.Format("image-{0}.png", i);
                images[i].Save(outputfile, ImageFormat.Png);
            }
        }
    }
}

RTFドキュメントを他のフォーマットに変換することは、一般的なニーズであるだけでなく、文書処理の不可欠な部分です。この記事の紹介を通じて、あなたはC#でRTF文書をWord、PDF、HTML、画像に変換する基本的な操作を習得することができます。

C#でWord文書を操作する方法については、こちらをご覧ください:

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?