Java 使用文档4J将office文件转换为pdf

Java 使用文档4J将office文件转换为pdf,java,vbscript,converter,documents4j,Java,Vbscript,Converter,Documents4j,我正在寻找一个好的转换器转换成Pdf的办公室文件和图像。我想试用一下免费的源代码文档4j。并具有以下代码: package documentsForJ; import java.io.File; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; import com.documents4j.api.DocumentType; import com.documents4j.api.IConverter

我正在寻找一个好的转换器转换成Pdf的办公室文件和图像。我想试用一下免费的源代码文档4j。并具有以下代码:

package documentsForJ;
import java.io.File;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import com.documents4j.api.DocumentType;
import com.documents4j.api.IConverter;
import com.documents4j.job.LocalConverter;

public class App {
    public static void main(String[] args) {

        File wordFile = new File("test.doc"), target = new File("test.pdf");

        IConverter converter = LocalConverter.builder().baseFolder(new File("G:\\documentsForJ\\target"))
                .workerPool(20, 25, 2, TimeUnit.SECONDS)
                .processTimeout(5, TimeUnit.SECONDS).build();

        Future<Boolean> conversion = converter.convert(wordFile).as(DocumentType.DOCX).to(target).as(DocumentType.PDF)
                // .prioritizeWith(1000) // optional
                .schedule();

    }
}
生成的VBS C:\temp\excel\u start1565707660.VBS出现以下错误:

这是生成的vbs的代码:

' See http://msdn.microsoft.com/en-us/library/bb243311%28v=office.12%29.aspx
Const WdExportFormatPDF = 17
Const MagicFormatPDF = 999

Dim arguments
Set arguments = WScript.Arguments

' Transforms a file using MS Excel into the given format.
Function ConvertFile( inputFile, outputFile, formatEnumeration )

  Dim fileSystemObject
  Dim excelApplication
  Dim excelDocument

  ' Get the running instance of MS Excel. If Excel is not running, exit the conversion.
  On Error Resume Next
  Set excelApplication = GetObject(, "Excel.Application")
  If Err <> 0 Then
    WScript.Quit -6
  End If
  On Error GoTo 0

  ' Find the source file on the file system.
  Set fileSystemObject = CreateObject("Scripting.FileSystemObject")
  inputFile = fileSystemObject.GetAbsolutePathName(inputFile)


  ' Convert the source file only if it exists.
  If fileSystemObject.FileExists(inputFile) Then

    ' Attempt to open the source document.
    On Error Resume Next
    Set excelDocument = excelApplication.Workbooks.Open(inputFile, , True, , , , , , , , , , , , 2)
    If Err <> 0 Then
        WScript.Quit -2
    End If
    On Error GoTo 0

    ' Convert: See http://msdn2.microsoft.com/en-us/library/bb221597.aspx
    On Error Resume Next
    If formatEnumeration = MagicFormatPDF Then
      excelDocument.ExportAsFixedFormat xlTypePDF, outputFile
    Else
      excelDocument.SaveAs outputFile, formatEnumeration
    End If

    ' Close the source document.
    excelDocument.Close False
    If Err <> 0 Then
        WScript.Quit -3
    End If
    On Error GoTo 0

    ' Signal that the conversion was successful.
    WScript.Quit 2

  Else

    ' Files does not exist, could not convert
    WScript.Quit -4

  End If

End Function

