//RemoteConverter.cs的内容
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Word;namespace PDFConverter
{
public class RemoteConverter : MarshalByRefObject
{
object paramMissing = Type.Missing;
public string errormessage;
private bool wordavailable = false;
private bool checkedword = false; public bool WordIsAvailable()
{
//don't check every time.... first time only
if (!checkedword)
{ try
{
ApplicationClass wordApplication = new ApplicationClass();
wordApplication.Visible = true;
wordApplication.Quit(ref paramMissing, ref paramMissing,
ref paramMissing);
wordavailable = true;
}
catch
{
wordavailable = false;
}
checkedword = true;
}
return wordavailable; } public bool convert(string source, string output)
{
if (System.IO.File.Exists(source))
{
//start conversion
try
{
ApplicationClass wordApplication = new ApplicationClass(); Document wordDocument = null;
object paramSourceDocPath = source; string paramExportFilePath = output; //this part is copied from Microsoft MSDN //set exportformat to pdf
WdExportFormat paramExportFormat = WdExportFormat.wdExportFormatPDF;
bool paramOpenAfterExport = false;
WdExportOptimizeFor paramExportOptimizeFor =
WdExportOptimizeFor.wdExportOptimizeForPrint;
WdExportRange paramExportRange = WdExportRange.wdExportAllDocument;
int paramStartPage = 0;
int paramEndPage = 0;
WdExportItem paramExportItem = WdExportItem.wdExportDocumentContent;
bool paramIncludeDocProps = true;
bool paramKeepIRM = true;
WdExportCreateBooks paramCreateBooks =
WdExportCreateBooks.wdExportCreateWordBooks;
bool paramDocStructureTags = true;
bool paramBitmapMissingFonts = true;
bool paramUseISO19005_1 = false; try
{
// Open the source document.
wordDocument = wordApplication.Documents.Open(
ref paramSourceDocPath, ref paramMissing, ref paramMissing,
ref paramMissing, ref paramMissing, ref paramMissing,
ref paramMissing, ref paramMissing, ref paramMissing,
ref paramMissing, ref paramMissing, ref paramMissing,
ref paramMissing, ref paramMissing, ref paramMissing,
ref paramMissing); // Export it in the specified format.
if (wordDocument != null)
wordDocument.ExportAsFixedFormat(paramExportFilePath,
paramExportFormat, paramOpenAfterExport,
paramExportOptimizeFor, paramExportRange, paramStartPage,
paramEndPage, paramExportItem, paramIncludeDocProps,
paramKeepIRM, paramCreateBooks, paramDocStructureTags,
paramBitmapMissingFonts, paramUseISO19005_1,
ref paramMissing);
}
catch (Exception ex)
{
// Respond to the error
errormessage = ex.Message;
}
finally
{
// Close and release the Document object.
if (wordDocument != null)
{
wordDocument.Close(ref paramMissing, ref paramMissing,
ref paramMissing);
wordDocument = null;
} // Quit Word and release the ApplicationClass object.
if (wordApplication != null)
{
wordApplication.Quit(ref paramMissing, ref paramMissing,
ref paramMissing);
wordApplication = null;
} //i don't know why this is here two times. I just copied it from the MSDN howto interop with word 2007
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
catch (Exception err)
{
errormessage = err.Message;
}
return true; }
else
{
errormessage = "ERROR: Inputfile not found";
} return false; } }
}
//Form1.cs 的内容
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace PDFConverter
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
//调用convert函数
//请问这里怎么调用,这里是一个batton把。我想调用convert函数实现,doc转换pdf格式
路径写死 本人c#新手,换忘高手多多指点,另外 c#就一个cs文件把?和以前的c++不一样吧,c++ 有.cpp .h
文件。那c#如何包含其他cs文件内容呢?不需要头文件?
}
}
}
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Word;namespace PDFConverter
{
public class RemoteConverter : MarshalByRefObject
{
object paramMissing = Type.Missing;
public string errormessage;
private bool wordavailable = false;
private bool checkedword = false; public bool WordIsAvailable()
{
//don't check every time.... first time only
if (!checkedword)
{ try
{
ApplicationClass wordApplication = new ApplicationClass();
wordApplication.Visible = true;
wordApplication.Quit(ref paramMissing, ref paramMissing,
ref paramMissing);
wordavailable = true;
}
catch
{
wordavailable = false;
}
checkedword = true;
}
return wordavailable; } public bool convert(string source, string output)
{
if (System.IO.File.Exists(source))
{
//start conversion
try
{
ApplicationClass wordApplication = new ApplicationClass(); Document wordDocument = null;
object paramSourceDocPath = source; string paramExportFilePath = output; //this part is copied from Microsoft MSDN //set exportformat to pdf
WdExportFormat paramExportFormat = WdExportFormat.wdExportFormatPDF;
bool paramOpenAfterExport = false;
WdExportOptimizeFor paramExportOptimizeFor =
WdExportOptimizeFor.wdExportOptimizeForPrint;
WdExportRange paramExportRange = WdExportRange.wdExportAllDocument;
int paramStartPage = 0;
int paramEndPage = 0;
WdExportItem paramExportItem = WdExportItem.wdExportDocumentContent;
bool paramIncludeDocProps = true;
bool paramKeepIRM = true;
WdExportCreateBooks paramCreateBooks =
WdExportCreateBooks.wdExportCreateWordBooks;
bool paramDocStructureTags = true;
bool paramBitmapMissingFonts = true;
bool paramUseISO19005_1 = false; try
{
// Open the source document.
wordDocument = wordApplication.Documents.Open(
ref paramSourceDocPath, ref paramMissing, ref paramMissing,
ref paramMissing, ref paramMissing, ref paramMissing,
ref paramMissing, ref paramMissing, ref paramMissing,
ref paramMissing, ref paramMissing, ref paramMissing,
ref paramMissing, ref paramMissing, ref paramMissing,
ref paramMissing); // Export it in the specified format.
if (wordDocument != null)
wordDocument.ExportAsFixedFormat(paramExportFilePath,
paramExportFormat, paramOpenAfterExport,
paramExportOptimizeFor, paramExportRange, paramStartPage,
paramEndPage, paramExportItem, paramIncludeDocProps,
paramKeepIRM, paramCreateBooks, paramDocStructureTags,
paramBitmapMissingFonts, paramUseISO19005_1,
ref paramMissing);
}
catch (Exception ex)
{
// Respond to the error
errormessage = ex.Message;
}
finally
{
// Close and release the Document object.
if (wordDocument != null)
{
wordDocument.Close(ref paramMissing, ref paramMissing,
ref paramMissing);
wordDocument = null;
} // Quit Word and release the ApplicationClass object.
if (wordApplication != null)
{
wordApplication.Quit(ref paramMissing, ref paramMissing,
ref paramMissing);
wordApplication = null;
} //i don't know why this is here two times. I just copied it from the MSDN howto interop with word 2007
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
catch (Exception err)
{
errormessage = err.Message;
}
return true; }
else
{
errormessage = "ERROR: Inputfile not found";
} return false; } }
}
//Form1.cs 的内容
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace PDFConverter
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
//调用convert函数
//请问这里怎么调用,这里是一个batton把。我想调用convert函数实现,doc转换pdf格式
路径写死 本人c#新手,换忘高手多多指点,另外 c#就一个cs文件把?和以前的c++不一样吧,c++ 有.cpp .h
文件。那c#如何包含其他cs文件内容呢?不需要头文件?
}
}
}
解决方案 »
- C#不用BHO怎样查找并点击IE6网页内连接 文本框赋值?谢谢!!
- 类型转换的问题
- 取出HeaderTemplate中Label控件,不用FindControl
- 一个关于IE的问题?恼死了……
- C# 4.0语言将出现重大改变,带来一段Code Preview
- 关于XML的小问题,着急!!
- 如何实现=(TextBox)tbx.FindControl("text")+i这样的效果?
- 开心智力题:十枚硬币排5直排.每排4枚硬币,怎么排?
- 思归老大来看看,c#下如何检测客户端有没有安装adobe公司的热reader等pdf浏览器,搞不定就别做mvp了,给我50万分
- 这样入门c#
- 反射问题
- AllowUserToAddRows=True 实现数据添加到数据库,怎么做
RemoteConverter RC = new RemoteConverter();//创建类的实例
bool result = RC.convert(string source, string output);
result 即为结果了
RemoteConverter con = new RemoteConverter();
con.convert("c:\\1.cod","c:\\1.pdf");