using Ghostscript.NET; using Ghostscript.NET.Rasterizer; namespace GhostScript { class Program { private static GhostscriptVersionInfo _lastInstalledVersion = null; private const int DPI = 200; static void Main(string[] args) { const string outputPath = @"output\"; if (!args.Any()) { Console.WriteLine("{0} [*.pdf]", Path.GetFileName(Environment.GetCommandLineArgs()[0])); return; } var inputPdfPath = args[0]; _lastInstalledVersion = GhostscriptVersionInfo.GetLastInstalledVersion( GhostscriptLicense.GPL | GhostscriptLicense.AFPL , GhostscriptLicense.GPL ); var rasterizer = new GhostscriptRasterizer(); rasterizer.CustomSwitches.Add("-dNOINTERPOLATE"); rasterizer.CustomSwitches.Add("-sPAPERSIZE=a4"); rasterizer.TextAlphaBits = 4; rasterizer.GraphicsAlphaBits = 4; rasterizer.Open(inputPdfPath, _lastInstalledVersion, false); if (Directory.Exists(outputPath)) { Directory.Delete(outputPath, true); } Directory.CreateDirectory(outputPath); for (var pageNumber = 1; pageNumber <= rasterizer.PageCount; pageNumber++) { var outputFileName = string.Format("Page-{0:0000}.jpg", pageNumber); var outputFilePath = Path.Combine(outputPath, outputFileName); using (var img = rasterizer.GetPage(DPI, DPI, pageNumber)) { img.Save(pageFilePath, ImageFormat.Jpeg); } } } } }
static void Print(string file) { using (var pd = new System.Drawing.Printing.PrintDocument()) { pd.PrinterSettings.Duplex = Duplex.Simplex; pd.PrintPage += (o, e) => { var img = System.Drawing.Image.FromFile(file); e.Graphics.DrawImage(img, e.Graphics.VisibleClipBounds); }; pd.Print(); } }
pd.PrinterSettings.PrinterName = " ";
var printrers = PrinterSettings.InstalledPrinters; pd.PrinterSettings.PrinterName = printrers[1];
e.HasMorePages = ++index < pages.Count;
pd.PrinterSettings.Duplex = Duplex.Vertical;
pd.PrintController = new StandardPrintController();
pd.DefaultPageSettings.PaperSource = pd.PrinterSettings.PaperSources[SourceId];
e.PageSettings.PaperSource = pd.PrinterSettings.PaperSources[SourceId];
using System; using System.Collections.Generic; using System.Diagnostics; using System.Drawing.Imaging; using System.Drawing.Printing; using System.IO; using System.Threading; using Ghostscript.NET; using Ghostscript.NET.Rasterizer; namespace GS_Parallel { class Program { public static Dictionary<int, MemoryStream> PageStore; // private const int Dpi = 200; private const int Quants = 30; private const int MaxThreads = 10; static void Main(string[] args) { PageStore = new Dictionary<int, MemoryStream>(); if (!args.Any()) { Console.WriteLine("{0} [*.pdf]", Path.GetFileName(Environment.GetCommandLineArgs()[0])); return; } var inputPdfPath = args[0]; ThreadPool.SetMaxThreads(MaxThreads, MaxThreads); var mainRasterizer = CreateRasterizer(inputPdfPath); // var step = mainRasterizer.PageCount / Quants; var tail = mainRasterizer.PageCount % Quants; var shift = 0; for (var i = 0; i < Quants; i++) { var wi = new WorkInfo() {StartPage = shift + 1, EndPage = shift + step, SourcefilePath = inputPdfPath}; ThreadPool.QueueUserWorkItem(PdfProcessing, wi); shift += step; } if (tail > 0) { var wi = new WorkInfo() { StartPage = shift + 1, EndPage = shift + tail, SourcefilePath = inputPdfPath }; ThreadPool.QueueUserWorkItem(PdfProcessing, wi); } Console.WriteLine("Start preparation"); while (PageStore.Count < mainRasterizer.PageCount) { // Console.WriteLine("{0:000.0}%", ((double)PageStore.Count) / mainRasterizer.PageCount * 100); Thread.Sleep(100); } Console.WriteLine("Start printing"); PrintPages(PageStore); } static GhostscriptVersionInfo _lastInstalledVersion = GhostscriptVersionInfo.GetLastInstalledVersion(GhostscriptLicense.GPL | GhostscriptLicense.AFPL, GhostscriptLicense.GPL); static GhostscriptRasterizer CreateRasterizer(string file) { var rasterizer = new GhostscriptRasterizer(); rasterizer.CustomSwitches.Add("-dNOINTERPOLATE"); rasterizer.CustomSwitches.Add("-dCOLORSCREEN=0"); rasterizer.CustomSwitches.Add("-sPAPERSIZE=a4"); rasterizer.TextAlphaBits = 4; rasterizer.GraphicsAlphaBits = 4; rasterizer.Open(file, _lastInstalledVersion, true); return _rasterizer; } static void PdfProcessing(object stateInfo) { var wi = (WorkInfo)stateInfo; var rasterizer = CreateRasterizer(wi.SourcefilePath); for (var pageNumber = wi.StartPage; pageNumber <= wi.EndPage; pageNumber++) { using (var img = rasterizer.GetPage(Dpi, Dpi, pageNumber)) { var mem = new MemoryStream(); img.Save(mem, ImageFormat.Jpeg); lock (PageStore) { PageStore[pageNumber] = mem; } } } } static void PrintPages(IReadOnlyDictionary<int, MemoryStream> pageStore) { using (var pd = new PrintDocument()) { pd.PrinterSettings.Duplex = Duplex.Simplex; pd.PrintController = new StandardPrintController(); var index = 0; pd.PrintPage += (o, e) => { var pageStream = pageStore[index + 1]; var img = System.Drawing.Image.FromStream(pageStream); e.Graphics.DrawImage(img, e.Graphics.VisibleClipBounds); index++; e.HasMorePages = index < pageStore.Count; Console.WriteLine("Print {0} of {1}; complete {2:000.0}%", index, pageStore.Count, ((double)index) / pageStore.Count * 100); }; pd.Print(); } } } class WorkInfo { public int StartPage; public int EndPage; public string SourcefilePath; } }
//Getting and configuration a jpeg encoder const long quality = 35L; var encoders = ImageCodecInfo.GetImageDecoders(); var jpgEncoder = encoders.FirstOrDefault(codec => codec.FormatID == ImageFormat.Jpeg.Guid); var encoderParams = new EncoderParameters(1); encoderParams.Param[0] = new EncoderParameter(Encoder.Quality, quality); //Save with the jpeg encoder img.Save(mems, jpgEncoder, encoderParams);
Source: https://habr.com/ru/post/279361/
All Articles