チュートリアル

簡単にライブラリーを使用するためのチュートリアルコレクション

チュートリアル».NET»Spire.Doc for .NET»変換»C#/VB.NET:Word をイメージに変換する(JPG, SVG, PNG)
2022-09-29

C#/VB.NET:Word をイメージに変換する(JPG, SVG, PNG)

コンピュータに MS Word をインストールする必要がないため、Word 文書形式よりも、イメージ書式をプラットフォーム間で共有したりプレビューしたりするのが便利です。また、Word をイメージに変換することで、文書のオリジナルの外観を維持でき、他の人がそれをさらに修正するのを防ぐことができます。この記事では、Spire.Doc for .NET を使用して、C# および VB.NET でプログラムによって Word をイメージに変換する方法を示します。

Spire.Doc for.NET をインストールします

まず、Spire.Doc for.NET パッケージに含まれている DLL ファイルを .NET プロジェクトの参照として追加する必要があります。DLL ファイルは、このリンクからダウンロードするか、NuGet を介してインストールできます。

PM> Install-Package Spire.Doc

Word を JPG に変換する

Spire.Doc for .NET が提供する Document.SaveToImages() メソッドは、Word 文書のページを Bitmap または Metafile に変換します。その後、Bitmap または Metafile は、BMP、EMF、JPEG、PNG、GIF または WMF 書式のイメージとして保存することができます。以下は Word を JPG に変換するための詳細な手順です。

  • Document オブジェクトを作成します。
  • Document.LoadFromFile() メソッドを使用して Word 文書をロードします。
  • Document.SaveToImages() メソッドを使用して、Word 文書を Bitmap イメージに変換します。
  • イメージコレクションをループし、特定のイメージを取得して JPG として保存します。
  • C#
  • VB.NET
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);
            }
        }
    }
}
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

Word を SVG に変換する

Spire.Doc for .NET を使用して Word 文書を一連のバイト配列として保存し、SVG 文書にそれぞれ書き込むことができます。Word を SVG に変換するための詳細な手順は次のとおりです。

  • Document オブジェクトを作成します。
  • Document.LoadFromFile() メソッドを使用して Word 文書をロードします。
  • Document.SaveToSVG() メソッドを使用して、文書をバイト配列キューとして保存します。
  • すべてのバイト配列をループし、指定されたバイト配列を取得する。
  • SVG 文書にバイト配列を書き込みます。
  • C#
  • VB.NET
using Spire.Doc;
using System;
using System.Collections.Generic;
using System.IO;

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

            //Wordをロードする 
            doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Template.docx");

            //文書をバイト配列キューとして保存する
            Queue<byte[]> svgBytes = doc.SaveToSVG();

            //すべてのバイト配列をループする
            for (int i = 0; i < svgBytes.Count; i++)
            {
                //キューを配列に変換する
                byte[][] bytes = svgBytes.ToArray();

                //出力文書名を指定する
                string outputfile = String.Format("‪Image-{0}.svg", i);‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬

                //SVG文書にバイト配列を書き込む                
                FileStream fs = new FileStream("C:\\Users\\Administrator\\Desktop\\Images\\" + outputfile, FileMode.Create);
                fs.Write(bytes[i], 0, bytes[i].Length);
                fs.Close();
            }
        }
    }
}
Imports Spire.Doc
Imports System
Imports System.Collections.Generic
Imports System.IO
 
Namespace CovnertWordToSVG
    Class Program
        Shared  Sub Main(ByVal args() As String)
            'Documentオブジェクトを作成する
            Document doc  =  New Document()
 
            'Wordをロードする
            doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Template.docx")
 
            '文書をバイト配列キューとして保存する
            Dim svgBytes()> As Queue<byte =  doc.SaveToSVG() 
 
            'すべてのバイト配列をループする
            Dim i As Integer
            For  i = 0 To  svgBytes.Count- 1  Step  i + 1
                'キューを配列に変換する
                Dim bytes()() As Byte =  svgBytes.ToArray() 
 
                '出力文書名を指定する
                Dim outputfile As String =  String.Format("‪Image-{0}.svg",i) ‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬
 
                ' SVG文書にバイト配列を書き込む
                Dim fs As FileStream =  New FileStream("C:\\Users\\Administrator\\Desktop\\Images\\" + outputfile,FileMode.Create) 
                fs.Write(bytes(i), 0, bytes(i).Length)
                fs.Close()
            Next
        End Sub
    End Class
