C# Word转为多种格式文件(Word转XPS/SVG/EMF/EPUB/TIFF)

简介: 一款有着强大的文档转换功能的工具,无论何时何地都会是现代办公环境极为需要的。在本篇文章中,将继续介绍关于Word文档的转换功能(Word转XPS/SVG/EMF/EPUB/TIFF)希望方法中的代码能为各位开发者们提供一定的参考价值。

一款有着强大的文档转换功能的工具,无论何时何地都会是现代办公环境极为需要的。在本篇文章中,将继续介绍关于Word文档的转换功能(Word转XPS/SVG/EMF/EPUB/TIFF)希望方法中的代码能为各位开发者们提供一定的参考价值。

PS:更多Word转换功能可以参阅这两篇文章

使用工具Free Spire.Doc for .NET

使用方法下载安装该控件后,在VS控制台应用程序中添加引用Spire.Doc.dll文件(dll文件可在该安装文件夹下Bin中获取)

1. Word转XPS

using Spire.Doc;
using System;

namespace WordtoXPS_Doc
{
    class Program
    {
        static void Main(string[] args)
        {
            //初始化String类,元素为需要转换的Word文档
            String file = "sample.docx";
            //创建一个Document类对象,加载sample文件
            Document doc = new Document(file);
            //将Word文件保存为XPS,并运行生成的文档
            doc.SaveToFile("Word2XPS.xps", FileFormat.XPS);
            System.Diagnostics.Process.Start("Word2XPS.xps");
        }
    }
}

调试运行该项目生成文档,如下图:

2.  WordSVG

using Spire.Doc;

namespace WordtoSVG_Doc
{
    class Program
    {
        static void Main(string[] args)
        {
            //实例化Document类,并加载Word sample
            Document doc = new Document();
            doc.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.docx");
            //保存为svg格式
            doc.SaveToFile("result.svg", FileFormat.SVG);
        }
    }
}

3. WordEmf

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

namespace WordtoEmf_Doc
{
    class Program
    {
        static void Main(string[] args)
        {
            //实例化一个Document类,并加载Word sample
            Document doc = new Document();
            doc.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.docx", FileFormat.Docx);

            //调用方法 SaveToImages()将Word第一页转为image并保存为Emf格式
            System.Drawing.Image image = doc.SaveToImages(0, Spire.Doc.Documents.ImageType.Metafile);
            image.Save("WordtoEmf.emf", ImageFormat.Emf);
        }
    }
}

4.  WordEpub

using Spire.Doc;

namespace WordtoEPUB
{
    class Epub
    {
        static void Main(string[] args)
        {
            //实例化Document类,并加载Word sample
            Document document = new Document();
            document.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.docx");

            //保存为Epub格式,并运行生成的文档
            document.SaveToFile("ToEpub.epub", FileFormat.EPub);
            System.Diagnostics.Process.Start("ToEpub.epub");
        }
    }
}

5. WordWord XML

using Spire.Doc;

namespace WordtoWordXML_Doc
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建一个Document类对象并加载Word sample
            Document doc = new Document();
            doc.LoadFromFile("sample.docx");
            //调用方法SaveToFile()保存Word为Word Xml
            doc.SaveToFile("WordToWordXML.xml", FileFormat.WordXml);
        }
    }
}

6.   WordTiff

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

