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());
}
}
I'm going to add an answer based on what I read in baaron's post here:
Convert PDF to JPG / Images without using a specific C# Library
I modified his code, and I think it will satisfy my needs. Like KenS posted in a comment above, though, this will continue to lose quality each time it is run.
Work has decided to put this off for now because they are not ready to risk losing information quality.
This code, then, is put out there as untested.
In theory, it should work.
If it helps, please let me know. I hate following up with answers to my own questions if no one ever looks at it.
You might be able to do something like this (unable to test code right now, but the principle of it checks out based on the Ghostscript.NET repo):
From my related post:
You could use the PDF Toolkit PDFtk:
Example:
The output consists of the first 12 pages of
inA.pdf
, followed by page 3 ofinB.pdf
and then pages 14 until end ofinA.pdf
.Many Linux distributions provide a PDFtk package you can download and install using their package manager.