发布网友
共2个回答
热心网友
你好,代码如下,试试吧
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Microsoft.Win32;
using System.Diagnostics;
using Word;
namespace 打印到文件
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
ConvertWord2Pdf(@"E:\\GIS中英文简历.doc");
}
object oMissing = System.Reflection.Missing.Value;
private string adobePdfPrint = "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)
{
MessageBox.Show(printername);
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.");
Word._Application word = new Word.Application();
object fileName = sourceFile;
// 打开Word文档
Word.Document doc = word.Documents.Open(ref fileName,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
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
{
System.Windows.Forms.Application.DoEvents();
}
}
doc.Application.ActivePrinter = oldPrint;
object SaveChanges = false;
word.Documents.Close(ref SaveChanges, ref oMissing, ref oMissing);
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
{
System.Windows.Forms.Application.DoEvents();
}
}
objProcess.CloseMainWindow();
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
热心网友
word有函数 ExportAsFixedFormat
MSDN上说明
The ExportAsFixedFormat method is used to publish a workbook to either the PDF or XPS format.
选一个word文件转为PDF
public static string WordConvertTOPDF(string path, string saveFielPath)
{
string result = "false";
object file = path;// _fileFullPath;
object readOnly = true;
object isVisible = true;
object confirmConverisons = false;
object openAndRepair = false;
Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
try
{
doc = wordApp.Documents.Open(ref file, ref confirmConverisons, ref readOnly,
ref _missing, ref _missing, ref _missing, ref _missing, ref _missing, ref _missing,
ref _missing, ref _missing, ref isVisible, ref openAndRepair, ref _missing, ref _missing, ref _missing);
}
catch (Exception ex)
{
result = ex.ToString();
}
if (doc.ActiveWindow.ActivePane.View.Type != WdViewType.wdPrintView)
{
if (doc.ActiveWindow.View.SplitSpecial == WdSpecialPane.wdPaneNone)
doc.ActiveWindow.ActivePane.View.Type = WdViewType.wdPrintView;
else
doc.ActiveWindow.View.Type = WdViewType.wdPrintView;
}
int FilePageCount = GetPageCount(doc);//得到总页数
try
{
if (!System.IO.File.Exists(saveFielPath))
doc.ExportAsFixedFormat(saveFielPath, WdExportFormat.wdExportFormatPDF, false, WdExportOptimizeFor.wdExportOptimizeForPrint, WdExportRange.wdExportFromTo,
1, FilePageCount, WdExportItem.wdExportDocumentContent, false, true, WdExportCreateBookmarks.wdExportCreateNoBookmarks, true, true, false, _missing);
result = string.Empty;
}
catch (Exception ex)
{
result = ex.ToString();
}
finally
{
if (doc != null)
{
doc.Close(ref _missing, ref _missing, ref _missing);
doc = null;
}
if (wordApp != null)
{
wordApp.Quit(ref _missing, ref _missing, ref _missing);
wordApp = null;
}
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
return result;
}
若是代码生成word之后转
doc.ExportAsFixedFormat(saveFielPath, WdExportFormat.wdExportFormatPDF,
false, WdExportOptimizeFor.wdExportOptimizeForPrint,
WdExportRange.wdExportFromTo,
1,
FilePageCount, WdExportItem.wdExportDocumentContent, false, true,
WdExportCreateBookmarks.wdExportCreateNoBookmarks, true, true, false,
_missing);