.OpenOffice 2.4.1 was used during writing of article. At the end of the article there is link to the instructions that will make code work with OpenOffice 3. IntroductionI must confess that I’m not a big fan of PDF. Still, it somehow manages to wiggle in almost every project I'm on – clients want to send out documents, Word is bounded to Windows, HTML is lame, PDF it is. Unfortunately, the situation with it and C# haven’t changed much in couple past years - if there were no new, fancy, priced components, I would conclude that it’s almost the same as it was in.NET 1.1 times – it is a pain to create PDFs.For those of you who have access to components which can convert popular formats to PDF, this article is pretty much useless. But, for those who don’t want or simply can’t shell out over $1000 for a chance to convert other formats to PDF – I hope that this solution will prove as an attractive alternative.
IdeaDuring a talk with my friend Toni Ruža (who is primarily a Python developer) about a way to easily convert some WordML reports to PDF, he pointed me to the headless OpenOffice mode. It seems that it has been around for quite some time, but as it is mainly targeted at Java developers, it is no wonder that there were no big fuss about it in C# groups. Still, it promises much – you install OpenOffice, start it in Service mode, send commands over the API, and get to use any feature it provides. More than anything else, my interest was to load any supported format into OpenOffice and then export it as PDF.Just to note, in this article, I'll talk about creating PDF from other documents, not from scratch. If you are looking for a way to do that, I'm encouraging you to first take a look at my article. Follow it, and you'll easily create WordML files (like the one used here) from a database or XML.
Create & Manipulate PDF Documents On the Fly. Aspose.PDF for.NET offers the capability to create PDF documents from scratch. You can use its API or transform images, XML, XSL-FO, HTML, SVG, PCL, XPS, EPUB or simple text files to PDF documents. Creating a PDF document from the scratch using Syncfusion.NET C ore library is easy. This article explains how to create a PDF file in Blazor framework using C#. Visual Studio 2019.
Solution architectureI was saddened to find out that the headless mode of OpenOffice just minimizes GUI operations, not totally avoiding them. As someone who has a pretty nasty experience with Word.Application.Open (using interactive applications such as Word by programmatically mimicking user actions), I started thinking on how to isolate OpenOffice and query it independently of the main application process, thus enabling loose coupling and a more stable environment. The result was a Windows Service which wraps the OpenOffice process, taking care of the security context and the usage, while providing the needed functionality over Remoting (am I a service-oriented freak or what?:)).Here is a diagram presenting the classes used in the process. Figure 2 – Testing pageBelieve it or not, that’s it! You now have a functioning PDF converter which can be queried from C#, by Remoting.During the wrapper implementation, I thought about multi-threading and (hopefully) made calling the ConvertToPDF thread safe. Conversion requests are queued and processed one by one, so the Open Office Wrapper Service can be used by more than one application and, why not, from multiple machines too (the generic sender for the application running on other machines should then be initialized with tcp://%machineHostingService%:6543/OpenOfficeServiceReceiver).
ConfigurationCurrently, there are the following settings for the Open Office Wrapper Service:. Port – It’s the port on which the service will listen for requests. By default, it is 6543. ProcessName – The name of the OpenOffice process (used when searching the process list to see if OO is alive). When you start OpenOffice in headless mode, it is soffice.bin (instead of soffice.exe). PathToOpenOffice – Self-explanatory, eh? If you have installed OpenOffice on a path other than the default, you should change this setting (the default path is c:Program FilesOpenOffice.org 2.4programsoffice.exe; on x64 machines, add (x86) after Program Files).
SecondsIdleAllowed – When a conversion request is submitted, OpenOfficeController checks if OO is running in the background, and if not, starts soffice.exe in headless mode. By default, if no new request is made in 60 seconds, the OpenOffice process will be killed. CheckIntervalInSeconds – The interval in which the service evaluates OpenOffice usage (bound to the previous setting). By default, it is 30 seconds. RequestTimeoutInSeconds – The time in which a response is expected from OpenOffice.
If the item stays in the queue for too long or OpenOffice gets too big a file for processing, a Timeout Exception will be thrown. The default wait is 30 seconds.Running in-process?I would like to mention once again that the Windows Service I wrote is only there to provide a security context and serve as a bridge to OpenOfficeWrapper.dll that implements the main stuff when it comes to communicating with OpenOffice. If you wish, you can directly reference OpenOfficeWrapper.dll and perform PDF conversions in-process, but you must be sure that your application will be run with sufficient security privileges! In my testing, the conversion was successful only if I run the application under an account that belonged to the Administrator group.Also, you could run into trouble when trying to run OpenOfficeWrapper on x64 versions of Windows. I’ve had tons of trouble trying to get my Web Application to convert PDF by using the OpenOfficeWrapper in process on a Windows 2003 x64 machine. So, if you really do not need to have everything in your application’s process, leave the code that wraps OpenOffice separated, and use it through a Windows Service.
Words of warnings and words of thanksTo me, the documentation of OpenOffice is terrible. OK, I could be another C# 'quasi-developer' who finds it easier to look at examples than to crawl through bunch of Wiki pages, diagrams, and forum posts just to get a couple lines of code that opens a document. But, for me - after an absolute champion of useless information, unrelated links, and broken searches in the - the is one more example of how you do not want your documentation to be organized. From what I’ve seen, OpenOffice is a great product considering the cost ($0), and it is a shame that I can’t say the same for its documentation.On the other hand, posts of server users on the are really helpful; I would specifically like to thank LarsB, tcedi, and DannyB. Most of the conversion code in ConvertToPDF.cs is taken from LarsB’s; so, thank you man – I hope you’ll continue to post useful snippets. ConclusionWith this article, I aimed at a simple goal – to provide an easy-to-follow, free, and versatile solution for converting documents to PDF by using C#. I am aware that there are technically more robust solutions, but I do not know any of them that’s free.
![C generate random double C generate random double](/uploads/1/2/4/0/124097390/342118222.gif)
If you know – please post it in the comments section along with an impression of this article.Enjoy!;) References., (thanks to goakiwi)History. January 8 th, 2009 - Added references.
July 22 nd, 2008 - Initial version of the article. Yinjiuwen 22-Apr-Apr-13 22:26“/”应用程序中的服务器错误。找不到指定的模块。 (异常来自 HRESULT:0x8007007E)说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。异常详细信息: System.IO.FileNotFoundException: 找不到指定的模块。 (异常来自 HRESULT:0x8007007E)源错误:行 21: byte wordML = File.ReadAllBytes(source);行 22:行 23: byte result = OpenOfficeService.Objects.GenericSender.Receiver.ConvertToPDF(wordML);行 24:行 25: // Write response to client源文件: C:UsersAdministratorDownloadsarticlesrcSolutionPDFWebDefault.aspx.cs 行: 23. Generalaelius 21-Jun-12 2:5821-Jun-12 2:58Hello I'm using OpenOffice 3.4.0, and I'm getting the following error:Method not found: 'unoidl.com.sun.star.uno.XComponentContext uno.util.Bootstrap.bootstrap'.' Marc lang 23-Dec-10 4:1823-Dec-10 4:18Hi,I have this working - kinda!Using OO 3.2, followed the steps in other thread and have it generating a PDF.However. In the 'finally' block after it has read the bytes from the PDF and returns them, my client is getting the error:The process cannot access the file 'C:Documents and SettingsDefault UserLocal SettingsApplication Datagnr-source-54183780.xml' because it is being used by another process.So looks as though the XML file is locked?Has anyone else experienced this?Bit of a shame, looks like it's going to leave the temp files on the server.I may try roll back to OO 2.4 to see if it helps.
. PDF files from scratch. Add digital signatures, barcodes, charts & much more.
and conversions. Simple object model. Unlimited (Royalty free) licensing optionsDynamicPDF Generator for.NET allows software developers to add PDF generation to any.NET Standard application (C# or VB.NET).
Our award winning product is used for real-timecreation of PDF documents based on dynamic data. PDF documents can be generatedstraight to a Web Form, file, byte array or System.IO.Stream object. The object model is intuitive and easy to learn,yet at the same time is very powerful. With a Free Evaluation Edition to try and withflexible and Royalty Free licensing options, why not start using DynamicPDF Generator Today!
Server LicensingServer based licensing is intended for developers who wish to install our software on their server or web server.It is licensed per production server and allows an unlimited number of installations on developmentworkstations and development servers. This licensing model is ideal for those who wish to installour software on a limited number of servers and allows us to offer a lower price to developerswho do not intend to distribute our software to a large number of servers or workstations. Annual Maintenance Plan.
Priority E-Mail Support (.