Let us create a small Windows application in C# to make this
whole process a reality. First, put a Textbox and a Button to browse and point
to a document that we wish to convert to PDF. Then we can have another button
that initiates the PDF conversion process.
Another important part of the process is the gswin32c.exe
that helps to convert the postscript file to PDF. So, we will need this file to
be copied to the bin\Debug folder of the project.
As stated earlier, the conversion process is a two step
process, one that converts the document to postscript and the other that
converts the postscript file to PDF. So, we will have 2 functions,convertToPs()
and convertToPDF(), that actually depicts these two process.
If we look closely at the code in Listing 1, we will get to
know that we are using the installed PDF printer to print the file. As the
Ghost PDF printer is a postscript printer, it will create the printable format
of the file as GSOUTPUT.PS.
Listing 1: convertToPs()
public void convertToPs(string file)
Process printProcess = new Process();
printProcess.StartInfo.FileName = file;
printProcess.StartInfo.Verb = "printto";
printProcess.StartInfo.Arguments = "\"Ghostscript PDF\"";
printProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
printProcess.StartInfo.CreateNoWindow = true;
// Wait until the PostScript file is created
catch (Exception ex)
This method takes in one argument, the full path of the file
which needs to be converted to PDF. Then we create a new Process, with the
FileName: The full path of the file to
be converter to PDF. This is converted to postscript in this method.
Verb: We are using "printto"
as the Process Verb. The default verb for printing any document is "print."
But, this will use the default printer to print the document. In order to use
the installed "Ghostscript PDF" printer, if it has not been set as a
default printer, we need to pass "printto" as the Verb and the
printer name as the argument.
Argument: The installed Ghostscript
These are the required properties that are to be set, to
convert our document to postscript. We can set other properties as per our
Now we are ready to start the process. The time taken to
convert the document to PDF depends on the size of the document. So, we have
used the WaitForExit() method to wait until the process completes. But we have
also used a try block where we are catching the InvalidOperationException, but
are not doing anything here.
Reason: Whenever we launch a
document, the associated application is invoked by the Operating System as a
new Process. Let us take an example. We are converting a .doc file to PDF.
Here, the OS will invoke Microsoft Word to open the document for printing. Now
say for instance, Microsoft Word is running on our system and we are editing a
different document on it. In this case, no new process is invoked; rather the
same process that was running is used to open the new word document. After the
print job completes, the process does not get disposed as the Application was
running previously. In this case we get an InvalidOperationException. Thus, we
need to catch this exception and ignore it for the normal operation of our job.
This method is the final step of our application which
converts the postscript file generated by the Ghostscript printer in the above
function to a PDF document.
Here, we are creating a process and opening the Windows
command window. Then we are using the gswin32c.exe the Command line utility
provided by Ghostscript and are converting the postscript file created by the
above function to PDF.
The function convertToPdf() in Listing 2 takes one argument:
the full path of the output .pdf file that needs to be created. Thus, if we
want to create a PDF file with the same name as the input file name, we can
just replace the extension of the inputted file with .pdf.
Listing 2: convertToPdf()
private string CreatePdf(string outputPath)
string command = "gswin32c -q -dNOPAUSE -sDEVICE=pdfwrite " +
"-sOutputFile=\" outputPath\" -fc:\\gsoutput.ps";
Process pdfProcess = new Process();
ProcessStartInfo info = new ProcessStartInfo("cmd");
info.WorkingDirectory = System.AppDomain.CurrentDomain.BaseDirectory;
info.CreateNoWindow = true;
info.UseShellExecute = false;
info.RedirectStandardInput = true;
info.RedirectStandardOutput = true;
pdfProcess.StartInfo = info;
writer = pdfProcess.StandardInput;
reader = pdfProcess.StandardOutput;
writer.AutoFlush = true;
string ret = reader.ReadToEnd();
catch (Exception ex)