如何在C#或VB.NET中打印.ODT文件?

时间:2018-03-14 16:30:15

标签: c# .net vb.net openoffice-writer

我在VB.NET中有一个程序打开并更改带有ODT扩展名的文档,我使用的是AODL dll,代码为trex:

Imports AODL.Document.TextDocuments

Dim document As New TextDocument
document.Load(PathLayout)
SearchAndReplaceString(document, "@NAME", "JOSÉ")
SearchAndReplaceString(document, "@LASTNAME", "HARTMANN")
document.SaveTo(PathLayout& nameArchive & ".odt")
document.Dispose()

 Public Sub SearchAndReplaceString(ByRef document As TextDocument, ByVal searchText As String, ByVal replaceText As String)

我该怎么做才能打印这份文件?

我尝试了以下代码:

enter image description here

但是,对象无法识别

1 个答案:

答案 0 :(得分:0)

图片中的代码显然基于https://wiki.openoffice.org/wiki/AODL_example_17,使用OpenOfficeLib.Connection。我找不到一个获得这个库的好地方,听起来你可能也没有。

相反,我建议坚持使用LibreOffice SDK附带的标准unoidl库。以下是使用VB.NET打印文档的示例。它是根据SDK附带的WriterDemo示例改编的。

Imports System
Imports Microsoft.VisualBasic
Imports unoidl.com.sun.star.lang
Imports unoidl.com.sun.star.uno
Imports unoidl.com.sun.star.bridge
Imports unoidl.com.sun.star.view
Imports uno.util
Imports System.Environment
Module WriterDemo
    Sub Main()
        Dim unoPath As String = "C:\Program Files (x86)\LibreOffice 5\program"
        SetEnvironmentVariable("UNO_PATH", unoPath, EnvironmentVariableTarget.Process)
        SetEnvironmentVariable("PATH", GetEnvironmentVariable("PATH") + ";" + unoPath, EnvironmentVariableTarget.Process)
        Dim xContext As XComponentContext = Bootstrap.bootstrap()
        Dim xFactory As XMultiServiceFactory = DirectCast(xContext.getServiceManager(), XMultiServiceFactory)
        Dim xDesktop As unoidl.com.sun.star.frame.XDesktop
        xDesktop = DirectCast(xFactory.createInstance("com.sun.star.frame.Desktop"), unoidl.com.sun.star.frame.XDesktop)
        Dim xComponentLoader As unoidl.com.sun.star.frame.XComponentLoader
        xComponentLoader = DirectCast(xDesktop, unoidl.com.sun.star.frame.XComponentLoader)
        Dim arProps() As unoidl.com.sun.star.beans.PropertyValue = New unoidl.com.sun.star.beans.PropertyValue() {}
        Dim xComponent As unoidl.com.sun.star.lang.XComponent
        xComponent = xComponentLoader.loadComponentFromURL("private:factory/swriter", "_blank", 0, arProps)
        Dim xTextDocument As unoidl.com.sun.star.text.XTextDocument
        xTextDocument = DirectCast(xComponent, unoidl.com.sun.star.text.XTextDocument)
        Dim xText As unoidl.com.sun.star.text.XText = xTextDocument.getText()
        Dim xSimpleText As unoidl.com.sun.star.text.XSimpleText
        xSimpleText = DirectCast(xText, unoidl.com.sun.star.text.XSimpleText)
        Dim xCursor As unoidl.com.sun.star.text.XTextCursor = xSimpleText.createTextCursor()
        xText.insertString(xCursor, "The first line in the newly created text document." & vbLf, False)
        Dim xPrintable As unoidl.com.sun.star.view.XPrintable
        xPrintable = DirectCast(xComponent, unoidl.com.sun.star.view.XPrintable)
        Dim printOpts() As unoidl.com.sun.star.beans.PropertyValue = New unoidl.com.sun.star.beans.PropertyValue() {}
        xPrintable.print(printOpts)
    End Sub
End Module

运行代码的说明:VB.Net example for LibreOffice SDK