C# Word文档转PDF的实现
2010-03-08 15:19
579 查看
private string adobePdfPrint = "Adobe PDF";
private string adobeDisPrint = "Acrobat Distiller";
private string regRoot = "SOFTWARE//Adobe//Acrobat Distiller//";
private string printerFileName = "acrodist.exe";
private string regName = "InstallPath";
/// <summary>
/// 获取acrodist.exe的安装路径
/// </summary>
/// <returns></returns>
private string GetAdobeDisFilePath()
{
RegistryKey regKey = null;
RegistryKey acrodistKey = null;
string printerName = string.Empty;
int i;
string regRootVersion = string.Empty;
regKey = Registry.LocalMachine;
// acrodist的4-8版本适用
for (i = 4; i < 9; i++)
{
regRootVersion = string.Format("{0}{1}.0", regRoot, i);
acrodistKey = regKey.OpenSubKey(regRootVersion);
if (acrodistKey != null)
{
printerName = acrodistKey.GetValue(regName, "") + "//" + printerFileName;
if (File.Exists(printerName))
{
return printerName;
}
}
}
throw new Exception("Acrobat Distiller printer not found!");
}
/// <summary>
/// 获取Adobe Printer
/// "Adobe PDF" 或 "Acrobat Distiller"
/// </summary>
/// <returns></returns>
private string GetAdobePrinter()
{
foreach (string printername in PrinterSettings.InstalledPrinters)
{
if (printername.ToUpper().IndexOf(adobePdfPrint.ToUpper()) != -1 || printername.ToUpper().IndexOf(adobeDisPrint.ToUpper()) != -1)
{
return printername;
}
}
return string.Empty;
}
public void ConvertWord2Pdf(string sourceFile)
{
if (PrinterSettings.InstalledPrinters.Count == 0)
throw new Exception("Did not find the printer, please install the printer.");
if (!File.Exists(sourceFile))
throw new Exception(string.Format("File not found:{0}", sourceFile));
string strDir = Path.GetDirectoryName(sourceFile);
string strName = Path.GetFileNameWithoutExtension(sourceFile);
string prnFile = string.Format("{0}//{1}.prn",strDir,strName);
string pdfFile = string.Format("{0}//{1}.pdf",strDir,strName);
object objPrnFile = (object)prnFile;
object background = true;
object printToFile = true;
object collate = true;
object append = false;
object manualDuplexPrint = false;
object copies = false;
object range = Word.WdPrintOutRange.wdPrintAllDocument;
object missing = System.Reflection.Missing.Value;
string msg = string.Empty;
string adobePrinter = GetAdobePrinter();
if (adobePrinter.Length == 0)
throw new Exception("Did not find Adobe PDF printer or Acrobat Distiller printer, please install the printer.");
IWord word = new Word2003Factory().CreateWord();
// 打开Word文档
Word.Document
b5b0
doc = word.OpenDocument(sourceFile);
string oldPrint = doc.Application.ActivePrinter;
// 将Adobe Printer设置为默认打印机
doc.Application.ActivePrinter = adobePrinter;
DateTime start = DateTime.Now;
DateTime end = start.AddSeconds(20);
// Word文档打印到Prn文件
doc.PrintOut(ref background, ref append, ref range, ref objPrnFile, ref missing, ref missing,
ref missing, ref missing,ref missing, ref printToFile, ref collate, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing);
while (!File.Exists(prnFile))
{
if (DateTime.Now > end)
{
throw new Exception("Word document print to prn document overtime");
}
else
{
Application.DoEvents();
}
}
doc.Application.ActivePrinter = oldPrint;
word.Close(false);
doc = null;
word = null;
// Prn装PDF
Process objProcess = new Process();
objProcess.StartInfo.CreateNoWindow = true;
objProcess.StartInfo.UseShellExecute = false;
objProcess.StartInfo.FileName = GetAdobeDisFilePath();
objProcess.StartInfo.Arguments = prnFile;
start = DateTime.Now;
end = start.AddSeconds(20);
objProcess.Start();
while (!File.Exists(pdfFile))
{
if (DateTime.Now > end)
{
throw new Exception("Word document print to prn document overtime");
}
else
{
Application.DoEvents();
}
}
objProcess.CloseMainWindow();
//if (File.Exists(prnFile))
//{
// File.Delete(prnFile);
//}
/*
ACRODISTXLib.PdfDistiller pdfDis = new ACRODISTXLib.PdfDistiller();
pdfDis.FileToPDF(prnFile, pdfFile, string.Empty);
start = DateTime.Now;
end = start.AddSeconds(20);
while(!File.Exists(pdfFile))
{
if (DateTime.Now > end)
{
throw new Exception("prn document print to pdf document overtime");
}
else
{
Application.DoEvents();
}
}
pdfDis = null;
*/
}
private string adobeDisPrint = "Acrobat Distiller";
private string regRoot = "SOFTWARE//Adobe//Acrobat Distiller//";
private string printerFileName = "acrodist.exe";
private string regName = "InstallPath";
/// <summary>
/// 获取acrodist.exe的安装路径
/// </summary>
/// <returns></returns>
private string GetAdobeDisFilePath()
{
RegistryKey regKey = null;
RegistryKey acrodistKey = null;
string printerName = string.Empty;
int i;
string regRootVersion = string.Empty;
regKey = Registry.LocalMachine;
// acrodist的4-8版本适用
for (i = 4; i < 9; i++)
{
regRootVersion = string.Format("{0}{1}.0", regRoot, i);
acrodistKey = regKey.OpenSubKey(regRootVersion);
if (acrodistKey != null)
{
printerName = acrodistKey.GetValue(regName, "") + "//" + printerFileName;
if (File.Exists(printerName))
{
return printerName;
}
}
}
throw new Exception("Acrobat Distiller printer not found!");
}
/// <summary>
/// 获取Adobe Printer
/// "Adobe PDF" 或 "Acrobat Distiller"
/// </summary>
/// <returns></returns>
private string GetAdobePrinter()
{
foreach (string printername in PrinterSettings.InstalledPrinters)
{
if (printername.ToUpper().IndexOf(adobePdfPrint.ToUpper()) != -1 || printername.ToUpper().IndexOf(adobeDisPrint.ToUpper()) != -1)
{
return printername;
}
}
return string.Empty;
}
public void ConvertWord2Pdf(string sourceFile)
{
if (PrinterSettings.InstalledPrinters.Count == 0)
throw new Exception("Did not find the printer, please install the printer.");
if (!File.Exists(sourceFile))
throw new Exception(string.Format("File not found:{0}", sourceFile));
string strDir = Path.GetDirectoryName(sourceFile);
string strName = Path.GetFileNameWithoutExtension(sourceFile);
string prnFile = string.Format("{0}//{1}.prn",strDir,strName);
string pdfFile = string.Format("{0}//{1}.pdf",strDir,strName);
object objPrnFile = (object)prnFile;
object background = true;
object printToFile = true;
object collate = true;
object append = false;
object manualDuplexPrint = false;
object copies = false;
object range = Word.WdPrintOutRange.wdPrintAllDocument;
object missing = System.Reflection.Missing.Value;
string msg = string.Empty;
string adobePrinter = GetAdobePrinter();
if (adobePrinter.Length == 0)
throw new Exception("Did not find Adobe PDF printer or Acrobat Distiller printer, please install the printer.");
IWord word = new Word2003Factory().CreateWord();
// 打开Word文档
Word.Document
b5b0
doc = word.OpenDocument(sourceFile);
string oldPrint = doc.Application.ActivePrinter;
// 将Adobe Printer设置为默认打印机
doc.Application.ActivePrinter = adobePrinter;
DateTime start = DateTime.Now;
DateTime end = start.AddSeconds(20);
// Word文档打印到Prn文件
doc.PrintOut(ref background, ref append, ref range, ref objPrnFile, ref missing, ref missing,
ref missing, ref missing,ref missing, ref printToFile, ref collate, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing);
while (!File.Exists(prnFile))
{
if (DateTime.Now > end)
{
throw new Exception("Word document print to prn document overtime");
}
else
{
Application.DoEvents();
}
}
doc.Application.ActivePrinter = oldPrint;
word.Close(false);
doc = null;
word = null;
// Prn装PDF
Process objProcess = new Process();
objProcess.StartInfo.CreateNoWindow = true;
objProcess.StartInfo.UseShellExecute = false;
objProcess.StartInfo.FileName = GetAdobeDisFilePath();
objProcess.StartInfo.Arguments = prnFile;
start = DateTime.Now;
end = start.AddSeconds(20);
objProcess.Start();
while (!File.Exists(pdfFile))
{
if (DateTime.Now > end)
{
throw new Exception("Word document print to prn document overtime");
}
else
{
Application.DoEvents();
}
}
objProcess.CloseMainWindow();
//if (File.Exists(prnFile))
//{
// File.Delete(prnFile);
//}
/*
ACRODISTXLib.PdfDistiller pdfDis = new ACRODISTXLib.PdfDistiller();
pdfDis.FileToPDF(prnFile, pdfFile, string.Empty);
start = DateTime.Now;
end = start.AddSeconds(20);
while(!File.Exists(pdfFile))
{
if (DateTime.Now > end)
{
throw new Exception("prn document print to pdf document overtime");
}
else
{
Application.DoEvents();
}
}
pdfDis = null;
*/
}
相关文章推荐
- 用C#实现生成PDF文档和将WORD转换为PDF
- C#实现MS-Office文档转Pdf(Word、Execel、PowerPoint、Visio、Project)
- 用C#.NET实现生成PDF文档和将WORD转换为PDF
- C#实现MS-Office文档转Pdf(Word、Execel、PowerPoint、Visio、Project)
- 用C#实现生成PDF文档和将WORD转换为PDF (转自海东的技术资料)
- 用C#实现生成PDF文档和将WORD转换为PDF
- 用C#实现生成PDF文档和将WORD转换为PDF (转自海东的技术资料)
- C# Word文档转PDF的实现
- C#实现上传WORD文档并另存为WEB页面
- 转:C#实现office文档转换为PDF或xps的一些方法
- 转:C#实现office文档转换为PDF或xps的一些方法
- C#实现Office文档转Pdf的方法
- (fujie724) C#:简单实现动态数据生成Word文档并保存
- 能够按页号提取word文档文本内容的小程序,由C#实现
- C#采用OpenXml实现给word文档添加文字
- 用C#实现生成PDF文档(原码)
- 用C#实现在Word文档中搜索文本
- C#:简单实现动态数据生成Word文档并保存
- C# 在PDF文档中创建表格的实现方法
- C#(文字类型转换)【下拉菜单->C#字体CodeBehind】附:【用C#实现生成PDF文档】