While testing in the samples from PDFPrintTest, we noticed that Example 2 coupled with Event Handler's example 1 is not behaving properly.
Example 1 of PrintPage Event Handler:
void PrintPage(object sender, PrintPageEventArgs ev)
{
Graphics gr = ev.Graphics;
gr.PageUnit = GraphicsUnit.Inch;
Rectangle rectPage = ev.PageBounds; //print without margins
//Rectangle rectPage = ev.MarginBounds; //print using margins
float dpi = gr.DpiX;
if (dpi > 300) dpi = 300;
int example = 1;
bool use_hard_margins = false;
// Example 1) Print the Bitmap.
if (example == 1)
{
pdfdraw.SetDPI(dpi);
Bitmap bmp = pdfdraw.GetBitmap(pageitr.Current());
//bmp.Save("tiger.jpg");
gr.DrawImage(bmp, rectPage, 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel);
}
Full sample code here: https://www.pdftron.com/pdfnet/samplecode/PDFPrintTest.cs.html
You'll notice the bmp.Save("tiger.jpg");
in comment, that's the point where it goes wrong. If we run the code and save the bmp, we get exactly what we need in the jpg file. However, gr.DrawImage(bmp, rectPage, 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel);
prints a plain empty pdf page. Why is that ?
Our goal: We need to force a printjob with 40% grayscale in certain circumstances. Winforms does not support this, we can only set grayscale, not specify a percentage, so we are looking to intercept the print and change the output to 40% grayscale, which lead us to the PdfNet Print samples. From these samples, only example 2 in the handler has Graphics gr
which accepts a color matrix to set the wanted grayscale to the page.
Any non-PdfNet solution is welcome aswell, but it's still odd that the sample code isn't working out of the box.