| View previous topic :: View next topic |
| Author |
Message |
alfour

Joined: 16 Aug 2004 Posts: 989
|
Posted: Tue Jul 07, 2009 12:02 pm Post subject: How to convert a Word document to PDF using PDX Writer |
|
|
PDX Writer is our free software tool for converting various documents into PDF format. PDX Writer runs as a virtual printer so literally any application with a print functionality can convert a document to a PDF file. Additionaly, PDX Writer can use an electronic Signature Pad to capture your signature and paste it into any place in output PDF document.
Here is how you can use PDX Writer to convert a Word document to a PDF format.
First, download PDX Writer from our website. Run the setup. You will notice a PDX Writer printer added to the system.
Second, launch PDX Writer configuration utility, select Copy to checkbox, and enter a path to a folder you want PDX Writer to deposit newly created PDF files. Save changes.
When PDX Writer finishes with PDF file creation it copies newly created PDF file to a folder you specified above and stores the full path to the PDF file into the Registry at HKEY_CURRENT_USER\Software\NeOT\PDX Writer key, value=newfile. Finally, PDX Writer opens a Windows event object with the name "Global\PDX_WRITER" and signals it so any application that waits on the same event object will get notification.
You will need to use Microsoft Office Word application to print a Word document to PDX Writer. Create Java wrappers for Microsoft Word using Java2COM Automation Wizard.
In this example we will use WMI to control Windows printers. Create Java wrappers for WMI as well.
In your Java application, launch Microsoft Word and load a document you want to convert to PDF format. Since Microsoft Word doesn't provide an interface to print to any printer - just to default printer - we will need to set PDX Writer as a default printer then revert back to original default printer once Word finishes printing.
Create an event object and wait till PDX Writer copies newly created PDF file and signals completion. Finally retrieve file name from the registry and grab the file.
| Code: | Apartment.initialize();
//Launch Microsoft Word
_Application app = new _Application();
app.createInstance();
//dont show any message boxes
app.SetDisplayAlerts(wdAlertsNone);
Documents docs = app.GetDocuments();
//open document
_Document doc=docs.Open(new COMVariant(fileName));
//create event to communicate with PDX Writer
int event=createEvent("Global\\PDX_WRITER",false);
if (event == 0)
{
System.out.println("Unable to create Global\\PDX_WRITER");
}
//get current default printer
String def = getDefaultPrinter();
//make PDX Writer default printer
setDefaultPrinter("PDX Writer");
//print
doc.PrintOut(new COMVariant(false));
doc.Release();
docs.Release();
//quit Word
app.Quit();
app.Release();
//reset original default printer
setDefaultPrinter(def);
if(event!=0) //if event exists
{
//wait PDX_WRITER
int res=wait(event,20000);
if (res == 0)
{
//PDF Write signals
String pdf = regGetValue("Software\\NeOT\\PDX Writer", "newfile");
//open pdf
shellExecute("open",pdf);
}
else
{
System.out.println("wait failed res=" + res);
}
}
Apartment.uninitialize(); |
The full source, Java wrappers are here |
|
| Back to top |
|
 |
alfour

Joined: 16 Aug 2004 Posts: 989
|
Posted: Tue Jul 07, 2009 12:19 pm Post subject: |
|
|
Here is the rest of the code
| Code: | //enumerate Win32_Printers and retuns default printer name
private String getDefaultPrinter() throws COMException
{
String printer = null;
IWbemLocator loc = new IWbemLocator();
loc.createInstance();
IWbemServices[] svs = new IWbemServices[1];
loc.ConnectServer("\\\\.\\root\\cimv2", null, null, null, 0, null, null, svs);
IWbemServices service = svs[0];
COMAuthInfo authInfo = new COMAuthInfo(null, null, null,
COMAuthInfo.RPC_C_AUTHN_LEVEL_CONNECT,
COMAuthInfo.RPC_C_IMP_LEVEL_IMPERSONATE);
service.CoSetProxyBlanket(authInfo);
String query = "SELECT * FROM Win32_Printer";
IEnumWbemClassObject[] _enum = new IEnumWbemClassObject[1];
svs[0].ExecQuery("WQL", query,
WBEM_FLAG_ENSURE_LOCATABLE | WBEM_FLAG_FORWARD_ONLY,
null, _enum);
while (true)
{
IWbemClassObject[] objs = new IWbemClassObject[1];
int[] ret = new int[1];
int timeout = 1000;
COMHResult hr = _enum[0].Next(timeout, 1, objs, ret);
if (hr.is(COMHResult.S_OK))
{
IWbemClassObject obj = objs[0];
if (getBooleanValue(obj, "Default"))
{
printer = getStringValue(obj, "Caption");
break;
}
obj.Release();
}
else
break;
}
_enum[0].Release();
svs[0].Release();
service.Release();
loc.Release();
return printer;
}
//enumerate Win32_Printers, finds a printer by name, sets as default printer
private void setDefaultPrinter(String printer) throws COMException
{
IWbemLocator loc = new IWbemLocator();
loc.createInstance();
IWbemServices[] svs = new IWbemServices[1];
loc.ConnectServer("\\\\.\\root\\cimv2", null, null, null, 0, null, null, svs);
IWbemServices service = svs[0];
COMAuthInfo authInfo = new COMAuthInfo(null, null, null,
COMAuthInfo.RPC_C_AUTHN_LEVEL_CONNECT,
COMAuthInfo.RPC_C_IMP_LEVEL_IMPERSONATE);
service.CoSetProxyBlanket(authInfo);
String query = "SELECT * FROM Win32_Printer";
IEnumWbemClassObject[] _enum = new IEnumWbemClassObject[1];
svs[0].ExecQuery("WQL", query,
WBEM_FLAG_ENSURE_LOCATABLE | WBEM_FLAG_FORWARD_ONLY,
null, _enum);
while (true)
{
IWbemClassObject[] objs = new IWbemClassObject[1];
int[] ret = new int[1];
int timeout = 1000;
COMHResult hr = _enum[0].Next(timeout, 1, objs, ret);
if (hr.is(COMHResult.S_OK))
{
IWbemClassObject obj = objs[0];
if (printer.equals(getStringValue(obj, "Caption")))
{
String path = getStringValue(obj, "__RELPATH");
IWbemClassObject[] out = new IWbemClassObject[] { new IWbemClassObject() };
IWbemCallResult[] res = new IWbemCallResult[] { new IWbemCallResult() };
svs[0].ExecMethod(path, "SetDefaultPrinter", 0, null, null, out, res);
out[0].Release();
res[0].Release();
break;
}
obj.Release();
}
else
break;
}
_enum[0].Release();
svs[0].Release();
service.Release();
loc.Release();
}
//gets String property
private String getStringValue(IWbemClassObject obj, String name) throws COMException
{
COMVariant[] pVar = new COMVariant[1];
pVar[0] = new COMVariant();
int[] pType = new int[1];
int[] pFlavor = new int[1];
obj.Get(name, 0, pVar, pType, pFlavor);
String s = pVar[0].asString();
pVar[0].free();
return s;
}
//gets Boolean property
private boolean getBooleanValue(IWbemClassObject obj, String name) throws COMException
{
COMVariant[] pVar = new COMVariant[1];
pVar[0] = new COMVariant();
int[] pType = new int[1];
int[] pFlavor = new int[1];
obj.Get(name, 0, pVar, pType, pFlavor);
boolean s = pVar[0].asBoolean();
pVar[0].free();
return s;
} |
|
|
| Back to top |
|
 |