namespace convert_word_to_tiff
{
    class Program
    {
        static void Main(string[] args)
        {
            //实例化一个Document类,加载Word sample
            Document document = new Document(@"C:\Users\Administrator\Desktop\sample.docx");

            //调用方法JoinTiffImages()将Word保存为tiff格式,并运行生成的文档
            JoinTiffImages(SaveAsImage(document), "result.tiff", EncoderValue.CompressionLZW);
            System.Diagnostics.Process.Start("result.tiff");
        }
        //自定义方法SaveAsImage()将Word文档保存为图像
        private static Image[] SaveAsImage(Document document)
        {
            Image[] images = document.SaveToImages(ImageType.Bitmap);
            return images;
        }
        private static ImageCodecInfo GetEncoderInfo(string mimeType)
        {
            ImageCodecInfo[] encoders = ImageCodecInfo.GetImageEncoders();
            for (int j = 0; j < encoders.Length; j++)
            {
                if (encoders[j].MimeType == mimeType)
                    return encoders[j];
            }
            throw new Exception(mimeType + " mime type not found in ImageCodecInfo");
        }
        //自定义方法JoinTiffImages()将Word保存为TIFF图片格式(使用指定编码器和图像编码参数)
        public static void JoinTiffImages(Image[] images, string outFile, EncoderValue compressEncoder)
        {            
            System.Drawing.Imaging.Encoder enc = System.Drawing.Imaging.Encoder.SaveFlag;
            EncoderParameters ep = new EncoderParameters(2);
            ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.MultiFrame);
            ep.Param[1] = new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, (long)compressEncoder);
            Image pages = images[0];
            int frame = 0;
            ImageCodecInfo info = GetEncoderInfo("image/tiff");
            foreach (Image img in images)
            {
                if (frame == 0)
                {
                    pages = img;                   
                    pages.Save(outFile, info, ep);
                }

                else
                {
                    ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.FrameDimensionPage);

                    pages.SaveAdd(img, ep);
                }
                if (frame == images.Length - 1)
                {                    
                    ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.Flush);
                    pages.SaveAdd(ep);
                }
                frame++;
            }
        }
    }
}

以上是本次关于Word转成其他格式文件的具体描述,方法中的代码供参考。欢迎转载(转载请注明出处)

 

目录
相关文章
|
26天前
|
监控 安全 C#
使用C#如何监控选定文件夹中文件的变动情况?
使用C#如何监控选定文件夹中文件的变动情况?
85 19
|
21天前
|
编译器 C# Windows
C#基础:手动编译一个.cs源代码文件并生成.exe可执行文件
通过上述步骤,应该能够高效准确地编译C#源代码并生成相应的可执行文件。此外,这一过程强调了对命令行编译器的理解,这在调试和自动化编译流程中是非常重要的。
34 2
|
26天前
|
文字识别 C# Python
使用C#将几个Excel文件合并去重分类
使用C#将几个Excel文件合并去重分类
20 3
|
26天前
|
数据库
C#Winform使用NPOI获取word中的数据
C#Winform使用NPOI获取word中的数据
97 2
|
17天前
|
C# 图形学 数据安全/隐私保护
Unity数据加密☀️ 二、使用Rider将C#代码生成DLL文件
Unity数据加密☀️ 二、使用Rider将C#代码生成DLL文件
|
18天前
|
C#
C# 写日志文件
C# 写日志文件
26 0
|
3月前
|
C#
【C#】C#读写Excel文件
【C#】C#读写Excel文件
43 1
|
3月前
|
JavaScript 前端开发 C#
初识Unity——创建代码、场景以及五个常用面板(创建C#代码、打开代码文件、场景的创建、Project、Hierarchy、Inspector、Scene、Game )
初识Unity——创建代码、场景以及五个常用面板(创建C#代码、打开代码文件、场景的创建、Project、Hierarchy、Inspector、Scene、Game )
49 0
|
4月前
|
开发框架 前端开发 .NET
C#编程与Web开发
【4月更文挑战第21天】本文探讨了C#在Web开发中的应用,包括使用ASP.NET框架、MVC模式、Web API和Entity Framework。C#作为.NET框架的主要语言,结合这些工具,能创建动态、高效的Web应用。实际案例涉及企业级应用、电子商务和社交媒体平台。尽管面临竞争和挑战,但C#在Web开发领域的前景将持续拓展。
155 3
|
4月前
|
SQL 开发框架 安全
C#编程与多线程处理
【4月更文挑战第21天】探索C#多线程处理,提升程序性能与响应性。了解C#中的Thread、Task类及Async/Await关键字,掌握线程同步与安全,实践并发计算、网络服务及UI优化。跟随未来发展趋势,利用C#打造高效应用。
171 3