ArtifexSoftware / Ghostscript.NET

Ghostscript.NET - managed wrapper around the Ghostscript library (32-bit & 64-bit). Tested with Ghostscript versions < 10.
https://ghostscript.com
GNU Affero General Public License v3.0
403 stars 155 forks source link

Everything bold and blurred #112

Closed atmike closed 1 year ago

atmike commented 1 year ago

Hello all, I currently have the task that I have to print shipping labels that I get as PDF via C#. I use Ghostscript to split the PDF into individual pages and then print the images. This works very well, but I have the problem that everything is printed bold. This is especially a problem with the printed 2D barcodes, because then you can not read them.

Here is the Code I' using at the moment to create the Images:

int desired_dpi = 360;
using (var rasterizer = new GhostscriptRasterizer(Ghostscripts))
{       
      rasterizer.GraphicsAlphaBits = 0;
      rasterizer.TextAlphaBits = 0;
      var bytes = StaticHelper.ReadFully(inputFile);

      var path = Path.Combine(setting.DownloadFolder, Guid.NewGuid().ToString() + ".pdf");
      File.WriteAllBytes(path, bytes);
      rasterizer.Open(path);

      for (var pageNumber = 1; pageNumber <= rasterizer.PageCount; pageNumber++)
      {
             PrintInfo PrintInfo = new PrintInfo() { PDFFile = path };

             var pageFilePath = Path.Combine(setting.DownloadFolder, string.Format("Page-{0}-{1}.png", Guid.NewGuid().ToString(), pageNumber));

             var img = rasterizer.GetPage(desired_dpi, pageNumber);
             img.Save(pageFilePath, ImageFormat.Png);
             img.Dispose();
      }
      rasterizer.Close();
}

As you can see a already played with GraphicsAlphaBits / TextAlphaBits and sets them to 0 and I changed the DPI. But nothing worked the Bold will not go away.

By the way, when I send the PDF directly to the printer, it prints correctly.

Anyone have an idea how to solve this?

Best Regards Michael

jhabjan commented 1 year ago

Can you please share your PDF so I can take a look into this?

atmike commented 1 year ago

Found your Mail address and send you all the Information to your gmail address. Thanks for helping

jhabjan commented 1 year ago

Got it, please give me some time to look into this.

jhabjan commented 1 year ago

It looks like interpolation issue. By default GhostscriptRasterizer adds -dDOINTERPOLATE parameter which you can override (turn it off) by adding -dNOINTERPOLATE custom switch before opening GhostscriptRasterizer like this:

int desired_dpi = 360;
using (var rasterizer = new GhostscriptRasterizer(Ghostscripts))
{       
      var bytes = StaticHelper.ReadFully(inputFile);

      var path = Path.Combine(setting.DownloadFolder, Guid.NewGuid().ToString() + ".pdf");
      File.WriteAllBytes(path, bytes);

      rasterizer.CustomSwitches.Add("-dNOINTERPOLATE");    // >>>>> custom switch 

      rasterizer.Open(path);

      for (var pageNumber = 1; pageNumber <= rasterizer.PageCount; pageNumber++)
      {
             PrintInfo PrintInfo = new PrintInfo() { PDFFile = path };

             var pageFilePath = Path.Combine(setting.DownloadFolder, string.Format("Page-{0}-{1}.png", Guid.NewGuid().ToString(), pageNumber));

             var img = rasterizer.GetPage(desired_dpi, pageNumber);
             img.Save(pageFilePath, ImageFormat.Png);
             img.Dispose();
      }
      rasterizer.Close();
}

I've tested your PDF with -dNOINTERPOLATE custom switch and that fixes the problem.

Cheers

atmike commented 1 year ago

Hi Josip, thank you for your help this was exactly the solution. Barcodes look good and I can scan them. Thanks a lot for your help