asp.net和vb.net将msword转换为pdf [英] asp.net and vb.net convert msword to pdf

查看:65
本文介绍了asp.net和vb.net将msword转换为pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I am writing an asp.net application using Visual Studio 2010. Need to convert MS Word documents to PDF.  Must have both documents.  The vb.net code is:
<pre lang="vb">
Const wdFileFormatPDF = 17
Dim objWord As Microsoft.Office.Interop.Word.Application
Dim objDocument As Microsoft.Office.Interop.Word.Document
objWord = GetObject("", "Word.Application";)
objDocument = objWord.Documents.Open(WordFileName, False, False)
objDocument.ExportAsFixedFormat(PDFFileName, wdFileFormatPDF)
objWord.Quit()
Return True
</pre>

The process works when I run the website using Visual Studio.  When I build the system and run as an internet application, the process fails on the 6th line because there is no instance of the objDocument since the previous statement didn't open the document, which exists.  The MS Word document is created using OpenXML.

What is the correct way to convert MS Word to PDF in ASP.Net with vb.net?

Thanks for any help.

推荐答案

从来没有真正的转换,因为Word和PDF概念不同:Word文档流畅,PDF完全固定;这就像电子纸一样。您只能导出Word文档的一个可能的视图。最好的模拟是打印Word文档。



Microsoft Word文件格式是专有的(即使现代格式是开放的,这是Open XML, https://en.wikipedia.org/wiki/Office_Open_XML [ ^ ])与W3标准无关。浏览器的作者试图遵循标准,甚至几乎不能实现Word文件或任何其他Microsoft Office文件的查看。最好避免这些文件。



因此,如果您真的非常需要使用它们,由于某种原因,您必须以某种格式呈现办公室文件数据浏览器可以理解,例如HTML。这几乎不可能是一对一的对应关系,但可以非常接近。在使用ASP.NET时,可以在服务器端使用Microsoft Open XML SDK。请参阅我最近关于该主题和我引用的文章的答案:从文件读取数据到C#.net应用程序 [ ^ ]。



另请参阅此处引用的过去答案:如何从MS Visual Studio 2010中的添加引用添加microsoft excel 15.0对象库 [ ^ ]。



https://msdn.microsoft.com/en-us/library/office/bb448854 .aspx [ ^ ],

http://www.microsoft.com/en-us/download/details.aspx?id=30425 [ ^ ],

http://openxmldeveloper.org [ ^ ]。



Microsoft Open Technologies的开源: https://github.com/OfficeDev/Open-XML-SDK [ ^ ]。



现在,关于阅读Word文档:https://msdn.microsoft.com/en-us/library/office/ff478541.aspx [ ^ ]。



此CodeProject文章可以也很有用:使用OpenXML和C#编辑Word文档而不使用自动化/互操作 [ ^ ]。



这里,没有自动化/互操作是必不可少的。请参阅Microsoft文章解释为什么不建议使用互操作:

http://support.microsoft.com/default.aspx?scid=kb;EN-US;q257757#kb2 [ ^ ],

http://support.microsoft.com/kb/257757/en-us [ ^ ]。



首先,打开XML SDK,您不必安装专有和封闭源的Microsoft Office。



另请参阅此处引用的过去答案:如何从MS Visual Studio 2010中的添加引用添加microsoft excel 15.0对象库 [ ^ ]。



And最后,为了使用PDF,这是一组可以使用的PDF库: http:// csharp- source.net/open-source/pdf-libraries



-SA
There is never really "convert", because Word and PDF concepts are different: Word document is fluid, and PDF is totally fixed; this is something as electronic paper. You can export only one of the possible views of the Word document. The best analogue to this is printing of a Word document.

Microsoft Word file formats are proprietary (even though the modern format is open, this is Open XML, https://en.wikipedia.org/wiki/Office_Open_XML[^]) and have nothing to do with W3 standards. The authors of browsers, trying to follow standards, will hardly even implement viewing of Word files or any other Microsoft Office files. These files are best avoided.

So, if you really badly need to use them, by some reason, you will have to present the office file data in some format which a browser can understand, such as HTML. This hardly can be one-to-one correspondence, but can be pretty close. As you are using ASP.NET, you can use Microsoft Open XML SDK on the server side. Please see my recent answer on the topic and the articles I referenced: Read data from file to C#.net application[^].

See also my past answers referenced here: How to add microsoft excel 15.0 object library from Add Reference in MS Visual Studio 2010[^].

https://msdn.microsoft.com/en-us/library/office/bb448854.aspx[^],
http://www.microsoft.com/en-us/download/details.aspx?id=30425[^],
http://openxmldeveloper.org[^].

Open Source from Microsoft Open Technologies: https://github.com/OfficeDev/Open-XML-SDK[^].

Now, about reading a Word document: https://msdn.microsoft.com/en-us/library/office/ff478541.aspx[^].

This CodeProject article can also be useful: Edit Word Documents using OpenXML and C# Without Automation/Interop[^].

Here, "Without Automation/Interop" is essential. Please see Microsoft articles explaining why using interop is not recommended:
http://support.microsoft.com/default.aspx?scid=kb;EN-US;q257757#kb2[^],
http://support.microsoft.com/kb/257757/en-us[^].

First of all, with Open XML SDK, you don't have to install proprietary and closed-source Microsoft Office.

See also my past answers referenced here: How to add microsoft excel 15.0 object library from Add Reference in MS Visual Studio 2010[^].

And finally, to work with PDF, this is a set of referenced to PDF libraries you can use: http://csharp-source.net/open-source/pdf-libraries.

—SA


这篇关于asp.net和vb.net将msword转换为pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