C# Word转为多种格式文件(Word转XPS/SVG/EMF/EPUB/TIFF)
一款有着强大的文档转换功能的工具,无论何时何地都会是现代办公环境极为需要的。在本篇文章中,将继续介绍关于Word文档的转换功能(Word转XPS/SVG/EMF/EPUB/TIFF)希望方法中的代码能为各位开发者们提供一定的参考价值。
PS:更多Word转换功能可以参阅这两篇文章
使用方法:下载安装该控件后,在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. Word转SVG
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. Word转Emf
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(, Spire.Doc.Documents.ImageType.Metafile);
image.Save("WordtoEmf.emf", ImageFormat.Emf);
}
}
}
4. Word转Epub
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. Word转Word 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. Word转Tiff
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 = ; 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();
ep.Param[] = new EncoderParameter(enc, (long)EncoderValue.MultiFrame);
ep.Param[] = new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, (long)compressEncoder);
Image pages = images[];
int frame = ;
ImageCodecInfo info = GetEncoderInfo("image/tiff");
foreach (Image img in images)
{
if (frame == )
{
pages = img;
pages.Save(outFile, info, ep);
} else
{
ep.Param[] = new EncoderParameter(enc, (long)EncoderValue.FrameDimensionPage); pages.SaveAdd(img, ep);
}
if (frame == images.Length - )
{
ep.Param[] = new EncoderParameter(enc, (long)EncoderValue.Flush);
pages.SaveAdd(ep);
}
frame++;
}
}
}
}
以上是本次关于Word转成其他格式文件的具体描述,方法中的代码供参考。欢迎转载(转载请注明出处)
C# Word转为多种格式文件(Word转XPS/SVG/EMF/EPUB/TIFF)的更多相关文章
- C# Word转PDF/HTML/XML/XPS/SVG/EMF/EPUB/TIFF
一款有着强大的文档转换功能的工具,无论何时何地都会是现代办公环境极为需要的.在本篇文章中,将介绍关于Word文档的转换功能(Word转XPS/SVG/EMF/EPUB/TIFF).希望方法中的代码能为 ...
- 在Java中导出word、excel格式文件时JSP页面头的设置
我们在JSP中往往会把一些表格里的东西需要导出到本地,一般都是导成word.excel格式的文件.这只需要在JSP页面头设置及在<head></head>标签中添加下面的代码: ...
- freemarker动态生成word并将生成的word转为PDF,openoffice转换word乱码
之前项目有个需求,需要先动态生成word内容,然后再预览生成word的内容(不能修改).整理一下,方便以后使用. 网上参考了好多大神的博客.具体也忘了参考谁的了,如有侵权,请告知修改. 思路一: 将目 ...
- java 将word转为PDF (100%与word软件转换一样)
jdk环境:jdk_8.0.1310.11_64 (64位) 1.引入pom文件 <!-- word转pdf(依赖windows本地的wps) --> <dependency& ...
- C# 合并多种格式文件为PDF
文档合并是一种高效文档处理方式.如果能够有一个方法能将多种不同类型的文档合并成一种文档格式,那么在文档存储管理上将为我们提供极大的便利.因此,本篇文章介绍了一种如何使用免费组件Free Spire.O ...
- Excel文件转为其他格式文件
引用:Spire.XLS 是一个 Excel 文件的读写库,无需安装office,使用起来也挺方便的.Spire还有一些其他的库(Spire.Doc,Spire.Pdf……) 说明:Spire.XLS ...
- Magicodes.IE在.NET Core中通过请求头导出多种格式文件
前言 在2.2里程碑中我们增加了一些新的功能,正如标题所写通过请求头进行导出我们不同格式的文件.下面我们来看一下如何使用.通过这种方式无论是对我们的数据多用途,还是说对我们的数据校验都做到了轻松易配. ...
- MATLAB中将mat文件转为txt格式文件
直接保存为txt文件: 可以用fprintf函数,来代替save函数 比如现在我有一个变量a=[0.1223 345.4544] 如果我想保存它的话,可以用下面的程序: fid = fopen(' ...
- Java 将Word转为PDF、PNG、SVG、RTF、XPS、TXT、XML
同一文档在不同的编译或阅读环境中,需要使用特定的文档格式来打开,通常需要通过转换文档格式的方式来实现.下面将介绍在Java程序中如何来转换Word文档为其他几种常见文档格式,如PDF.图片png.sv ...
随机推荐
- 03SpringMVC,Spring,Hibernate整合(Date时间转换)
项目结构 2 web.xml的配置内容如下: <?xmlversion="1.0"encoding="UTF-8"?> <web-app ...
- Python爬虫! 单爬,批量爬,这都不是事!
昨天做了一个煎蛋网妹子图的爬虫,个人感觉效果不错.但是每次都得重复的敲辣么多的代码(相比于Java或者其他语言的爬虫实现,Python的代码量可谓是相当的少了),就封装了一下!可以实现对批量网址以及单 ...
- 11 OptionsMenu 菜单
OptionsMenu 选项菜单(系统菜单 ) OptionsMenu:系统级别菜单 菜单的使用步骤: 1. res里的menu里添加布局 在布局里写菜单项 2. 在逻辑代码中使用OnCreateOp ...
- 1073. Scientific Notation (20)
题目如下: Scientific notation is the way that scientists easily handle very large numbers or very small ...
- Android:Field can be converted to a local varible.
背景 使用 Android Studio 开发 Android 有一段时间了,偶尔会碰到 AS 在一些私有变量上有黄色高亮提示Field can be converted to a local var ...
- Android初级教程IP拨号器初识广播接受者
需求:输入ip号码并且保存在本地,监听打电话广播,如果电话号码以0开头,则加上ip区号拨打. 首先定义一个页面布局: <LinearLayout xmlns:android="http ...
- Android读取网络图片到本地的简约的实现
今天在网上看到了一个关于读取网络文件的小视频,觉得不错,拿来与大家分享 思路 具体的思路比较的简单,但是思想非常的单纯.那就是输入一个网址,点击按钮,将从网络上获取的一张图片显示到一个ImageVie ...
- Tomcat如何检测内存泄漏
一般情况下,如果我们重启web应用是通过重启tomcat的话,则不存在内存泄漏问题.但如果不重启tomcat而对web应用进行重加载则可能会导致内存泄漏,因为重加载后有可能会导致原来的某些内存无法让G ...
- ffmpeg转码器移植VC的工程:ffmpeg for MFC
本文介绍一个自己做的FFMPEG移植到VC下的开源工程:ffmpeg for MFC.本工程将ffmpeg工程中的ffmpeg转码器(ffmpeg.c)移植到了VC环境下.并且使用MFC做了一套简单的 ...
- Dynamics CRM 2011/2013 section的隐藏
代码如下 Xrm.Page.ui.tabs.get("TabName").sections.get("SectionName").setVisi ...