Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions Examples/GhostscriptSharpExamples/Example1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System;
using System.Collections.Generic;
using System.Text;
using GhostscriptSharp;
using System.Runtime.InteropServices;

namespace Examples
{
/// <summary>
/// Example of using GS DLL as a ps2pdf converter.
/// </summary>
/// <remarks>Port of Ghostscript Example code at http://pages.cs.wisc.edu/~ghost/doc/cvs/API.htm</remarks>
class Example1
{
public const String KernelDllName = "kernel32.dll";
public const String GhostscriptDllDirectory = @"C:\Program Files\gs\gs8.71\bin";

[DllImport(KernelDllName, SetLastError = true)]
static extern int SetDllDirectory(string lpPathName);

static void Main(string[] args)
{
IntPtr minst;

int code, code1;
string[] gsargv = new string[10];
gsargv[0] = "ps2pdf"; /* actual value doesn't matter */
gsargv[1] = "-dNOPAUSE";
gsargv[2] = "-dBATCH";
gsargv[3] = "-dSAFER";
gsargv[4] = "-sDEVICE=pdfwrite";
gsargv[5] = "-sOutputFile=out.pdf";
gsargv[6] = "-c";
gsargv[7] = ".setpdfwrite";
gsargv[8] = "-f";
gsargv[9] = @"Files\input.ps";

SetDllDirectory(GhostscriptDllDirectory);

code = API.CreateAPIInstance(out minst, IntPtr.Zero);
if (code < 0)
{
System.Environment.Exit(1);
}
code = API.InitAPI(minst, gsargv.Length, gsargv);
code1 = API.ExitAPI(minst);
if ((code == 0) || (code == (int)API.GhostscriptErrorCode.e_Quit))
{
code = code1;
}

API.DeleteAPIInstance(minst);
if ((code == 0) || (code == (int)API.GhostscriptErrorCode.e_Quit))
{
System.Environment.Exit(0);
}
System.Environment.Exit(1);
}
}
}
107 changes: 107 additions & 0 deletions Examples/GhostscriptSharpExamples/Example2.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
using System;
using System.Collections.Generic;
using System.Text;
using GhostscriptSharp;
using System.Runtime.InteropServices;

namespace Examples
{
/// <summary>
/// Similar to command line gs
/// </summary>
/// <remarks>Port of Ghostscript Example code at http://pages.cs.wisc.edu/~ghost/doc/cvs/API.htm</remarks>
class Example2
{
public const String KernelDllName = "kernel32.dll";
public const String GhostscriptDllDirectory = @"C:\Program Files\gs\gs8.71\bin";
public static String start_string = "systemdict /start get exec" + System.Environment.NewLine;

[DllImport(KernelDllName, SetLastError = true)]
static extern int SetDllDirectory(string lpPathName);

[DllImport(KernelDllName, EntryPoint = "RtlMoveMemory", SetLastError = true)]
static extern int CopyMemory(IntPtr dest, IntPtr source, Int64 count);

static void Main(string[] args)
{
#region StdIn Handler
StringBuilder sbInput = new StringBuilder();
// This is very slow, especially because Ghostscript asks for input 1 character at a time
API.StdinCallback stdin = (caller_handle, str, n) =>
{
if (n == 0)
{
str = IntPtr.Zero;
return 0;
}
if (sbInput.Length == 0)
{
sbInput.AppendLine(Console.ReadLine());
}
if (sbInput.Length > 0)
{
int len = (sbInput.Length < n) ? sbInput.Length : n;
byte[] b = ASCIIEncoding.ASCII.GetBytes(sbInput.ToString(0, len));
GCHandle cHandle = GCHandle.Alloc(b, GCHandleType.Pinned);
IntPtr cPtr = cHandle.AddrOfPinnedObject();
Int64 copyLen = (long)len;
CopyMemory(str, cPtr, copyLen);
cPtr = IntPtr.Zero;
cHandle.Free();
sbInput.Remove(0, len);
return len;
}
return 0;
};
#endregion
#region StdOut Handler
API.StdoutCallback stdout = (caller_handle, buf, len) =>
{
Console.Write(buf.Substring(0, len));
return len;
};
#endregion
#region StdErr Handler
API.StdoutCallback stderr = (caller_handle, buf, len) =>
{
Console.Error.Write(buf.Substring(0, len));
return len;
};
#endregion

string[] gsargv = new string[args.Length + 1];
gsargv[0] = "GhostscriptSharp"; /* actual value doesn't matter */
Array.Copy(args, 0, gsargv, 1, args.Length);

IntPtr minst;
int code, code1;
int exit_code;

SetDllDirectory(GhostscriptDllDirectory);

code = API.CreateAPIInstance(out minst, IntPtr.Zero);
if (code < 0)
{
System.Environment.Exit(1);
}
API.Set_Stdio(minst, stdin, stdout, stderr);
code = API.InitAPI(minst, gsargv.Length, gsargv);
if (code == 0)
{
API.RunString(minst, start_string, 0, out exit_code);
}
code1 = API.ExitAPI(minst);
if ((code == 0) || (code == (int)API.GhostscriptErrorCode.e_Quit))
{
code = code1;
}

API.DeleteAPIInstance(minst);
if ((code == 0) || (code == (int)API.GhostscriptErrorCode.e_Quit))
{
System.Environment.Exit(0);
}
System.Environment.Exit(1);
}
}
}
72 changes: 72 additions & 0 deletions Examples/GhostscriptSharpExamples/Example3.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using System;
using System.Collections.Generic;
using System.Text;
using GhostscriptSharp;
using System.Runtime.InteropServices;

