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.
as it should be
as it is
By the way, when I send the PDF directly to the printer, it prints correctly.
Anyone have an idea how to solve this?
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();
}