使用C#将word文档转换为可供传真的tif文件
2008-04-07 10:54
561 查看
预备工作:完整安装office 2003,然后添加COM组件引用,Microsoft WORD 11.0
在打印机和传真机选项中将Microsoft Office Document Image Writer设为默认打印机
需添加的命名空间:using Microsoft.Office.Interop.Word;
using System.IO;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class PrintDocToTif
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string tifPath = null;
//设置生成的tif文件路径
//路径名的命名规则为无后缀名,否则调用 varDoc.PrintOut方法时会打开打印预览窗体
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public PrintDocToTif(string buildTifPath)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.tifPath = buildTifPath;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//// <summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 生成无后缀名的tif文件
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// </summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// <param name="sourceFile">源WORD文件</param>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
/// <param name="destiFile"></param>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public void PrintTo(object sourceFile)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
try
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//生成的tif路径
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
object OutputFileName = (object)tifPath;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
object varMissing = Type.Missing;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//定义Word实例对象
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Microsoft.Office.Interop.Word.Application varWord = new Microsoft.Office.Interop.Word.Application();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//设置Word的虚拟打印机
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
varWord.ActivePrinter = "Microsoft Office Document Image Writer";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//定义Word文档实例对象
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Microsoft.Office.Interop.Word.Document varDoc = varWord.Documents.Open
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
(ref sourceFile, ref varMissing, ref varMissing, ref varMissing, ref varMissing,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
varDoc.Activate(); //激活文档
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
object PrintToFile = true;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//打印成tif文件
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
varDoc.PrintOut(ref varMissing, ref varMissing, ref varMissing, ref OutputFileName, ref varMissing,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref PrintToFile,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
object saveChange = WdSaveOptions.wdSaveChanges; //关闭word文档时不打开新窗体
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
varDoc.Close(ref saveChange, ref varMissing, ref varMissing); //关闭word文档
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
varWord.Quit(ref varMissing, ref varMissing, ref varMissing); //退出Word
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
catch (Exception ex)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
throw new Exception(ex.Message);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//// <summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 将无后缀名的生成文件重命名为tif文件
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// </summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
/// <param name="filePath">生成的无后缀名的文件路径</param>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public void ReNameToTif()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string newTifPath = "";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (File.Exists(tifPath))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
newTifPath = tifPath + ".tif";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
File.Move(tifPath, newTifPath);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
联系方式:yiai027@163.com
QQ:25717594
在打印机和传真机选项中将Microsoft Office Document Image Writer设为默认打印机
需添加的命名空间:using Microsoft.Office.Interop.Word;
using System.IO;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class PrintDocToTif
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string tifPath = null;
//设置生成的tif文件路径
//路径名的命名规则为无后缀名,否则调用 varDoc.PrintOut方法时会打开打印预览窗体
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public PrintDocToTif(string buildTifPath)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.tifPath = buildTifPath;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//// <summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 生成无后缀名的tif文件
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// </summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// <param name="sourceFile">源WORD文件</param>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
/// <param name="destiFile"></param>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public void PrintTo(object sourceFile)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
try
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//生成的tif路径
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
object OutputFileName = (object)tifPath;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
object varMissing = Type.Missing;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//定义Word实例对象
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Microsoft.Office.Interop.Word.Application varWord = new Microsoft.Office.Interop.Word.Application();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//设置Word的虚拟打印机
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
varWord.ActivePrinter = "Microsoft Office Document Image Writer";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//定义Word文档实例对象
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Microsoft.Office.Interop.Word.Document varDoc = varWord.Documents.Open
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
(ref sourceFile, ref varMissing, ref varMissing, ref varMissing, ref varMissing,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
varDoc.Activate(); //激活文档
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
object PrintToFile = true;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//打印成tif文件
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
varDoc.PrintOut(ref varMissing, ref varMissing, ref varMissing, ref OutputFileName, ref varMissing,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref PrintToFile,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
object saveChange = WdSaveOptions.wdSaveChanges; //关闭word文档时不打开新窗体
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
varDoc.Close(ref saveChange, ref varMissing, ref varMissing); //关闭word文档
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
varWord.Quit(ref varMissing, ref varMissing, ref varMissing); //退出Word
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
catch (Exception ex)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
throw new Exception(ex.Message);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//// <summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 将无后缀名的生成文件重命名为tif文件
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// </summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
/// <param name="filePath">生成的无后缀名的文件路径</param>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public void ReNameToTif()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string newTifPath = "";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (File.Exists(tifPath))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
newTifPath = tifPath + ".tif";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
File.Move(tifPath, newTifPath);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
联系方式:yiai027@163.com
QQ:25717594
相关文章推荐
- PHP使用COM组件转换word文档为HTML并实现在线预览
- C#中使用XSLT文件将XML文档转换为HTML
- C# 将Word文档转换为HTML
- 用C#实现生成PDF文档和将WORD转换为PDF (转自海东的技术资料)
- 使用C#替换Word文档里的文字和图片
- Java 使用jacob实现各类办公文档(ppt,Excel,word,text,imge)转换成PDF
- C# 中使用Word文档对图像进行操作
- 如何使用C#在Word文档中插入图片以及读取Word文档中的图片?
- 将WORD文档转换成为HTML网页文件的C#代码
- 用C#实现生成PDF文档和将WORD转换为PDF
- C# 将word文档转换为pdf
- C#中使用FlashPrinter将WORD文档转成PDF或者SWF
- 使用MSOffice .NET API 将文档(Word \ Excel \ PowerPoint \ Visio \ text \ XML \ RTF \ CSV等)转换为PDF
- c# word文档与二进制数据的相互转换
- 用C#实现生成PDF文档和将WORD转换为PDF (转自海东的技术资料)
- 将网页内容转换成word或excel文档的方法(c#.net)
- [翻译]使用c#读取word文档
- C#多文档 含父窗体 Word文档读取文本 转换txt
- 使用maven管理jacob.jar对excel、word文档进行转换为html细节描述
- c#.net 将网页内容转换成word或excel文档