End Namespace

Word をカスタム解像度の PNG に変換する

より解像度の高いイメージはコンテンツをより明確に表示することができます。以下の手順に従って、Word を PNG に変換するときに、イメージ解像度を設定することができます。

  • Document オブジェクトを作成します。
  • Document.LoadFromFile() メソッドを使用して Word 文書をロードします。
  • Document.SaveToImages() メソッドを使用して、文書を Bitmap イメージに変換します。
  • イメージコレクションをループし、特定のビットマップを取得します。
  • カスタム ResetResolution() メソッドを使用して、イメージの解像度を再設定します。
  • イメージを PNG として保存します。
  • C#
  • VB.NET
using Spire.Doc;
using System;
using System.Drawing;
using System.Drawing.Imaging;
using Spire.Doc.Documents;

namespace ConvertWordToPng
{
    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.Metafile);

            //イメージコレクションをループする
            for (int i = 0; i < images.Length; i++)
            {
                //イメージの解像度を設定する
                Image newimage = ResetResolution(images[i] as Metafile, 150);

                //イメージをPNGとして保存する
                string outputfile = String.Format("image-{0}.png", i);
                newimage.Save(outputfile, ImageFormat.Png);
            }
        }

        //ResetResolution()を使用しでイメージの解像度を設定する
        public static Image ResetResolution(Metafile mf, float resolution)
        {
            int width = (int)(mf.Width * resolution / mf.HorizontalResolution);
            int height = (int)(mf.Height * resolution / mf.VerticalResolution);
            Bitmap bmp = new Bitmap(width, height);
            bmp.SetResolution(resolution, resolution);
            using (Graphics g = Graphics.FromImage(bmp))
            {
                g.DrawImage(mf, Point.Empty);
            }
            return bmp;
        }
    }
}
Imports Spire.Doc
Imports System
Imports System.Drawing
Imports System.Drawing.Imaging
Imports Spire.Doc.Documents

Namespace ConvertWordToPng
    Class Program
        Shared Sub Main(ByVal args() As String)
            'Documentオブジェクトを作成する
            Dim doc As Document = New Document()

            'Word文書をロードする
            doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Template.docx")

            '文書全体のページをイメージに変換する
            Dim images() As Image = doc.SaveToImages(ImageType.Metafile)

            'イメージコレクションをループする
            Dim i As Integer
            For i = 0 To images.Length - 1 Step i + 1
                'イメージの解像度を設定する
                Dim Newimage As Image = ResetResolution(images(i) As Metafile, 150)

                'イメージをPNGとして保存する
                Dim outputfile As String = String.Format("image-{0}.png", i)
                Newimage.Save(outputfile, ImageFormat.Png)
            Next
        End Sub

        'ResetResolution()を使用しでイメージの解像度を設定する
        Public Shared Function ResetResolution(ByVal mf As Metafile, ByVal resolution As Single) As Image
            Dim width As Integer = CType((mf.Width * resolution / mf.HorizontalResolution), Integer)
            Dim height As Integer = CType((mf.Height * resolution / mf.VerticalResolution), Integer)
            Dim bmp As Bitmap = New Bitmap(width, height)
            bmp.SetResolution(resolution, resolution)
            Imports (Graphics g = Graphics.FromImage(bmp))
            {
                g.DrawImage(mf, PoInteger.Empty)
            }
            Return bmp
        End Function
    End Class
End Namespace

C#/VB.NET:Word をイメージに変換する(JPG, SVG, PNG)

一時ライセンスを申請する

結果ドキュメントから評価メッセージを削除したい場合、または機能制限を取り除く場合は、についてこのメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。にお問い合わせ、30 日間有効な一時ライセンスを取得してください。

Read 834 times