alfour

Joined: 16 Aug 2004 Posts: 989
|
Posted: Tue Jul 07, 2009 12:21 pm Post subject: |
|
|
Even more code ...
| Code: | //creates Win32 event
private int createEvent(String name, boolean state)
{
Coroutine coro = new Coroutine("KERNEL32", "CreateEventA");
coro.addArgNull();
coro.addArg(false);
coro.addArg(state);
coro.addArg(name);
coro.invoke();
return coro.answerAsInteger();
}
//wait Win32 event
private int wait(int handle, int howLong) throws SecurityException
{
Coroutine coro = new Coroutine("KERNEL32", "WaitForSingleObject");
coro.addArg(handle);
coro.addArg(howLong);
int rc = coro.invoke();
if (rc != 0)
return rc;
return coro.answerAsInteger();
}
//close Win32 handle
private boolean closeHandle(int handle)
{
Coroutine coro = new Coroutine("KERNEL32", "CloseHandle");
coro.addArg(handle);
coro.invoke();
return coro.answerAsBoolean();
}
//gets a String value from the Registry
private String regGetValue(String subkey, String valueName)
{
int KEY_ACCESS_QUERY_VALUE=0x0001;
int HKEY_CURRENT_USER=0x80000001;
int key=regOpenKey(HKEY_CURRENT_USER, subkey, KEY_ACCESS_QUERY_VALUE);
if (key == 0)
return null;
String val = regQueryValue(key, valueName);
regCloseKey(key);
return val;
}
//close a handle to a Registry key
private int regCloseKey(int key)
{
Coroutine co = new Coroutine("advapi32", "RegCloseKey");
co.addArg(key);
if (co.invoke() != 0)
return -1;
int ret = co.answerAsInteger();
if (ret != 0)
return ret;
return 0;
}
//opens a Registry key
private int regOpenKey(int key, String subkey, int accessMask)
{
Coroutine co = new Coroutine("advapi32", "RegOpenKeyExA");
co.addArg(key);
co.addArg(subkey);
co.addArg(0);
co.addArg(accessMask);
co.addArg(new byte[4]);
if (co.invoke() != 0)
return -1;
int ret = co.answerAsInteger();
if (ret != 0)
return 0;
return co.intFromParameterAt(4);
}
//gets a value from the Registry
private String regQueryValue(int key, String valueName)
{
int ERROR_MORE_DATA = 234;
int bufferSize = 10;
boolean success = false;
Coroutine co;
int ret;
while (true)
{
co = new Coroutine("advapi32", "RegQueryValueExA");
co.addArg(key);
co.addArg(valueName);
co.addArgNull();
co.addArg(new byte[4]);
co.addArg(new byte[bufferSize]);
byte[] sz = new byte[4];
Coroutine.setDWORDAtOffset(sz, bufferSize, 0);
co.addArg(sz);
if (co.invoke() != 0)
return null;
ret = co.answerAsInteger();
if (ret == 0)
{
success = true;
bufferSize = co.intFromParameterAt(5);
break;
}
else if (ERROR_MORE_DATA == ret)
bufferSize = co.intFromParameterAt(5);
else
break;
}
if (!success)
return null;
return new String(co.byteArrayFromParameterAt(4, bufferSize-1));
}
//performs an operation on a file
private int shellExecute(String operation, String file)
{
int SW_SHOW = 5;
Coroutine co = new Coroutine("shell32", "ShellExecuteA");
co.addArg(0);
co.addArg(operation);
co.addArg(file);
co.addArgNull();
co.addArgNull();
co.addArg(SW_SHOW);
if (co.invoke() != 0)
return -1;
return co.answerAsInteger();
} |
|
|
| Back to top |
|
 |
JoshTshirt
Joined: 19 May 2010 Posts: 1
|
Posted: Wed May 19, 2010 6:22 am Post subject: |
|
|
This is a better PDF converter than freepdf.  |
|
| 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 © phpBB Group
|