0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

C#/VB.NET Word をJPGに変換する方法

Last updated at Posted at 2023-03-02

ドキュメントを編集するとき、Wordは非常に良い選択です。しかし、保存と転送の際には、Wordを他のドキュメント形式に変換することが考えられます。例えば、WordをJPGに変換します。プログラミングによる変換を考えるなら、Free Spire.Doc for .NETライブラリが役に立つかもしれません。以下に詳細な変換方法とコードを示します。

Free Spire.Doc for .NETをインストールする

方法1NuGetでFree Spire.Doc for .NETをインストールする
Visual Studioを開いて新しいプロジェクトを作成します。次に、「Solution Explorer」で「References」を右クリックし、「Nuget Manage Packages」を選択します。Free Spire.Doc for .NETを検索してインストールします。
方法2:手動でSpire.doc.dllを追加する
Free Spire.Doc for .NETをローカルにダウンロードしインストールします。次に、Visual Studioて新しいプロジェクトを作成し、右側の「Solution Explorer」で「References」を右クリックし、「Add Reference」>「Browse」を選択して、インストールパス下のBINフォルダのdllファイルを見つけ、「OK」をクリックして、プログラムに参照を追加します。

Word をJPGに変換する方法

  • Document オブジェクトを作成します。
  • Document.LoadFromFile()メソッドを使用して Word 文書を読み込みます。
  • Document.SaveToImages()メソッドを使用して、Word 文書を Bitmap 画像に変換します。
  • 画像コレクションをループし、特定の画像を取得して JPG として保存します。
    C#:
using Spire.Doc;
using Spire.Doc.Documents;
using System;
using System.Drawing;
using System.Drawing.Imaging;

namespace ConvertWordToJPG
{
    class Program
    {
        static void Main(string[] args)
        {
            //Documentオブジェクトを作成する
            Document doc = new Document();

            //Wordを読み込む
            doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Template.docx");

            //文書全体のページを画像に変換する 
            Image[] images = doc.SaveToImages(ImageType.Bitmap);

            //画像コレクションをループする
            for (int i = 0; i < images.Length; i++)
            {
                //画像をJPGとして保存する
                string outputfile = String.Format("‪Image-{0}.jpg", i);‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬
                images[i].Save("C:\\Users\\Administrator\\Desktop\\Images\\" + outputfile, ImageFormat.Jpeg);
            }
        }
    }
}

VB.Net:

Imports Spire.Doc
Imports Spire.Doc.Documents
Imports System
Imports System.Drawing
Imports System.Drawing.Imaging
 
Namespace ConvertWordToJPG
    Class Program
        Shared  Sub Main(ByVal args() As String)
            'Documentオブジェクトを作成する
            Document doc  =  New Document()
 
            'Wordを読み込む
           doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Template.docx")

            '文書全体のページを画像に変換する 
            Dim images() As Image =  doc.SaveToImages(ImageType.Bitmap) 
 
            '画像コレクションをループする
            Dim i As Integer
            For  i = 0 To  images.Length- 1  Step  i + 1
                '画像をJPGとして保存する
                Dim outputfile As String =  String.Format("‪Image-{0}.jpg",i) ‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬
                images(i).Save("C:\\Users\\Administrator\\Desktop\\Images\\" + outputfile, ImageFormat.Jpeg)
            Next
        End Sub
    End Class
End Namespace

image.png

0
2
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
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?