' Execute the script.
Call ConvertFile( arguments.Unnamed.Item(0), arguments.Unnamed.Item(1), CInt(arguments.Unnamed.Item(2)) )
”请参阅http://msdn.microsoft.com/en-us/library/bb243311%28v=office.12%29.aspx
常量WdExportFormatPDF=17
常数MagicFormatPDF=999
模糊的论据
Set arguments=WScript.arguments
'使用MS Excel将文件转换为给定格式。
函数ConvertFile(inputFile、outputFile、formatEnumeration)
Dim文件系统对象
Dim Excel应用程序
Dim Excel文档
'获取正在运行的MS Excel实例。如果Excel未运行,请退出转换。
出错时继续下一步
设置excelApplication=GetObject(,“Excel.Application”)
如果错误为0,则
WScript.Quit-6
如果结束
错误转到0
'在文件系统上查找源文件。
设置fileSystemObject=CreateObject(“Scripting.fileSystemObject”)
inputFile=fileSystemObject.GetAbsolutePathName(inputFile)
'仅当源文件存在时才转换它。
如果fileSystemObject.files存在(inputFile),则
'尝试打开源文档。
出错时继续下一步
设置excelDocument=excelApplication.Workbooks.Open(inputFile,True,,,2)
如果错误为0,则
WScript.Quit-2
如果结束
错误转到0
“皈依者:见http://msdn2.microsoft.com/en-us/library/bb221597.aspx
出错时继续下一步
如果formatEnumeration=MagicFormatPDF,则
excelDocument.ExportAsFixedFormat xlTypePDF,输出文件
其他的
excelDocument.SaveAs输出文件,formatEnumeration
如果结束
'关闭源文档。
Excel文档。关闭False
如果错误为0,则
WScript.Quit-3
如果结束
错误转到0
'表示转换成功的信号。
WScript.Quit 2
其他的
'文件不存在,无法转换
WScript.Quit-4
如果结束
端函数
'执行脚本。
调用ConvertFile(arguments.Unnamed.Item(0)、arguments.Unnamed.Item(1)、CInt(arguments.Unnamed.Item(2)))

如果你能给我任何建议,如何修复这个错误,我将非常高兴。提前多谢

新发布的文档4j版本1.0.3很可能解决了此问题,该版本为转换脚本参数添加了正确的转义。以前,当VBScript处理您观察到的脚本参数时,没有逃过任何参数,这可能会导致混淆。

似乎您可能在文件中的某个位置使用了特殊字符。VBS抱怨脚本中提供的参数少于三个。目前,存在一个错误,任何文件名中的空格都没有正确转义,转换失败。您的案例中似乎存在类似的问题。请为test.doc提供正确的路径,脚本似乎无法找到它们。试着把它放在一个没有空格的文件夹中,并像
File wordFile=new File(“C:\\test\\test.doc”)那样引用它带有空格的文件夹也可以使用,但请先试用此文件夹。
' See http://msdn.microsoft.com/en-us/library/bb243311%28v=office.12%29.aspx
Const WdExportFormatPDF = 17
Const MagicFormatPDF = 999

Dim arguments
Set arguments = WScript.Arguments

' Transforms a file using MS Excel into the given format.
Function ConvertFile( inputFile, outputFile, formatEnumeration )

  Dim fileSystemObject
  Dim excelApplication
  Dim excelDocument

  ' Get the running instance of MS Excel. If Excel is not running, exit the conversion.
  On Error Resume Next
  Set excelApplication = GetObject(, "Excel.Application")
  If Err <> 0 Then
    WScript.Quit -6
  End If
  On Error GoTo 0

  ' Find the source file on the file system.
  Set fileSystemObject = CreateObject("Scripting.FileSystemObject")
  inputFile = fileSystemObject.GetAbsolutePathName(inputFile)


  ' Convert the source file only if it exists.
  If fileSystemObject.FileExists(inputFile) Then

    ' Attempt to open the source document.
    On Error Resume Next
    Set excelDocument = excelApplication.Workbooks.Open(inputFile, , True, , , , , , , , , , , , 2)
    If Err <> 0 Then
        WScript.Quit -2
    End If
    On Error GoTo 0

    ' Convert: See http://msdn2.microsoft.com/en-us/library/bb221597.aspx
    On Error Resume Next
    If formatEnumeration = MagicFormatPDF Then
      excelDocument.ExportAsFixedFormat xlTypePDF, outputFile
    Else
      excelDocument.SaveAs outputFile, formatEnumeration
    End If

    ' Close the source document.
    excelDocument.Close False
    If Err <> 0 Then
        WScript.Quit -3
    End If
    On Error GoTo 0

    ' Signal that the conversion was successful.
    WScript.Quit 2

  Else

    ' Files does not exist, could not convert
    WScript.Quit -4

  End If

End Function

' Execute the script.
Call ConvertFile( arguments.Unnamed.Item(0), arguments.Unnamed.Item(1), CInt(arguments.Unnamed.Item(2)) )