Look for the installed Ghostscript libraries:
List<GhostscriptVersionInfo> gsVersions =
GhostscriptVersionInfo.GetInstalledVersions(GhostscriptLicense.GPL | GhostscriptLicense.AFPL);
Using GhostscriptRasterizer class:
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
// required Ghostscript.NET namespaces
using Ghostscript.NET;
using Ghostscript.NET.Rasterizer;
namespace Ghostscript.NET.Samples
{
public class RasterizerSample : ISample
{
private GhostscriptVersionInfo _lastInstalledVersion = null;
private GhostscriptRasterizer _rasterizer = null;
public void Start()
{
int desired_x_dpi = 96;
int desired_y_dpi = 96;
string inputPdfPath = @"F:\pdf\155.pdf";
string outputPath = @"E:\_pdf_out\";
_lastInstalledVersion =
GhostscriptVersionInfo.GetLastInstalledVersion(
GhostscriptLicense.GPL | GhostscriptLicense.AFPL,
GhostscriptLicense.GPL);
_rasterizer = new GhostscriptRasterizer();
_rasterizer.Open(inputPdfPath, _lastInstalledVersion, false);
for (int pageNumber = 1; pageNumber <= _rasterizer.PageCount; pageNumber++)
{
string pageFilePath = Path.Combine(outputPath, "Page-" + pageNumber.ToString() + ".png");
Image img = _rasterizer.GetPage(desired_x_dpi, desired_y_dpi, pageNumber);
img.Save(pageFilePath, ImageFormat.Png);
Console.WriteLine(pageFilePath);
}
}
}
}
Render postscript to the screen:
_interpreter = new GhostscriptInterpreter(lastVersion); _interpreter.Setup(new StdIOHandler(txtOutput), new DisplayHandler(_preview.pbDisplay)); _interpreter.InitArgs(_args); _interpreter.Run(txtPostscript.Text);
How to run multiple instances:
// make sure that constructor 'fromMemory' option is set to true if // you want to run multiple instances of the Ghostscript GhostscriptProcessor processor1 = new GhostscriptProcessor(_gs_verssion_info, true); GhostscriptProcessor processor2 = new GhostscriptProcessor(_gs_verssion_info, true);
Using GhostscriptViewer class:
using System;
using System.Collections.Generic;
using System.Drawing;
// required Ghostscript.NET namespaces
using Ghostscript.NET;
using Ghostscript.NET.Viewer;
namespace Ghostscript.NET.Samples.Samples
{
public class DisplayPdfSample : ISample
{
private GhostscriptVersionInfo _lastInstalledVersion = null;
private GhostscriptViewer _viewer = null;
private Bitmap _pdfPage = null;
public void Start()
{
// there can be multiple Ghostscript versions installed on the system
// and we can choose which one we will use. In this sample we will use
// the last installed Ghostscript version. We can choose if we want to
// use GPL or AFPL (commercial) version of the Ghostscript. By setting
// the parameters below we told that we want to fetch the last version
// of the GPL or AFPL Ghostscript and if both are available we prefer
// to use GPL version.
_lastInstalledVersion =
GhostscriptVersionInfo.GetLastInstalledVersion(
GhostscriptLicense.GPL | GhostscriptLicense.AFPL,
GhostscriptLicense.GPL);
// create a new instance of the viewer
_viewer = new GhostscriptViewer();
// set the display update interval to 10 times per second. This value
// is milliseconds based and updating display every 100 milliseconds
// is optimal value. The smaller value you set the rasterizing will
// take longer as DisplayUpdate event will be raised more often.
_viewer.ProgressiveUpdateInterval = 100;
// attach three main viewer events
_viewer.DisplaySize += new GhostscriptViewerViewEventHandler(_viewer_DisplaySize);
_viewer.DisplayUpdate += new GhostscriptViewerViewEventHandler(_viewer_DisplayUpdate);
_viewer.DisplayPage += new GhostscriptViewerViewEventHandler(_viewer_DisplayPage);
// open PDF file using the last Ghostscript version. If you want to use
// multiple viewers withing a single process then you need to pass 'true'
// value as the last parameter of the method below in order to tell the
// viewer to load Ghostscript from the memory and not from the disk.
_viewer.Open("E:\test\test.pdf",_lastInstalledVersion, false);
}
// this is the first raised event before PDF page starts rasterizing.
// this event is raised only once per page showing and it tells us
// the dimensions of the PDF page and gives us page image reference.
void _viewer_DisplaySize(object sender, GhostscriptViewerViewEventArgs e)
{
// store PDF page image reference
_pdfPage = e.Image;
}
// this event is raised when a tile or the part of the page is rasterized
// code in this event must be fast or it will slow down the Ghostscript
// rasterizing.
void _viewer_DisplayUpdate(object sender, GhostscriptViewerViewEventArgs e)
{
// if we are displaying the image in the PictureBox we can update
// it by calling PictureBox.Invalidate() and PictureBox.Update()
// methods. We dont need to set image reference again because
// Ghostscript.NET is changing Image object directly in the
// memory and does not create new Bitmap instance.
}
// this is the last raised event after complete page is rasterized
void _viewer_DisplayPage(object sender, GhostscriptViewerViewEventArgs e)
{
// complete PDF page is rasterized and we can update our PictureBox
// once again by calling PictureBox.Invalidate() and PictureBox.Update()
}
// dummy method just to list other viewer properties and methods
private void Other_Viewer_Methods()
{
// show first pdf page
_viewer.ShowFirstPage();
// show previous pdf page
_viewer.ShowPreviousPage();
// show next pdf page
_viewer.ShowNextPage();
// show last pdf page
_viewer.ShowLastPage();
// show page based on page number
_viewer.ShowPage(6);
// refresh current page / rasterize it again
_viewer.RefreshPage();
// zoom in
_viewer.ZoomIn();
// zoom out
_viewer.ZoomOut();
// get first page number
int fpn = _viewer.FirstPageNumber;
// get last page number
int lpn = _viewer.LastPageNumber;
// get current page number
int cpn = _viewer.CurrentPageNumber;
}
}
}
Other:
using System;
using System.Collections.Generic;
using System.Threading;
namespace Ghostscript.NET.Samples
{
public class RunMultipleInstancesSample : ISample
{
private GhostscriptVersionInfo _gs_verssion_info =
GhostscriptVersionInfo.GetLastInstalledVersion(GhostscriptLicense.GPL | GhostscriptLicense.AFPL, GhostscriptLicense.GPL);
public void Start()
{
ThreadPool.QueueUserWorkItem(new WaitCallback(Instance1));
ThreadPool.QueueUserWorkItem(new WaitCallback(Instance2));
}
private void Process(string input, string output, int startPage, int endPage)
{
// make sure that constructor 'fromMemory' option is set to true if
// you want to run multiple instances of the Ghostscript
Ghostscript.NET.Processor.GhostscriptProcessor processor = new Processor.GhostscriptProcessor(_gs_verssion_info, true);
processor.Process(CreateTestArgs(input, output, startPage, endPage), new ConsoleStdIO(true, true, true));
}
private void Instance1(object target)
{
// export pdf pages to images
Process(@"E:\mc-1.pdf", @"E:\_pdf_out\a_test-%03d.png", 1, 100);
}
private void Instance2(object target)
{
// export pdf pages to images
Process(@"E:\mc-2.pdf", @"E:\_pdf_out\b_test-%03d.png", 1, 100);
}
private string[] CreateTestArgs(string inputPath, string outputPath, int pageFrom, int pageTo)
{
List<string> gsArgs = new List<string>();
gsArgs.Add("-q");
gsArgs.Add("-dSAFER");
gsArgs.Add("-dBATCH");
gsArgs.Add("-dNOPAUSE");
gsArgs.Add("-dNOPROMPT");
gsArgs.Add(@"-sFONTPATH=" + System.Environment.GetFolderPath(System.Environment.SpecialFolder.Fonts));
gsArgs.Add("-dFirstPage=" + pageFrom.ToString());
gsArgs.Add("-dLastPage=" + pageTo.ToString());
gsArgs.Add("-sDEVICE=png16m");
gsArgs.Add("-r72");
gsArgs.Add("-sPAPERSIZE=a4");
gsArgs.Add("-dNumRenderingThreads=" + Environment.ProcessorCount.ToString());
gsArgs.Add("-dTextAlphaBits=4");
gsArgs.Add("-dGraphicsAlphaBits=4");
gsArgs.Add(@"-sOutputFile=" + outputPath);
gsArgs.Add(@"-f" + inputPath);
return gsArgs.ToArray();
}
}
}