namespace Examples
{
/// <summary>
/// Shows how to feed GhostscriptSharp piecemeal
/// </summary>
/// <remarks>When feeding Ghostscript piecemeal buffers, one can use the normal operators to configure things and invoke library routines. The following example
/// would cause Ghostscript to open and process the file named "example.pdf" as if it had been passed as an argument to gsapi_init_with_args().</remarks>
/// <example>code = gsapi_run_string(minst, "(example.pdf) .runlibfile", 0, &exit_code);</example>
/// <remarks>Port of Ghostscript Example code at http://pages.cs.wisc.edu/~ghost/doc/cvs/API.htm</remarks>
class Example3
{
public const String KernelDllName = "kernel32.dll";
public const String GhostscriptDllDirectory = @"C:\Program Files\gs\gs8.71\bin";
public static String command = "1 2 add == flush" + System.Environment.NewLine;

[DllImport(KernelDllName, SetLastError = true)]
static extern int SetDllDirectory(string lpPathName);

[DllImport(KernelDllName, EntryPoint = "RtlMoveMemory", SetLastError = true)]
static extern int CopyMemory(IntPtr dest, IntPtr source, Int64 count);

static void Main(string[] args)
{
string[] gsargv = new string[args.Length + 1];
gsargv[0] = "GhostscriptSharp"; /* actual value doesn't matter */
Array.Copy(args, 0, gsargv, 1, args.Length);

IntPtr minst;
int code, code1;
int exit_code;

SetDllDirectory(GhostscriptDllDirectory);

code = API.CreateAPIInstance(out minst, IntPtr.Zero);
if (code < 0)
{
System.Environment.Exit(1);
}
code = API.InitAPI(minst, gsargv.Length, gsargv);
if (code == 0)
{
API.RunStringBegin(minst, 0, out exit_code);
API.RunStringContinue(minst, command, Convert.ToUInt16(command.Length), 0, out exit_code);
API.RunStringContinue(minst, "qu", 2u, 0, out exit_code);
API.RunStringContinue(minst, "it", 2u, 0, out exit_code);
API.RunStringEnd(minst, 0, out exit_code);
}
code1 = API.ExitAPI(minst);
if ((code == 0) || (code == (int)API.GhostscriptErrorCode.e_Quit))
{
code = code1;
}

API.DeleteAPIInstance(minst);
int returnValue = 1;
if ((code == 0) || (code == (int)API.GhostscriptErrorCode.e_Quit))
{
returnValue = 0;
}
Console.WriteLine("Example3 completed with exit value {0}", returnValue);
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
System.Environment.Exit(returnValue);
}
}
}
Binary file not shown.
64 changes: 64 additions & 0 deletions Examples/GhostscriptSharpExamples/GhostscriptSharpExamples.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>9.0.30729</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{6B6799EB-0AF5-4AED-8164-A7DA275EFC9D}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Examples</RootNamespace>
<AssemblyName>Examples</AssemblyName>
<TargetFrameworkVersion>v2.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<StartupObject>Examples.Example1</StartupObject>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Example3.cs" />
<Compile Include="Example2.cs" />
<Compile Include="Example1.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\GhostScriptSharp\GhostscriptSharp.csproj">
<Project>{56605627-E6FA-4F47-9440-FB877CEA5C84}</Project>
<Name>GhostscriptSharp</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="Files\input.ps">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
36 changes: 36 additions & 0 deletions Examples/GhostscriptSharpExamples/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Examples")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Examples")]
[assembly: AssemblyCopyright("Copyright © 2011")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("289eb845-b037-437a-98d9-1fe7eb6e6791")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
Loading