- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在开发一个使用 LibreOffice 执行邮件合并的 C# 应用程序。
我可以执行邮件合并并将结果保存为 pdf,但在调用 xDesktop.terminate()
后发生崩溃,并且在下次打开 LibreOffice 时出现崩溃报告。
每次我使用 com.sun.star.text.MailMerge
服务并关闭 LibreOffice 时,用作邮件合并基础的模型都不会从临时文件夹中删除。
例如文件:%TEMP%\lu97964g78o.tmp\lu97964g78v.tmp
%TEMP%\lu97964g78o.tmp\SwMM0.odt
看来我没有正确关闭 MailMerge 服务。
重现 Writer 崩溃的最少代码:
// Program.cs
using System;
using System.IO;
namespace LibreOffice_MailMerge
{
class Program
{
static void Main(string[] args)
{
// LibreOffice crash after calling xDesktop.terminate().
// The crash reporting appear when the second itaration begins.
int i;
for (i = 0; i < 2; i++)
{
//Minimal code to reproduce the crash.
using (var document = new TextDocument())
{
document.MailMerge();
}
}
}
}
}
// TextDocument.cs
using Microsoft.Win32;
using System;
using unoidl.com.sun.star.frame;
using unoidl.com.sun.star.lang;
using unoidl.com.sun.star.uno;
namespace LibreOffice_MailMerge
{
class TextDocument : IDisposable
{
private XComponentContext localContext;
private XMultiComponentFactory serviceManager;
private XDesktop xDesktop;
public TextDocument()
{
InitializeEnvironment(); // Add LibreOffice in PATH environment variable.
localContext = uno.util.Bootstrap.bootstrap();
serviceManager = localContext.getServiceManager();
xDesktop = (XDesktop)serviceManager.createInstanceWithArgumentsAndContext("com.sun.star.frame.Desktop", new uno.Any[] { }, localContext);
}
public void MailMerge()
{
// #############################################
// # No crash if these two lines are commented #
// #############################################
var oMailMerge = serviceManager.createInstanceWithArgumentsAndContext("com.sun.star.text.MailMerge", new uno.Any[] { }, localContext);
((XComponent)oMailMerge).dispose();
}
public void Dispose()
{
if (xDesktop != null)
{
xDesktop.terminate();
}
}
}
}
操作系统:Windows 10 64位和Windows 7 32位
LibreOffice 和 SDK 版本:5.3.0.3 x86(还测试了 5.2.4.2 和 5.2.5.1 x86)
LibreOffice 快速入门:已禁用
Crashreport
Complete Visual Studio project在 GitHub 上。
非常感谢任何能告诉我哪里错了的人。
编辑:更新代码并提交错误报告。
编辑 2:希望做一些有用的事情,我发布了上述问题的解决方法。
基本上,我通过将要在其中创建新用户配置文件的目录作为参数传递来启动 LibreOffice 进程。
我还更改了 tmp
环境变量的路径,仅让 LibreOffice 进程指向上一个目录。
当我完成工作后,我删除了这个包含崩溃报告和由 LibreOffice API 错误创建的临时文件的目录。
程序.cs:
using System;
using System.IO;
namespace LibreOffice_MailMerge
{
class Program
{
static void Main(string[] args)
{
// Example of mail merge.
using (var document = new WriterDocument())
{
var modelPath = Path.Combine(Environment.CurrentDirectory, "Files", "Test.odt");
var csvPath = Path.Combine(Environment.CurrentDirectory, "Files", "Test.csv");
var outputPath = Path.Combine(Path.GetTempPath(), "MailMerge.pdf");
document.MailMerge(modelPath, csvPath);
document.ExportToPdf(outputPath);
}
}
}
}
LibreOffice.cs:
using Microsoft.Win32;
using System;
using System.Diagnostics;
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;
namespace LibreOffice_MailMerge
{
class LibreOffice : IDisposable
{
// LibreOffice process.
private Process process;
// LibreOffice user profile directory.
public string UserProfilePath { get; private set; }
public XComponentContext Context { get; private set; }
public XMultiComponentFactory ServiceManager { get; private set; }
public XDesktop2 Desktop { get; private set; }
public LibreOffice()
{
const string name = "MyProjectName";
UserProfilePath = Path.Combine(Path.GetTempPath(), name);
CleanUserProfile();
InitializeEnvironment();
var arguments = $"-env:UserInstallation={new Uri(UserProfilePath)} --accept=pipe,name={name};urp --headless --nodefault --nofirststartwizard --nologo --nolockcheck";
process = new Process();
process.StartInfo.UseShellExecute = false;
process.StartInfo.FileName = "soffice";
process.StartInfo.Arguments = arguments;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.EnvironmentVariables["tmp"] = UserProfilePath;
process.Start();
var xLocalContext = uno.util.Bootstrap.defaultBootstrap_InitialComponentContext();
var xLocalServiceManager = xLocalContext.getServiceManager();
var xUnoUrlResolver = (XUnoUrlResolver)xLocalServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", xLocalContext);
for (int i = 0; i <= 10; i++)
{
try
{
ServiceManager = (XMultiComponentFactory)xUnoUrlResolver.resolve($"uno:pipe,name={name};urp;StarOffice.ServiceManager");
break;
}
catch (unoidl.com.sun.star.connection.NoConnectException)
{
System.Threading.Thread.Sleep(1000);
if (Equals(i, 10))
{
throw;
}
}
}
Context = (XComponentContext)((XPropertySet)ServiceManager).getPropertyValue("DefaultContext").Value;
Desktop = (XDesktop2)ServiceManager.createInstanceWithContext("com.sun.star.frame.Desktop", Context);
}
/// <summary>
/// Set up the environment variables for the process.
/// </summary>
private void InitializeEnvironment()
{
var nodes = new RegistryHive[] { RegistryHive.CurrentUser, RegistryHive.LocalMachine };
foreach (var node in nodes)
{
var key = RegistryKey.OpenBaseKey(node, RegistryView.Registry32).OpenSubKey(@"SOFTWARE\LibreOffice\UNO\InstallPath");
if (key != null && key.ValueCount > 0)
{
var unoPath = key.GetValue(key.GetValueNames()[key.ValueCount - 1]).ToString();
Environment.SetEnvironmentVariable("PATH", $"{unoPath};{Environment.GetEnvironmentVariable("PATH")}", EnvironmentVariableTarget.Process);
Environment.SetEnvironmentVariable("URE_BOOTSTRAP", new Uri(Path.Combine(unoPath, "fundamental.ini")).ToString(), EnvironmentVariableTarget.Process);
return;
}
}
throw new System.Exception("LibreOffice not found.");
}
/// <summary>
/// Delete LibreOffice user profile directory.
/// </summary>
private void CleanUserProfile()
{
if (Directory.Exists(UserProfilePath))
{
Directory.Delete(UserProfilePath, true);
}
}
#region IDisposable Support
private bool disposed = false;
protected virtual void Dispose(bool disposing)
{
if (!disposed)
{
if (disposing)
{
}
if (Desktop != null)
{
Desktop.terminate();
Desktop = null;
ServiceManager = null;
Context = null;
}
if (process != null)
{
// Wait LibreOffice process.
if (!process.WaitForExit(5000))
{
process.Kill();
}
process.Dispose();
}
CleanUserProfile();
disposed = true;
}
}
~LibreOffice()
{
Dispose(false);
}
public void Dispose()
{
Dispose(true);
GC.Collect();
GC.SuppressFinalize(this);
}
#endregion
}
}
WriterDocument.cs:
using System;
using System.IO;
using unoidl.com.sun.star.beans;
using unoidl.com.sun.star.frame;
using unoidl.com.sun.star.lang;
using unoidl.com.sun.star.sdb;
using unoidl.com.sun.star.task;
using unoidl.com.sun.star.text;
using unoidl.com.sun.star.util;
namespace LibreOffice_MailMerge
{
class WriterDocument : LibreOffice
{
private XTextDocument xTextDocument = null;
private XDatabaseContext xDatabaseContext;
public WriterDocument()
{
xDatabaseContext = (XDatabaseContext)ServiceManager.createInstanceWithContext("com.sun.star.sdb.DatabaseContext", Context);
}
/// <summary>
/// Execute a mail merge.
/// </summary>
/// <param name="modelPath">Full path of model.</param>
/// <param name="csvPath">>Full path of CSV file.</param>
public void MailMerge(string modelPath, string csvPath)
{
const string dataSourceName = "Test";
var dataSourcePath = Path.Combine(UserProfilePath, $"{dataSourceName}.csv");
var databasePath = Path.Combine(UserProfilePath, $"{dataSourceName}.odb");
File.Copy(csvPath, dataSourcePath);
CreateDataSource(databasePath, dataSourceName, dataSourcePath);
// Set up the mail merge properties.
var oMailMerge = ServiceManager.createInstanceWithContext("com.sun.star.text.MailMerge", Context);
var properties = (XPropertySet)oMailMerge;
properties.setPropertyValue("DataSourceName", new uno.Any(typeof(string), dataSourceName));
properties.setPropertyValue("DocumentURL", new uno.Any(typeof(string), new Uri(modelPath).AbsoluteUri));
properties.setPropertyValue("Command", new uno.Any(typeof(string), dataSourceName));
properties.setPropertyValue("CommandType", new uno.Any(typeof(int), CommandType.TABLE));
properties.setPropertyValue("OutputType", new uno.Any(typeof(short), MailMergeType.SHELL));
properties.setPropertyValue("SaveAsSingleFile", new uno.Any(typeof(bool), true));
// Execute the mail merge.
var job = (XJob)oMailMerge;
xTextDocument = (XTextDocument)job.execute(new NamedValue[0]).Value;
var model = ((XPropertySet)oMailMerge).getPropertyValue("Model").Value;
CloseDocument(model);
DeleteDataSource(dataSourceName);
((XComponent)oMailMerge).dispose();
}
/// <summary>
/// Export the document as PDF.
/// </summary>
/// <param name="outputPath">Full path of the PDF file</param>
public void ExportToPdf(string outputPath)
{
if (xTextDocument == null)
{
throw new System.Exception("You must first perform a mail merge.");
}
var xStorable = (XStorable)xTextDocument;
var propertyValues = new PropertyValue[2];
propertyValues[0] = new PropertyValue() { Name = "Overwrite", Value = new uno.Any(typeof(bool), true) };
propertyValues[1] = new PropertyValue() { Name = "FilterName", Value = new uno.Any(typeof(string), "writer_pdf_Export") };
var pdfPath = new Uri(outputPath).AbsoluteUri;
xStorable.storeToURL(pdfPath, propertyValues);
}
private void CloseDocument(Object document)
{
if (document is XModel xModel && xModel != null)
{
((XModifiable)xModel).setModified(false);
if (xModel is XCloseable xCloseable && xCloseable != null)
{
try
{
xCloseable.close(true);
}
catch (CloseVetoException) { }
}
else
{
try
{
xModel.dispose();
}
catch (PropertyVetoException) { }
}
}
}
/// <summary>
/// Register a new data source.
/// </summary>
/// <param name="databasePath">Full path of database.</param>
/// <param name="datasourceName">The name by which register the database.</param>
/// <param name="dataSourcePath">Full path of CSV file.</param>
private void CreateDataSource(string databasePath, string dataSourceName, string dataSourcePath)
{
DeleteDataSource(dataSourceName);
var oDataSource = xDatabaseContext.createInstance();
var XPropertySet = (XPropertySet)oDataSource;
// http://api.libreoffice.org/docs/idl/ref/interfacecom_1_1sun_1_1star_1_1sdb_1_1XOfficeDatabaseDocument.html
var xOfficeDatabaseDocument = ((XDocumentDataSource)oDataSource).DatabaseDocument;
var xModel = (XModel)xOfficeDatabaseDocument;
var xStorable = (XStorable)xOfficeDatabaseDocument;
// Set up the datasource properties.
var properties = new PropertyValue[9];
properties[0] = new PropertyValue() { Name = "Extension", Value = new uno.Any(typeof(string), "csv") };
properties[1] = new PropertyValue() { Name = "HeaderLine", Value = new uno.Any(typeof(bool), true) };
properties[2] = new PropertyValue() { Name = "FieldDelimiter", Value = new uno.Any(typeof(string), ";") };
properties[3] = new PropertyValue() { Name = "StringDelimiter", Value = new uno.Any(typeof(string), "\"") };
properties[4] = new PropertyValue() { Name = "DecimalDelimiter", Value = new uno.Any(typeof(string), ".") };
properties[5] = new PropertyValue() { Name = "ThousandDelimiter", Value = new uno.Any(typeof(string), "") };
properties[6] = new PropertyValue() { Name = "EnableSQL92Check", Value = new uno.Any(typeof(bool), false) };
properties[7] = new PropertyValue() { Name = "PreferDosLikeLineEnds", Value = new uno.Any(typeof(bool), true) };
properties[8] = new PropertyValue() { Name = "CharSet", Value = new uno.Any(typeof(string), "UTF-8") };
var uri = Uri.EscapeUriString($"sdbc:flat:{dataSourcePath}".Replace(Path.DirectorySeparatorChar, '/'));
XPropertySet.setPropertyValue("URL", new uno.Any(typeof(string), uri));
XPropertySet.setPropertyValue("Info", new uno.Any(typeof(PropertyValue[]), properties));
// Save the database and register the datasource.
xStorable.storeAsURL(new Uri(databasePath).AbsoluteUri, xModel.getArgs());
xDatabaseContext.registerObject(dataSourceName, oDataSource);
CloseDocument(xOfficeDatabaseDocument);
((XComponent)oDataSource).dispose();
}
/// <summary>
/// Revoke datasource.
/// </summary>
/// <param name="datasourceName">The name of datasource.</param>
private void DeleteDataSource(string datasourceName)
{
if (xDatabaseContext.hasByName(datasourceName))
{
var xDocumentDataSource = (XDocumentDataSource)xDatabaseContext.getByName(datasourceName).Value;
xDatabaseContext.revokeDatabaseLocation(datasourceName);
CloseDocument(xDocumentDataSource);
((XComponent)xDocumentDataSource).dispose();
}
}
#region IDisposable Support
private bool disposed = false;
protected override void Dispose(bool disposing)
{
if (!disposed)
{
if (disposing)
{
}
if (xTextDocument != null)
{
CloseDocument(xTextDocument);
xTextDocument = null;
}
disposed = true;
base.Dispose(disposing);
}
}
#endregion
}
}
最佳答案
如果没有崩溃,我无法让它工作,并且根据 this discussion ,其他人也遇到过同样的问题。
但是应该可以多次关闭和重新打开文档(而不是 LibreOffice 应用程序本身)而不会崩溃。
因此,首先手动或使用 shell 脚本(例如 PowerShell)打开 LibreOffice。然后运行您的应用程序。执行多个邮件合并但不调用 xDesktop.terminate()
。应用程序完成后,手动关闭 LibreOffice 或使用 shell 脚本关闭它。
结果:没有崩溃! :)
关于c# - 在 C# 中执行邮件合并时 LibreOffice 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42170214/
#include using namespace std; class C{ private: int value; public: C(){ value = 0;
这个问题已经有答案了: What is the difference between char a[] = ?string?; and char *p = ?string?;? (8 个回答) 已关闭
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 7 年前。 此帖子已于 8 个月
除了调试之外,是否有任何针对 c、c++ 或 c# 的测试工具,其工作原理类似于将独立函数复制粘贴到某个文本框,然后在其他文本框中输入参数? 最佳答案 也许您会考虑单元测试。我推荐你谷歌测试和谷歌模拟
我想在第二台显示器中移动一个窗口 (HWND)。问题是我尝试了很多方法,例如将分辨率加倍或输入负值,但它永远无法将窗口放在我的第二台显示器上。 关于如何在 C/C++/c# 中执行此操作的任何线索 最
我正在寻找 C/C++/C## 中不同类型 DES 的现有实现。我的运行平台是Windows XP/Vista/7。 我正在尝试编写一个 C# 程序,它将使用 DES 算法进行加密和解密。我需要一些实
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
有没有办法强制将另一个 窗口置于顶部? 不是应用程序的窗口,而是另一个已经在系统上运行的窗口。 (Windows, C/C++/C#) 最佳答案 SetWindowPos(that_window_ha
假设您可以在 C/C++ 或 Csharp 之间做出选择,并且您打算在 Windows 和 Linux 服务器上运行同一服务器的多个实例,那么构建套接字服务器应用程序的最明智选择是什么? 最佳答案 如
你们能告诉我它们之间的区别吗? 顺便问一下,有什么叫C++库或C库的吗? 最佳答案 C++ 标准库 和 C 标准库 是 C++ 和 C 标准定义的库,提供给 C++ 和 C 程序使用。那是那些词的共同
下面的测试代码,我将输出信息放在注释中。我使用的是 gcc 4.8.5 和 Centos 7.2。 #include #include class C { public:
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我的客户将使用名为 annoucement 的结构/类与客户通信。我想我会用 C++ 编写服务器。会有很多不同的类继承annoucement。我的问题是通过网络将这些类发送给客户端 我想也许我应该使用
我在 C# 中有以下函数: public Matrix ConcatDescriptors(IList> descriptors) { int cols = descriptors[0].Co
我有一个项目要编写一个函数来对某些数据执行某些操作。我可以用 C/C++ 编写代码,但我不想与雇主共享该函数的代码。相反,我只想让他有权在他自己的代码中调用该函数。是否可以?我想到了这两种方法 - 在
我使用的是编写糟糕的第 3 方 (C/C++) Api。我从托管代码(C++/CLI)中使用它。有时会出现“访问冲突错误”。这使整个应用程序崩溃。我知道我无法处理这些错误[如果指针访问非法内存位置等,
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
我有一些 C 代码,将使用 P/Invoke 从 C# 调用。我正在尝试为这个 C 函数定义一个 C# 等效项。 SomeData* DoSomething(); struct SomeData {
这个问题已经有答案了: Why are these constructs using pre and post-increment undefined behavior? (14 个回答) 已关闭 6
我是一名优秀的程序员,十分优秀!