C# Word转PDF/HTML/XML/XPS/SVG/EMF/EPUB/TIFF
一款有着强大的文档转换功能的工具,无论何时何地都会是现代办公环境极为需要的。在本篇文章中,将介绍关于Word文档的转换功能(Word转XPS/SVG/EMF/EPUB/TIFF)。希望方法中的代码能为各位开发者们提供一定的参考价值。
使用工具:Free Spire.Doc for .NET(社区版)
使用方法:下载安装该控件后,在VS控制台应用程序中添加引用Spire.Doc.dll文件(dll文件可在该安装文件夹下Bin中获取)
1.Word转PDF/HTML/XML
using Spire.Doc;
namespace Doc2PDF
{
class Program
{
static void Main(string[] args)
{
//创建一个Document类对象,并加载Word文档
Document document = new Document();
document.LoadFromFile(@"C:\Users\Administrator\Desktop\Test.docx");
//调用方法SaveToFile()将Word转为PDF、HTML和XML
document.SaveToFile("Test.PDF", FileFormat.PDF);
document.SaveToFile("Test.html", FileFormat.Html);
document.SaveToFile("Test.xml", FileFormat.Xml);
//运行生成的文档
System.Diagnostics.Process.Start("Test.PDF");
System.Diagnostics.Process.Start("Test.html");
System.Diagnostics.Process.Start("Test.xml");
}
}
}
复制代码
2.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");
}
}
}
复制代码
调试运行该项目生成文档,如下图:
3.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);
}
}
}
复制代码
4. 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(0, Spire.Doc.Documents.ImageType.Metafile);
image.Save("WordtoEmf.emf", ImageFormat.Emf);
}
}
}
复制代码
5. 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");
}
}
}
复制代码
6. 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);
}
}
}
复制代码
7. 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 = 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++;
}
}
}
}
复制代码(编辑:雷林鹏 来源:网络)
C# Word转PDF/HTML/XML/XPS/SVG/EMF/EPUB/TIFF的更多相关文章
- C# Word转为多种格式文件(Word转XPS/SVG/EMF/EPUB/TIFF)
一款有着强大的文档转换功能的工具,无论何时何地都会是现代办公环境极为需要的.在本篇文章中,将继续介绍关于Word文档的转换功能(Word转XPS/SVG/EMF/EPUB/TIFF)希望方法中的代码能 ...
- Java 将Word转为PDF、PNG、SVG、RTF、XPS、TXT、XML
同一文档在不同的编译或阅读环境中,需要使用特定的文档格式来打开,通常需要通过转换文档格式的方式来实现.下面将介绍在Java程序中如何来转换Word文档为其他几种常见文档格式,如PDF.图片png.sv ...
- C# 将Word转为PDF、XPS、Epub、RTF(基于Spire.Cloud.Word.SDK)
本文介绍通过调用Spire.Cloud.Word.SDK提供的ConvertApi接口将Word转换为PDF.XPS.Epub.RTF以及将Docx转为Doc格式等.调用接口方法及步骤参考以下步骤: ...
- PCB 批量Word转PDF实现方法
自上次公司电脑中毒带来的影响,导致系统自动生成的Word档PCB出货报告,通过公司邮件服务器以附件的方式发送给客户后,客户是无法打开或打开缓慢的现象,如果将Word档转为PDF后在客户端是可以正常打开 ...
- C#,VB.NET如何将Word转换为PDF和Text
众所周知,Word是我们日常工作中常用的办公软件之一,有时出于某种需求我们需要将Word文档转换为PDF以及Text.那么如何以C#,VB.NET编程的方式来实现这一功能呢? 下面我将分开介绍如何运用 ...
- Jacob工具类使用文件互转服务 word转html html转excel word转pdf excel转pdf ppt转pdf
前提条件 必须安装MS office 1.jdk使用jdk1.8 2.jacob.dll放在..\jdk1.8\jre\bin目录下 3.eclipse的jre版本要和jdk一致,window-&g ...
- 利用aspose-words 实现 java中word转pdf文件
利用aspose-words 实现 java中word转pdf文件 首先下载aspose-words-15.8.0-jdk16.jar包 引入jar包,编写Java代码 package test; ...
- ASP.NET Word转为PDF
1.首先安装 Microsoft Office 2007加载项:Microsoft Save as PDF-简体中文版:下载地址: http://download.microsoft.com/down ...
- Word转pdf,再转图片插入PDF
WORD转PDF所需jar包: https://yangtaotao.lanzous.com/ice1jlc PDF转图片所需jar包: https://yangtaotao.lanzous.com/ ...
随机推荐
- Linux系统下 Rsync 环境安装搭建
一.Rsync简介 1.认识 Rsync(remote synchronize)是一个远程数据同步工具,可通过LAN/WAN快速同步多台主机间的文件.Rsync使用所谓的“Rsync算法”来使本地和远 ...
- VC编译选项 多线程(/MT)
VC编译选项 多线程(/MT)多线程调试(/MTd)多线程 DLL (/MD)多线程调试 DLL (/MDd)C 运行时库 库文件Single threa ...
- HDU 5652 India and China Origins(并查集)
India and China Origins Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/ ...
- Java面试题全集(上)
2013年年底的时候,我看到了网上流传的一个叫做<Java面试题大全>的东西,认真的阅读了以后发现里面的很多题目是重复且没有价值的题目,还有不少的参考答案也是错误的,于是我花了半个月时间对 ...
- Nginx + Tomcat 负载均衡配置详解
Nginx常用操作指南一.Nginx 与 Tomcat 安装.配置及优化1. 检查和安装依赖项 yum -y install gcc pcre pcre-devel zlib zlib-devel o ...
- AWR之-enq TX - row lock contention的性能故障-转
1 对这一个小时进行AWR的收集和分析,首先,从报告头中看到DB Time达到近500分钟,(DB Time)/Elapsed=8,这个比值偏高: Snap Id Snap Time Sessio ...
- web应用/http协议/web框架
一.web应用 Web应用程序是一种可以通过Web访问的应用程序,程序的最大好处是用户很容易访问应用程序,用户只需要有浏览器即可,不需要再安装其他软件.应用程序有两种模式C/S.B/S.C/S是客户端 ...
- Celery最佳实践(转)
原文:http://my.oschina.net/siddontang/blog/284107 英文原文:https://denibertovic.com/posts/celery-best-prac ...
- 【我的Android进阶之旅】解决Android Studio 运行gradle命令时报错: 错误: 编码GBK的不可映射字符
1.问题描述 最近在负责公司基础业务和移动基础设施的开发工作,正在负责Lint代码静态检查工作.因此编写了自定义的Lint规则,在调试过程中,编译的时候出现了如下所示的错误: 部分输出日志如下所示: ...
- thinkphp5手动注册命名空间
手动注册 命名空间.利用application/config.php配置文件来注册命名空间1:在application目录同级创建一个myExtend文件夹,里面再创建一个myTest文件夹.里面放一 ...