我想在不使用Microsoft互操作库的情况下将word文件转换为PDF格式? [英] I want to convert a word file into PDF format without using Microsoft interop library?
问题描述
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
string extention = Path.GetExtension(FileUpload1.PostedFile.FileName);
if (extention.ToLower() == ".doc" || extention.ToLower() == ".docx")
{
string path = System.IO.Path.GetFullPath(FileUpload1.PostedFile.FileName);
string filename = TextBox1.Text;
FileUpload1.SaveAs(Server.MapPath("pdf\\") + filename);
ConvertTopdf(path, filename);
Label1.Text = "File upload Successfull";
}
else
{
Label1.Text = "Please Select the word file only";
}
}
else
{
Label1.Text = "Please Selet the File";
}
}
public void ConvertTopdf(string path, string filename)
{
SautinSoft.UseOffice u = new SautinSoft.UseOffice();
if (u.InitWord() == 0)
{
//convert Word (RTF, DOC, DOCX to PDF)
u.ConvertFile(path, Server.MapPath("pdf\\") + filename, SautinSoft.UseOffice.eDirection.DOC_to_PDF);
}
u.CloseOffice();
}
我的尝试:
protected void Button1_Click(object sender,EventArgs e)
{
if(FileUpload1.HasFile)
{
string extention = Path.GetExtension(FileUpload1.PostedFile.FileName);
if(extention.ToLower()==。doc|| extention.ToLower()== .docx)
{
string path = System.IO.Path.GetFullPath(FileUpload1.PostedFile.FileName);
string filename = TextBox1.Text;
FileUpload1.SaveAs(Server.MapPath(pdf\\)+ filename);
ConvertTopdf(path,filename);
Label1.Text =文件上传成功;
}
其他
{
Label1.Text =请只选择单词文件;
}
}
else
{
Label1.Text =请选择文件;
}
}
public void ConvertTopdf(字符串路径,字符串文件名)
{
SautinSoft.UseOffice u = new SautinSoft.UseOffice();
if(u.InitWord()== 0)
{
//将Word(RTF,DOC,DOCX转换为PDF)
u.ConvertFile(path,Server.MapPath(pdf \\\ \\)+ filename,SautinSoft.UseOffice.eDirection.DOC_to_PDF);
}
u.CloseOffice();
}
What I have tried:
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
string extention = Path.GetExtension(FileUpload1.PostedFile.FileName);
if (extention.ToLower() == ".doc" || extention.ToLower() == ".docx")
{
string path = System.IO.Path.GetFullPath(FileUpload1.PostedFile.FileName);
string filename = TextBox1.Text;
FileUpload1.SaveAs(Server.MapPath("pdf\\") + filename);
ConvertTopdf(path, filename);
Label1.Text = "File upload Successfull";
}
else
{
Label1.Text = "Please Select the word file only";
}
}
else
{
Label1.Text = "Please Selet the File";
}
}
public void ConvertTopdf(string path, string filename)
{
SautinSoft.UseOffice u = new SautinSoft.UseOffice();
if (u.InitWord() == 0)
{
//convert Word (RTF, DOC, DOCX to PDF)
u.ConvertFile(path, Server.MapPath("pdf\\") + filename, SautinSoft.UseOffice.eDirection.DOC_to_PDF);
}
u.CloseOffice();
}
推荐答案
当然,使用Office互操作将是一个糟糕的解决方案。最好使用Microsoft Open XML SDK:
https:// msdn .microsoft.com / zh-CN / library / office / bb448854.aspx ,
https://www.microsoft.com/en-us/download/details.aspx?id=30425 。
这是一个开源替代方案: NPOI 。
这个是一组可以使用的PDF库参考: http://csharp-source.net/open-source/ pdf-libraries 。
另见我过去的答案:
问题在没有办公室或开放办公室的情况下将单词转换为PDF,
如何从MS Visual Studio 2010中的添加引用添加microsoft excel 15.0对象库 [ ^ ]。
-SA
Of course, using Office interop would be a bad solution. It's better to use Microsoft Open XML SDK:
https://msdn.microsoft.com/en-us/library/office/bb448854.aspx,
https://www.microsoft.com/en-us/download/details.aspx?id=30425.
This is one open-source alternative: NPOI.
This is a set of references to PDF libraries you can use: http://csharp-source.net/open-source/pdf-libraries.
See also my past answers:
Question Convert word to PDF without office or open office,
How to add microsoft excel 15.0 object library from Add Reference in MS Visual Studio 2010[^].
—SA
>
这篇关于我想在不使用Microsoft互操作库的情况下将word文件转换为PDF格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!