Skip to content

Instantly share code, notes, and snippets.

@jackhong
Created February 9, 2011 23:49
Show Gist options
  • Save jackhong/819605 to your computer and use it in GitHub Desktop.
Save jackhong/819605 to your computer and use it in GitHub Desktop.
using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace TestGS
{
class Program
{
static void Main(string[] args)
{
convertToPs(args[0]);
createPdf(args[1]);
}
static void convertToPs(string file)
{
try
{
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;
printProcess.Start();
// Wait until the PostScript file is created
try
{
printProcess.WaitForExit();
}
catch (InvalidOperationException) { }
printProcess.Close();
}
catch (Exception ex)
{
throw ex;
}
}
static string createPdf(string outputPath)
{
try
{
string command = "gswin32c -q -dNOPAUSE -sDEVICE=pdfwrite " +
"-sOutputFile=" + outputPath + " -fe:\\GSOUTPUT.PS";
Process pdfProcess = new Process();
StreamWriter writer;
StreamReader reader;
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;
pdfProcess.Start();
writer = pdfProcess.StandardInput;
reader = pdfProcess.StandardOutput;
writer.AutoFlush = true;
writer.WriteLine(command);
writer.Close();
string ret = reader.ReadToEnd();
return ret;
}
catch (Exception ex)
{
throw ex;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment