使用GhostScript.NET DPI打印问题打印PDF

我正在使用GhostScript.NET来打印PDF。 当我以96DPI打印时,PDF打印很好,但有点模糊。 如果我尝试以600DPI打印文档,则打印的页面会非常放大。

using GhostScript.NET.Rasterizer; using System.Drawing.Printing; PrintDocument doc = new PrintDocument(); doc.PrinterSettings.PrinterName=""; doc.PrinterSettings.Copies=(short)1; GhostScriptRasterizer rasterizer = new GhostScriptRasterizer(); rasterizer.Open("abc.pdf"); //Image page = rasterizer.GetPage(96,96); <-- this one prints ok Image page = rasterizer.GetPage(600,600); doc.Graphics.DrawImage(page, new Point()); 

我在注意页面对象时注意到的一件事是,尽管我传递了GetPage()600,600,但返回的图像的Horizo​​ntalResolution为96,VerticalResolution为96。

所以我尝试了以下内容:

 Bitmap b = new Bitmap(page.Width,page.Height); b.SetResolution(600,600); Graphics g = Graphics.FromImage(b); g.DrawImage(page,0,0); page = b; 

它的Horizo​​ntalResolution为600,VerticalResolution为600,但这样打印的图像更大!

谁能在这里给出建议?

嗨我有同样的问题。

光栅器只能缩放到dpi …

你必须使用GhostScriptProcessor。

 private List GetImageWithGhostGhostscriptProcessor(string psFilename, string outputPath, int dip = 300) { if (!Directory.Exists(outputPath)) Directory.CreateDirectory(outputPath); GhostscriptVersionInfo gv = GhostscriptVersionInfo.GetLastInstalledVersion( GhostscriptLicense.GPL | GhostscriptLicense.AFPL, GhostscriptLicense.GPL); using (GhostscriptProcessor processor = new GhostscriptProcessor(gv, true)) { processor.Processing += new GhostscriptProcessorProcessingEventHandler(processor_Processing); List switches = new List(); switches.Add("-empty"); switches.Add("-dSAFER"); switches.Add("-dBATCH"); switches.Add("-dNOPAUSE"); switches.Add("-dNOPROMPT"); switches.Add(@"-sFONTPATH=" + System.Environment.GetFolderPath(System.Environment.SpecialFolder.Fonts)); switches.Add("-dFirstPage=" + 1); switches.Add("-dLastPage=" + 1); //switches.Add("-sDEVICE=png16m"); switches.Add("-sDEVICE=tiff24nc"); //switches.Add("-sDEVICE=pdfwrite"); switches.Add("-r" + dip); switches.Add("-dTextAlphaBits=4"); switches.Add("-dGraphicsAlphaBits=4"); switches.Add(@"-sOutputFile=" + outputPath + "\\page-%03d.tif"); //switches.Add(@"-sOutputFile=" + outputPath + "page-%03d.png"); switches.Add(@"-f"); switches.Add(psFilename); processor.StartProcessing(switches.ToArray(), null); } return Directory.EnumerateFiles(outputPath).ToList(); } 

我认为您传递给DrawImage方法的参数应该是您希望图像在纸上的大小,而不是默认情况下保留它们,DrawImage命令将为您处理缩放。 可能最简单的方法是使用DrawImage命令的以下重写。

注意:如果图像的比例与矩形不同,则会使图像歪斜。 对图像大小和纸张大小的一些简单数学运算将允许您创建一个适合纸张边界的新矩形,而不会使图像偏斜。

这对我有用:

 int desired_x_dpi = 600; int desired_y_dpi = 600; Image img = rasterizer.GetPage(desired_x_dpi, desired_y_dpi, pageNumber); System.Drawing.Printing.PrintDocument pd = new System.Drawing.Printing.PrintDocument(); pd.PrintPage += (sender, args) => { args.Graphics.DrawImage(img, args.MarginBounds); }; pd.Print();