Skip to content

Commit

Permalink
Importing the project
Browse files Browse the repository at this point in the history
  • Loading branch information
mephraim committed Jul 9, 2009
0 parents commit c2568e0
Show file tree
Hide file tree
Showing 14 changed files with 417 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Tests/*.suo
*/bin/*
*/obj/*
108 changes: 108 additions & 0 deletions GhostScriptSharp/GhostscriptSharp.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace GhostscriptSharp
{
/// <summary>
/// Wraps the Ghostscript API with a C# interface
/// </summary>
public class GhostscriptWrapper
{
#region Hooks into Ghostscript DLL
[DllImport("gsdll32.dll", EntryPoint = "gsapi_new_instance")]
private static extern int CreateAPIInstance(out IntPtr pinstance, IntPtr caller_handle);

[DllImport("gsdll32.dll", EntryPoint = "gsapi_init_with_args")]
private static extern int InitAPI(IntPtr instance, int argc, string[] argv);

[DllImport("gsdll32.dll", EntryPoint = "gsapi_exit")]
private static extern int ExitAPI(IntPtr instance);

[DllImport("gsdll32.dll", EntryPoint = "gsapi_delete_instance")]
private static extern void DeleteAPIInstance(IntPtr instance);
#endregion

/// <summary>
/// Generates a thumbnail jpg for the pdf at the input path and saves it
/// at the output path
/// </summary>
public static void GeneratePageThumb(string inputPath, string outputPath, int page, int width, int height)
{
GeneratePageThumbs(inputPath, outputPath, page, page, width, height);
}

/// <summary>
/// Generates a collection of thumbnail jpgs for the pdf at the input path
/// starting with firstPage and ending with lastPage.
/// Put "%d" somewhere in the output path to have each of the pages numbered
/// </summary>
public static void GeneratePageThumbs(string inputPath, string outputPath, int firstPage, int lastPage, int width, int height)
{
CallAPI(GetArgs(inputPath, outputPath, firstPage, lastPage, width, height));
}

/// <summary>
/// Calls the Ghostscript API with a collection of arguments to be passed to it
/// </summary>
private static void CallAPI(string[] args)
{
// Get a pointer to an instance of the Ghostscript API and run the API with the current arguments
IntPtr gsInstancePtr;
CreateAPIInstance(out gsInstancePtr, IntPtr.Zero);
InitAPI(gsInstancePtr, args.Length, args);

Cleanup(gsInstancePtr);
}

/// <summary>
/// Frees up the memory used for the API arguments and clears the Ghostscript API instance
/// </summary>
private static void Cleanup(IntPtr gsInstancePtr)
{
ExitAPI(gsInstancePtr);
DeleteAPIInstance(gsInstancePtr);
}

/// <summary>
/// Returns an array of arguments to be sent to the Ghostscript API
/// </summary>
/// <param name="inputPath">Path to the source file</param>
/// <param name="outputPath">Path to the output file</param>
/// <param name="firstPage">The page of the file to start on</param>
/// <param name="lastPage">The page of the file to end on</param>
private static string[] GetArgs(string inputPath, string outputPath, int firstPage, int lastPage, int width, int height)
{
return new[]
{
// Keep gs from writing information to standard output
"-q",
"-dQUIET",

"-dPARANOIDSAFER", // Run this command in safe mode
"-dBATCH", // Keep gs from going into interactive mode
"-dNOPAUSE", // Do not prompt and pause for each page
"-dNOPROMPT", // Disable prompts for user interaction
"-dMaxBitmap=500000000", // Set high for better performance

// Set the starting and ending pages
String.Format("-dFirstPage={0}", firstPage),
String.Format("-dLastPage={0}", lastPage),

// Configure the output anti-aliasing, resolution, etc
"-dAlignToPixels=0",
"-dGridFitTT=0",
"-sDEVICE=jpeg",
"-dTextAlphaBits=4",
"-dGraphicsAlphaBits=4",
String.Format("-r{0}x{1}", width, height),

// Set the input and output files
String.Format("-sOutputFile={0}", outputPath),
inputPath
};
}
}
}
60 changes: 60 additions & 0 deletions GhostScriptSharp/GhostscriptSharp.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?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>{56605627-E6FA-4F47-9440-FB877CEA5C84}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>GhostscriptSharp</RootNamespace>
<AssemblyName>GhostscriptSharp</AssemblyName>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</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.Core">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Drawing" />
<Reference Include="System.Xml.Linq">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Data.DataSetExtensions">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="GhostscriptSharp.cs" />
</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>
30 changes: 30 additions & 0 deletions GhostScriptSharp/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
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("GhostscriptSharp")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyProduct("GhostscriptSharp")]
[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("56ecd9b7-8386-481f-8603-3215c77c2eb6")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
[assembly: AssemblyVersion("0.3")]
[assembly: AssemblyFileVersion("0.3")]
26 changes: 26 additions & 0 deletions Tests/GhostscriptSharpTests.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

Microsoft Visual Studio Solution File, Format Version 10.00
# Visual Studio 2008
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GhostscriptSharpTests", "GhostscriptSharpTests\GhostscriptSharpTests.csproj", "{2B5A3CF6-D6A9-49D5-B88E-419DA34F1C20}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GhostscriptSharp", "..\GhostscriptSharp\GhostscriptSharp.csproj", "{56605627-E6FA-4F47-9440-FB877CEA5C84}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{2B5A3CF6-D6A9-49D5-B88E-419DA34F1C20}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2B5A3CF6-D6A9-49D5-B88E-419DA34F1C20}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2B5A3CF6-D6A9-49D5-B88E-419DA34F1C20}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2B5A3CF6-D6A9-49D5-B88E-419DA34F1C20}.Release|Any CPU.Build.0 = Release|Any CPU
{56605627-E6FA-4F47-9440-FB877CEA5C84}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{56605627-E6FA-4F47-9440-FB877CEA5C84}.Debug|Any CPU.Build.0 = Debug|Any CPU
{56605627-E6FA-4F47-9440-FB877CEA5C84}.Release|Any CPU.ActiveCfg = Release|Any CPU
{56605627-E6FA-4F47-9440-FB877CEA5C84}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
40 changes: 40 additions & 0 deletions Tests/GhostscriptSharpTests/GhostscriptSharpTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using NUnit.Framework;
using GhostscriptSharp;
using System.IO;

namespace GhostscriptSharpTests
{
[TestFixture]
public class GhostscriptSharpTests
{
private readonly string TEST_FILE_LOCATION = "test.pdf";
private readonly string SINGLE_FILE_LOCATION = "output.jpg";
private readonly string MULTIPLE_FILE_LOCATION = "output%d.jpg";

private readonly int MULTIPLE_FILE_PAGE_COUNT = 10;

[Test]
public void GenerateSinglePageThumbnail()
{
GhostscriptWrapper.GeneratePageThumb(TEST_FILE_LOCATION, SINGLE_FILE_LOCATION, 1, 100, 100);
Assert.IsTrue(File.Exists(SINGLE_FILE_LOCATION));
}

[Test]
public void GenerateMultiplePageThumbnails()
{
GhostscriptWrapper.GeneratePageThumbs(TEST_FILE_LOCATION, MULTIPLE_FILE_LOCATION, 1, MULTIPLE_FILE_PAGE_COUNT, 100, 100);
for (var i = 1; i <= MULTIPLE_FILE_PAGE_COUNT; i++)
Assert.IsTrue(File.Exists(String.Format("output{0}.jpg", i)));
}

[TearDown]
public void Cleanup()
{
File.Delete(SINGLE_FILE_LOCATION);
for (var i = 1; i <= MULTIPLE_FILE_PAGE_COUNT; i++)
File.Delete(String.Format("output{0}.jpg", i));
}
}
}
76 changes: 76 additions & 0 deletions Tests/GhostscriptSharpTests/GhostscriptSharpTests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?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>{2B5A3CF6-D6A9-49D5-B88E-419DA34F1C20}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>GhostScriptSharpTests</RootNamespace>
<AssemblyName>GhostScriptSharpTests</AssemblyName>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</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="nunit.framework, Version=2.5.0.9122, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL" />
<Reference Include="System" />
<Reference Include="System.Core">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Xml.Linq">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Data.DataSetExtensions">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="GhostscriptSharpTests.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="test.pdf">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<Content Include="gsdll32.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</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>
5 changes: 5 additions & 0 deletions Tests/GhostscriptSharpTests/GhostscriptSharpTests.csproj.user
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<ProjectView>ShowAllFiles</ProjectView>
</PropertyGroup>
</Project>
33 changes: 33 additions & 0 deletions Tests/GhostscriptSharpTests/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
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("GhostscriptSharpTests")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyProduct("GhostscriptSharpTests")]
[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("3e435c97-69dc-46ca-a6ed-b207670e02ff")]

// 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")]
Binary file added Tests/GhostscriptSharpTests/gsdll32.dll
Binary file not shown.
Binary file added Tests/GhostscriptSharpTests/test.pdf
Binary file not shown.
Loading

0 comments on commit c2568e0

Please sign in to comment.