`

C# Word文档转换功能汇总(一)Word 转 XPS/Svg/Emf/Epub/Tiff )

阅读更多

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

使用方法下载安装该控件后,在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++;
            }
        }
    }
}

 



 

  • 大小: 143.6 KB
  • 大小: 150.2 KB
  • 大小: 145.5 KB
  • 大小: 144.9 KB
  • 大小: 154.7 KB
  • 大小: 85.1 KB
  • 大小: 120.9 KB
分享到:
评论

相关推荐

    .net c#源码实例SVG转换png/jpeg/jpg/pdf(svg.dll itextsharp.dll)

    在C#中,可以创建一个PdfDocument对象,然后利用iTextSharp的SVG支持,将SVG图像转换为PDF页面。这涉及到将SVG解析为PDF的图形指令,然后添加到PDF文档中。 4. 引用动态链接库(dll): 在C#项目中,可以直接引用...

    SVG转EMF的示例代码

    这个示例代码对于理解SVG和EMF之间的转换原理非常有帮助,同时也可以作为开发中实际转换功能的基础。如果你在Windows环境中需要处理SVG图形,并希望在其他应用程序(如Word、PowerPoint)中保持矢量特性,这个转换...

    C#Svg转换为png/jpeg等图片(C#版)

    在C#编程环境中,处理SVG与PNG或JPEG之间的转换是一项常见的需求。例如,在Web应用中,用户可能需要将SVG图标或图形下载为PNG或JPEG格式以便于打印或在不支持SVG的浏览器中显示。以下是一个详细的步骤,介绍了如何在...

    c/c++ svg转png, svg文件渲染例子

    接着,我们创建一个`PlutoRenderer`实例,传入解析后的SVG文档,以及目标PNG文件的宽度、高度。最后,调用`render()`方法将SVG渲染到PNG文件。 需要注意的是,实际使用时需要确保PlutoSVG库已经正确安装并在编译...

    http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd

    This is SVG, a language for describing two-dimensional graphics in XML. The Scalable Vector Graphics (SVG) Copyright 2001, 2002, 2011 World Wide Web Consortium (Massachusetts Institute of ...

    JAVA WMF 转换SVG,PNG

    Batik是一个开源的Java库,用于处理SVG图像,其中包括了SVG到其他图像格式的转换功能。在提供的文件列表中,batik-all-1.8pre-r1084380.jar包含了Batik的所有组件,包括SVG渲染引擎。通过使用Batik的Transcoder API...

    c# SVG转Image

    C#中可以使用第三方库来实现这一功能,如SVG.NET或者在本项目中使用的Svg.dll。以下是一些关键知识点: 1. **SVG解析**:SVG文件是一种XML文档,因此可以使用XML解析器来读取和理解其结构。SVG文件包含形状、路径、...

    emf2svg-FreeHEP

    This is the API specification of the FreeHEP VectorGraphics package for converting emf to svg.

    .net 将SVG标签代码转成图片格式

    总结来说,使用.NET和SVG.dll库在VS2012中进行SVG到PNG的转换是一个简单的过程,主要包括导入SVG库、创建SVG文档实例、设置图像尺寸、渲染SVG到Bitmap,最后保存Bitmap为PNG文件。这个功能对于.NET开发者在处理SVG...

    LaTeX公式转矢量图,支持SVG、PDF、EPS、EMF四种格式(Python)

    本文将深入探讨如何使用Python将LaTeX公式转换为SVG、PDF、EPS和EMF四种矢量图像格式。 首先,让我们理解矢量图的优势。与位图图像不同,矢量图不依赖像素,而是由线条、形状和路径组成,这意味着它们可以无限缩放...

    asposejar 支持常见文档转换 word转pdf excel转pdf ppt转pdf pdf转word pdf转exce

    - **PDF转SVG**:SVG是一种矢量图形格式,Aspose.PDF可以将PDF转换为SVG,保持图形的清晰度,适用于高质量的打印和Web设计。 3. **标签“范文/模板/素材”**: 这个标签可能意味着Aspose.jar在创建或编辑模板文档...

    svg转css,css转svg,svg与css互相转换并压缩

    例如,`svg2css`项目(对应压缩包中的`svg2css-master`)可能就是一个这样的工具,它可以帮助开发者实现SVG和CSS之间的转换。这个工具可能包含解析SVG文件,将其转换为CSS,同时也可以逆向操作,将CSS背景图像解码为...

    Android代码-Android SVG library

    Sharp is a Scalable Vector Graphics (SVG) implementation for Android. It facilitates loading vector graphics as SharpDrawables, and can effectively be used wherever a conventional image would be ...

    doc/docx转html,以及wmf与emf转jpg,png图片

    Batik是一个Java SVG工具包,虽然主要处理SVG,但它也提供了将其他矢量图形格式转换为像素图像的功能。 1. 解析图形: Batik提供了`VectorGraphics2D`类来解析WMF或EMF文件。 2. 转换:通过`Rasterizer`或者`Image...

    java将word转换为html,包含JAVA实现代码与需要jar文件

    在Java编程环境中,将Microsoft Word文档转换为HTML格式是一项常见的需求,这主要涉及到文本和图像的处理。在本文中,我们将深入探讨如何使用Java来实现这个功能,包括所需的jar文件和处理Word中的矢量图(WMF和EMF...

    C#下处理生成svg矢量图的类库,功能超全

    C#下处理生成svg矢量图的类库,功能超全。SharpVectorGraphics.0.4.alpha版本。

    JavaPDF操作类库API_Free Spire.PDF for Java_5.1.0

    1.1 文档转换:PDF转图片/Word/SVG/XPS/HTML/XPS/TIFF、图片转PDF 1.2 文档操作:文档创建、合并、拆分、压缩、复制;页面背景、页边距、纸张大小及方向、页面旋转、合并、缩放;表单域;页眉页脚;水印;文本列表...

    dxf转svg,svg转png

    标题提到的“dxf转svg,svg转png”涉及两种不同的转换过程,都是图形文件格式之间的转换。DXF(Drawing Exchange Format)是AutoCAD用于二维绘图的数据交换格式,SVG(Scalable Vector Graphics)是一种基于XML的矢量...

    spire.doc-fe_7.1(免费).zip

    doc for . net是一个专业的Word .... net平台创建、读取、写入、转换和打印Word文档文件,具有...此外,它还支持使用c#将Word Doc/Docx转换为PDF, Word转换为SVG, Word转换为PostScript, Word转换为PCL(打印机命令语言)。

    位图转矢量工具,位图转换矢量图工具

    "位图转矢量图工具"是一种专门用于将位图图像转换为矢量图的软件。这种工具的目的是解决位图放大后失真的问题,通过算法分析位图中的颜色和形状,将其转化为矢量图形的形式。描述中提到的这款工具被赞誉为“实测好用...

Global site tag (gtag.js) - Google Analytics