asp.NET c#:作为控制台应用程序而不是web应用程序工作的代码



我的基本问题是将.docx文件转换为.pdf。如果允许我使用Microsoft.Office.Interop.Word.dll,问题就会得到解决,但我没有使用,因为服务器不会安装MS Office。所以我需要一个免费/开源的库,这样我就可以做到这一点

http://www.docx4java.org/blog/2014/09/docx-to-pdf-in-c-net/

只要我把它作为控制台应用程序运行,它就可以正常工作。以下是相关的代码片段:

string fileIN = @"C:Users...Visual Studio 2013ProjectsHRDappHRDappLetter_TemplatesAP.docx";
string fileOUT = @"C:Users...Visual Studio 2013ProjectsHRDappHRDappLetter_TemplatesAP.pdf";
log.Info("Hello from Common Logging");
// Necessary, if slf4j-api and slf4j-NetCommonLogging are separate DLLs
ikvm.runtime.Startup.addBootClassPathAssembly(
System.Reflection.Assembly.GetAssembly(
typeof(org.slf4j.impl.StaticLoggerBinder)));
// Configure to find docx4j.properties
// .. add as URL the dir containing docx4j.properties (not the file itself!)
Plutext.PropertiesConfigurator.setDocx4jPropertiesDir(projectDir + @"srcsamplesresources");
java.io.File file = new java.io.File(fileIN);
// OK, do it..
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(file);
java.io.FileOutputStream fos = new java.io.FileOutputStream(new java.io.File(fileOUT));
org.docx4j.Docx4J.toPDF(wordMLPackage, fos);
fos.close();

如果在Web应用程序中使用此代码,则代码运行良好,直到

java.io.File file = new java.io.File(fileIN);

并被卡住

WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(file);

虽然文件路径是正确的,并且在控制台应用程序中运行良好,但我似乎缺少其他东西。日志还打印以下语句-

iisexpress.exe信息:0:[INFO]org.docx4j.jaxb.Context-使用Java 6/7 jaxb实现

并停止。任何能让我找到错误来源的回复都会很有帮助。谢谢

正如Jeroen(著名的IKVM)所解释的,当没有主程序集(例如在ASP.NET应用程序中)时,当代码试图动态加载类时,IKVM类加载器无法找到您的程序集。

所以你想添加的不仅仅是:

ikvm.runtime.Startup.addBootClassPathAssembly(
System.Reflection.Assembly.GetAssembly(
typeof(org.slf4j.impl.StaticLoggerBinder)));

而且:

ikvm.runtime.Startup.addBootClassPathAssembly(
System.Reflection.Assembly.GetAssembly(
typeof(org.slf4j.LoggerFactory)));
ikvm.runtime.Startup.addBootClassPathAssembly(
System.Reflection.Assembly.GetAssembly(
typeof(org.docx4j.jaxb.Context)));   

最新更新