在C#窗口应用程序中将docx文件转换为pdf文件时出错 [英] Error occured while converting docx file to pdf file in C# window Application
本文介绍了在C#窗口应用程序中将docx文件转换为pdf文件时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
// Set filter for file extension and default file extension
dlg.DefaultExt = ".doc";
dlg.Filter = "Word documents (.doc)|*.docx;*.doc";
// Display OpenFileDialog by calling ShowDialog method
Nullable<bool> result =Convert.ToBoolean ( dlg.ShowDialog());
// Get the selected file name and display in a TextBox
if (result == true)
{
// Open document
string filename = dlg.FileName;
textBox1.Text = filename;
}
}
private void button2_Click(object sender, EventArgs e)
{
Microsoft.Office.Interop.Word.Application appWord = new Microsoft.Office.Interop.Word.Application();
wordDocument = appWord.Documents.Open(textBox1.Text);
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "PDF Documents|*.pdf";
Document document = new Document();
try
{
if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string ext = System.IO.Path.GetExtension(sfd.FileName);
switch (ext)
{
case ".pdf":
wordDocument.ExportAsFixedFormat(sfd.FileName, WdExportFormat.wdExportFormatPDF); // Error " Export failed because the feature is not installled "
break;
}
textBox2 .Text = sfd.FileName;
}
System.Windows.Forms.MessageBox.Show("File Converted Successfully..");
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
System.Diagnostics.Process.Start(sfd.FileName); //
//An unhandled exception of type 'System.ComponentModel.Win32Exception' occurred in System.dll
}
推荐答案
下载并安装此插件: http:// www.microsoft.com/en-us/download/details.aspx?id=9943 [ ^ ]
如需了解更多信息,请参阅: Document.ExportAsFixedFormat方法 [ ^ ]
Download and install this addin: http://www.microsoft.com/en-us/download/details.aspx?id=9943[^]
For further information, please see: Document.ExportAsFixedFormat Method[^]
这篇关于在C#窗口应用程序中将docx文件转换为pdf文件时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文