新闻资讯 | 控件产品 | 技术文章 | 购物车 | 合作伙伴 | 问题解答 | 电子简报 | PDF论坛 | 资源下载
您现在的位置:技术文章>PDF开发篇 首页|PDF应用篇|PDF开发篇|解决方案

C# 上传文件时如何实现将doc、txt文件自动转换为pdf并保存

发布日期:2011-7-25 作者:蒋建华 出处:http://social.microsoft.com

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 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;
*/
}

原文地址:http://social.microsoft.com/Forums/zh-CN/visualcshartzhchs/thread/5ecc3de7-0eb1-4891-a86f-3e7550a50e3c

发布人:管理员 【报告错误】·【推荐好友】·【打印文章】
相关评论 本文现有 0 条评论 评分人数: 0 平均分: 0
相关评论
用户名: 密  码:
说明:输入正确的用户名和密码才能参与评论。如果您不是本站会员,你可以注册为本站会员。
注意:文章中的链接、内容等需要修改的错误,请用报告错误,以利文档及时修改。
注意:请不要在评论中含与内容无关的广告链接,违者封ID
文章搜索
推荐文章
循序渐进学习iTextShar
循序渐进学习iTextShar
循序渐进学习iTextShar
循序渐进学习iTextShar
循序渐进学习iTextShar
循序渐进学习iTextShar
循序渐进学习iTextShar
循序渐进学习iTextShar
循序渐进学习iTextShar
循序渐进学习iTextShar
热门点击
一个简单的PDF文件结构的分析(22751)
PHP的FDF文档支持(16275)
用C#制作PDF文件全攻略(13704)
Word转换PDF格式的C#或(12215)
ITextRenderer学习(11752)
pdfMaker文章:PDF加(11698)
PDF文件加密仿真(11114)
怎样用iReport制作Web(10485)
iText PDF概述(1)(10077)
从pdf文档中提取出文本(10017)
热门评论
从pdf文档中提取出文本(8)
PDFlib开发:创建超文本元(2)
客户端自动打印PDF(Prin(2)
浅谈PDFlib中文输出(一)(2)
xml的转换之pdf 2 ((1)
xml的转换之pdf 1 (x(1)
PDF知识讲座(1)(1)
PDFBox使用简介(1)
使用正则表达式计算PDF文档的(1)
word转pdf(1)