Includes:
using Ghostscript.NET;
using Ghostscript.NET.Processor;
using Ghostscript.NET.Rasterizer;
Right now, I am using Ghostscript.Net to merge several single PDFs into a single document:
/// <summary>
/// Ghostscripts the file specified in parameter 1 as a PDF to the file specified in parameter 2
/// </summary>
/// <param name="fileNames">String[]. Array of Full Paths to a file to convert to a single PDF</param>
/// <param name="outputPath">String. Full Path to where Ghostscript will write the PDF</param>
public static void GhostscriptNetJoin(String[] fileNames, String outputPath)
{
var sb = new StringBuilder();
foreach (var fileName in fileNames)
{
var source = (fileName.IndexOf(' ') == -1) ? fileName : String.Format("\"{0}\"", fileName);
sb.Append(source + " ");
}
var output_file = (outputPath.IndexOf(' ') == -1) ? outputPath : String.Format("\"{0}\"", outputPath);
var gsArgs = new List<String>();
gsArgs.Add("-empty"); // first argument is ignored. REF: http://stackoverflow.com/q/25202577/153923
gsArgs.Add("-dBATCH");
gsArgs.Add("-q");
gsArgs.Add("-dNOPAUSE");
gsArgs.Add("-dNOPROMPT");
gsArgs.Add("-sDEVICE=pdfwrite");
gsArgs.Add("-dPDFSETTINGS=/prepress");
gsArgs.Add(String.Format(@"-sOutputFile={0}", output_file));
gsArgs.Add(sb.ToString());
var version = GhostscriptVersionInfo.GetLastInstalledVersion();
using (var processor = new GhostscriptProcessor(version, false))
{
processor.Process(gsArgs.ToArray());
}
}
How could I come back later to REPLACE or UPDATE page N?
I have stubbed out a routine that has my plan, but at this time I do not know how to complete it. Can I supply arg
values or is there a different tool I should be using?
/// <summary>
/// Replace Specific Document from source PDF file
/// </summary>
/// <param name="source">String. Full path to the multi-page PDF</param>
/// <param name="documentN">String. Full path to the document to insert</param>
/// <param name="indexN">int. Page Index where the new document should be inserted</param>
public static void GhostscriptNetReplace(String source, String documentN, int indexN)
{
var list = new List<String>();
var version = GhostscriptVersionInfo.GetLastInstalledVersion();
using (var processor = new GhostscriptProcessor(version, false))
{
var gsArgs = new List<String>();
// what arguments are needed?
throw new NotImplementedException("I don't know how to code for this yet.");
processor.Process(gsArgs.ToArray());
}
list.RemoveAt(indexN);
list.Insert(indexN, documentN);
var sb = new StringBuilder();
foreach (var fileName in list)
{
var fmtSource = (fileName.IndexOf(' ') == -1) ? fileName : String.Format("\"{0}\"", fileName);
sb.Append(fmtSource + " ");
}
var output_file = (source.IndexOf(' ') == -1) ? source : String.Format("\"{0}\"", source);
using (var processor = new GhostscriptProcessor(version, false))
{
var gsArgs = new List<String>();
gsArgs.Add("-empty"); // first argument is ignored. REF: http://stackoverflow.com/q/25202577/153923
gsArgs.Add("-dBATCH");
gsArgs.Add("-q");
gsArgs.Add("-dNOPAUSE");
gsArgs.Add("-dNOPROMPT");
gsArgs.Add("-sDEVICE=pdfwrite");
gsArgs.Add("-dPDFSETTINGS=/prepress");
gsArgs.Add(String.Format(@"-sOutputFile={0}", output_file));
gsArgs.Add(sb.ToString());
processor.Process(gsArgs.ToArray());
}
}