LibreOffice net core 中怎么使用将pptx 转 pdf
在 LibreOffice net core 中,可以使用 UNO API 来将 pptx 文件转换为 pdf。以下是示例代码:
using System;
using System.IO;
using unoidl.com.sun.star.beans;
using unoidl.com.sun.star.bridge;
using unoidl.com.sun.star.frame;
using unoidl.com.sun.star.lang;
using unoidl.com.sun.star.uno;
using uno.util;
namespace PptxToPdfConverter
{
class Program
{
static void Main(string[] args)
{
string inputFile = "test.pptx";
string outputFile = "test.pdf";
// initialize the UNO runtime
var xLocalContext = Bootstrap.bootstrap();
var xRemoteFactory = (XMultiServiceFactory)xLocalContext.getServiceManager();
// create a new instance of LibreOffice
var xComponentContext = (XComponentContext)xRemoteFactory.createInstance("com.sun.star.uno.XComponentContext");
var xConnector = (XConnector)xRemoteFactory.createInstance("com.sun.star.connection.Connector");
var xConnection = xConnector.connect("socket,host=localhost,port=2083");
var xBridgeFactory = (XBridgeFactory)xRemoteFactory.createInstance("com.sun.star.bridge.BridgeFactory");
var xBridge = xBridgeFactory.createBridge("", "urp", "socket,host=localhost,port=2083", xComponentContext);
var xRemoteServiceManager = (XMultiServiceFactory)xBridge.getInstance("StarOffice.ServiceManager");
// get the desktop service
var xDesktop = (XComponentLoader)xRemoteServiceManager.createInstance("com.sun.star.frame.Desktop");
// load the pptx file
var xComponent = xDesktop.loadComponentFromURL($"file:///{Path.GetFullPath(inputFile)}", "_blank", 0, new PropertyValue[0]);
// export the pptx to pdf
var xStorable = (XStorable)xComponent;
var xExportFilter = (XFilter)xRemoteServiceManager.createInstance("com.sun.star.drawing.PDF");
var xExportProperties = new PropertyValue[]
{
new PropertyValue { Name = "ReduceImageResolution", Value = new Any(true) },
new PropertyValue { Name = "MaxImageResolution", Value = new Any(150) }
};
xStorable.storeToURL($"file:///{Path.GetFullPath(outputFile)}", xExportProperties);
// close the pptx file
var xCloseable = (XCloseable)xComponent;
xCloseable.close(false);
Console.WriteLine("Done!");
}
}
}
在这个示例中,我们使用 com.sun.star.frame.Desktop 服务加载 pptx 文件,然后使用 com.sun.star.drawing.PDF 过滤器将其导出为 pdf 文件。在导出过程中,我们设置了一些导出选项,例如将图像分辨率降低以减小文件大小。最后,我们使用 XCloseable 接口关闭打开的 pptx 文件。
请注意,此示例假设已经在本机上安装了 LibreOffice 并且正在运行。如果未安装 LibreOffice,您需要先安装它,并将 com.sun.star.connection.Connector 的参数中的 port 更改为适当的值
原文地址: https://www.cveoy.top/t/topic/fJJm 著作权归作者所有。请勿转载和采集!