/// <summary> /// Returns Ink coverage for specified page range. /// The result is ink coverage for the CMYK inks, separately for each single page (for RGB colors, it does a silent conversion to CMYK color space internally). /// This function is supported only in Ghostscript v9.05 or newer. /// </summary> /// <param name="path">PDF file path.</param> /// <param name="firstPage">Designated start page of the document. Pages of all documents in PDF collections are numbered sequentionally.</param> /// <param name="lastPage">Designated end page of the document. Pages of all documents in PDF collections are numbered sequentionally.</param> /// <param name="versionInfo">GhostscriptVersionInfo instance that tells which Ghostscript library to use.</param> /// <returns>A dictionary of a page numbers with the ink coverage.</returns> public static Dictionary<int, GhostscriptPageInkCoverage> GetInkCoverage(string path, int firstPage, int lastPage, GhostscriptVersionInfo versionInfo) { GhostscriptPipedOutput gsPipedOutput = new GhostscriptPipedOutput(); string outputPipeHandle = "%handle%" + int.Parse(gsPipedOutput.ClientHandle).ToString("X2"); List<string> switches = new List<string>(); switches.Add("-empty"); switches.Add("-q"); if (firstPage != 0 && lastPage != 0) { switches.Add("-dFirstPage=" + firstPage.ToString()); switches.Add("-dLastPage=" + lastPage.ToString()); } switches.Add("-o" + outputPipeHandle); switches.Add("-sDEVICE=inkcov"); switches.Add(path); GhostscriptProcessor proc = new GhostscriptProcessor(versionInfo, false); proc.StartProcessing(switches.ToArray(), null); byte[] data = gsPipedOutput.Data; gsPipedOutput.Dispose(); gsPipedOutput = null; string output = Encoding.ASCII.GetString(data); if (output.Length > 0) { Dictionary<int, GhostscriptPageInkCoverage> result = new Dictionary<int, GhostscriptPageInkCoverage>(); string[] outputLines = output.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries); int pageNumber = firstPage == 0 ? 1 : firstPage; foreach(string line in outputLines) { GhostscriptPageInkCoverage pic = new GhostscriptPageInkCoverage(); pic.Page = pageNumber; pic.IsValid = false; string[] lineParts = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); if (lineParts.Length == 6) { pic.C = Math.Round(float.Parse(lineParts[0], CultureInfo.InvariantCulture) * 100, 2); pic.M = Math.Round(float.Parse(lineParts[1], CultureInfo.InvariantCulture) * 100, 2); pic.Y = Math.Round(float.Parse(lineParts[2], CultureInfo.InvariantCulture) * 100, 2); pic.K = Math.Round(float.Parse(lineParts[3], CultureInfo.InvariantCulture) * 100, 2); if (lineParts[5] == "OK") { pic.IsValid = true; } } result.Add(pageNumber, pic); pageNumber++; } return result; } else { return null; } }
/// <summary> /// Returns Ink coverage for specified page range. /// The result is ink coverage for the CMYK inks, separately for each single page (for RGB colors, it does a silent conversion to CMYK color space internally). /// This function is supported only in Ghostscript v9.05 or newer. /// </summary> /// <param name="path">PDF file path.</param> /// <param name="firstPage">Designated start page of the document. Pages of all documents in PDF collections are numbered sequentionally.</param> /// <param name="lastPage">Designated end page of the document. Pages of all documents in PDF collections are numbered sequentionally.</param> /// <param name="versionInfo">GhostscriptVersionInfo instance that tells which Ghostscript library to use.</param> /// <returns>A dictionary of a page numbers with the ink coverage.</returns> public static Dictionary <int, GhostscriptPageInkCoverage> GetInkCoverage(string path, int firstPage, int lastPage, GhostscriptVersionInfo versionInfo) { GhostscriptPipedOutput gsPipedOutput = new GhostscriptPipedOutput(); string outputPipeHandle = "%handle%" + int.Parse(gsPipedOutput.ClientHandle).ToString("X2"); List <string> switches = new List <string>(); switches.Add("-empty"); switches.Add("-q"); if (firstPage != 0 && lastPage != 0) { switches.Add("-dFirstPage=" + firstPage.ToString()); switches.Add("-dLastPage=" + lastPage.ToString()); } switches.Add("-o" + outputPipeHandle); switches.Add("-sDEVICE=inkcov"); switches.Add(path); GhostscriptProcessor proc = new GhostscriptProcessor(versionInfo, false); proc.StartProcessing(switches.ToArray(), null); byte[] data = gsPipedOutput.Data; gsPipedOutput.Dispose(); gsPipedOutput = null; string output = Encoding.ASCII.GetString(data); if (output.Length > 0) { Dictionary <int, GhostscriptPageInkCoverage> result = new Dictionary <int, GhostscriptPageInkCoverage>(); string[] outputLines = output.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries); int pageNumber = firstPage == 0 ? 1 : firstPage; foreach (string line in outputLines) { GhostscriptPageInkCoverage pic = new GhostscriptPageInkCoverage(); pic.Page = pageNumber; pic.IsValid = false; string[] lineParts = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); if (lineParts.Length == 6) { pic.C = Math.Round(float.Parse(lineParts[0], CultureInfo.InvariantCulture) * 100, 4); pic.M = Math.Round(float.Parse(lineParts[1], CultureInfo.InvariantCulture) * 100, 4); pic.Y = Math.Round(float.Parse(lineParts[2], CultureInfo.InvariantCulture) * 100, 4); pic.K = Math.Round(float.Parse(lineParts[3], CultureInfo.InvariantCulture) * 100, 4); if (lineParts[5] == "OK") { pic.IsValid = true; } } result.Add(pageNumber, pic); pageNumber++; } return(result); } else { return(null); } }