| View previous topic :: View next topic |
| Author |
Message |
LarsB OOo Advocate


Joined: 31 Aug 2005 Posts: 445 Location: Hamburg, Germany
|
Posted: Thu Dec 22, 2005 11:00 am Post subject: Commandline PDF converter tool |
|
|
HI,
in short, because have not the time for a long explantion.
Here's the code for full commandline PDF converter written
in C# using the CLI Assemblies. It should be simple to port
this to other programming lanaguages.
I will write some more comments tomorow
| Code: |
using System;
using System.IO;
using System.Diagnostics;
using unoidl.com.sun.star.lang;
using unoidl.com.sun.star.uno;
using unoidl.com.sun.star.bridge;
using unoidl.com.sun.star.frame;
namespace ConsoleApplication1
{
/// <summary>
/// usage pdfconverter sourcefile targetfile [1]
/// use 1 for overwrite the target.
/// </summary>
class PdfConverter
{
[STAThread]
static void Main(string[] args)
{
if(!CheckArguments(args))
return;
if(StartOpenOffice())
{
//Get a ComponentContext
unoidl.com.sun.star.uno.XComponentContext xLocalContext =
uno.util.Bootstrap.bootstrap();
//Get MultiServiceFactory
unoidl.com.sun.star.lang.XMultiServiceFactory xRemoteFactory =
(unoidl.com.sun.star.lang.XMultiServiceFactory)
xLocalContext.getServiceManager();
//Get a CompontLoader
XComponentLoader aLoader =
(XComponentLoader) xRemoteFactory.createInstance( "com.sun.star.frame.Desktop" );
//Load the sourcefile
XComponent xComponent = initDocument(aLoader,
PathConverter(args[0]), "_blank");
//Wait for loading
while(xComponent == null)
{
System.Threading.Thread.Sleep(1000);
}
saveDocument(xComponent, PathConverter(args[1]));
//Wait for input
Console.WriteLine("Conversation completed!");
}
}
/// <summary>
/// Load a given file or create a new blank file
/// </summary>
/// <param name="aLoader">A ComponentLoader</param>
/// <param name="file">The file</param>
/// <param name="target">The target</param>
/// <returns>Th Component</returns>
static XComponent initDocument(
XComponentLoader aLoader, string file, string target
)
{
XComponent xComponent = aLoader.loadComponentFromURL(
file, target, 0,
new unoidl.com.sun.star.beans.PropertyValue[0] );
return xComponent;
}
/// <summary>
/// Saves the document.
/// </summary>
/// <param name="xComponent">The x component.</param>
/// <param name="fileName">Name of the file.</param>
static void saveDocument(XComponent xComponent, string fileName)
{
unoidl.com.sun.star.beans.PropertyValue[] propertyValue =
new unoidl.com.sun.star.beans.PropertyValue[1];
propertyValue[0] = new unoidl.com.sun.star.beans.PropertyValue();
propertyValue[0].Name = "Filter";
propertyValue[0].Value = new uno.Any("writer_pdf_Export");
((XStorable)xComponent).storeToURL(fileName, propertyValue);
}
/// <summary>
/// Convert into OO file format
/// </summary>
/// <param name="file">The file.</param>
/// <returns>The converted file</returns>
private static string PathConverter(string file)
{
try
{
file = file.Replace(@"\", "/");
return "file:///"+file;
}
catch(System.Exception ex)
{
throw ex;
}
}
/// <summary>
/// Checks the arguments.
/// </summary>
/// <param name="args">The args.</param>
/// <returns>True if arguments are ok otherwise false</returns>
private static bool CheckArguments(string[] args)
{
bool overwrite = false;
if(args.Length >= 2)
{
if(File.Exists(args[0]))
{
if(args[1].ToLower().EndsWith(".pdf"))
{
if(args.Length > 2)
if(args[2] == "1")
overwrite = true;
if(File.Exists(args[1]) && overwrite)
{
File.Delete(args[1]);
}
else if(File.Exists(args[1]) && !overwrite)
{
Console.WriteLine("Targetfile exist use 1 as third argument to overwrite the target file!");
return false;
}
}
}
}
else
{
Console.WriteLine("Use sourcefile targetfile [1]!");
return false;
}
return true;
}
/// <summary>
/// Starts the open office.
/// </summary>
/// <returns></returns>
private static bool StartOpenOffice()
{
Process[] ps = Process.GetProcessesByName("soffice.exe");
if(ps != null)
{
if(ps.Length > 0)
return true;
else
{
Process p = Process.Start("soffice.exe");
//spent some time to start
System.Threading.Thread.Sleep(3000);
}
}
return true;
}
}
}
|
As mentioned tomorrow I will write some more info;)
Cheers
LarsB _________________ AODC - A free OpenDocument Converter
AODL - An independent OpenDocument Library C#
EmbeddedOpenOffice .net UserControl C#
EmbeddedOpenOffice Visual Studio .net Add In
http://www.OpenDocument4all.com/ |
|
| Back to top |
|
 |
schello Newbie

Joined: 27 Mar 2006 Posts: 3
|
Posted: Mon Mar 27, 2006 11:38 pm Post subject: |
|
|
Hi,
i tried your example., using .NET framework 2.0.50727. I have a successfull built but unfortunately the filter doesn't work. No matter if I use "text" or "writer_pdf_export" for the propertyValue[0].value i always get the same outputformat (mimetypeapplication/vnd.oasis.opendocument.textPK)
any hints?
regards
bernhard |
|
| Back to top |
|
 |
schello Newbie

Joined: 27 Mar 2006 Posts: 3
|
Posted: Tue Mar 28, 2006 8:14 am Post subject: solved |
|
|
propertyValue[0].Name = "FilterName" not "Filter"  |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|