简体   繁体   中英

How to print a .ODT file in C # or VB.NET?

I have a program in VB.NET that opens and changes a document with ODT extension, I am using the AODL dll, below a trex of code:

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)

what can i do for print this document?

I tried the code below:

在此处输入图片说明

But, the objects is not recognized

The code from the image, apparently based on https://wiki.openoffice.org/wiki/AODL_example_17 , uses OpenOfficeLib.Connection . I could not find a good place to obtain this library, and it sounds like you may not have it either.

Instead, I recommend sticking to the standard unoidl library that comes with the LibreOffice SDK. Here is an example of printing a document using VB.NET. It was adapted from the WriterDemo example included with the SDK.

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

Instructions for running the code: VB.Net example for LibreOffice SDK .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM