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.
We got it working, apparently it was only giving a white page when printing to pdf. The exact same code rendered a much too small image but actually printed. We're still not entirely sure what the issue was, but worked out new code that properly prints to pdf, and prints full-scale to a printer.
void PrintPage(object sender, PrintPageEventArgs ev)
{
Graphics gr = ev.Graphics;
gr.PageUnit = GraphicsUnit.Pixel; //this has been changed to Pixel, from Inch.
float dpi = gr.DpiX;
//if (dpi > 300) dpi = 300;
Rectangle rectPage = ev.PageBounds; //print without margins
//Rectangle rectPage = ev.MarginBounds; //print using margins
float dpi = gr.DpiX;
int example = 1;
bool use_hard_margins = false;
// Example 1) Print the Bitmap.
if (example == 1)
{
pdfdraw.SetDPI(dpi);
pdfdraw.SetDrawAnnotations(false);
Bitmap bmp = pdfdraw.GetBitmap(pageitr.Current());
gr.DrawImage(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height), 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel);
}
`
if (dpi > 300) dpi = 300;
This was the main culprit in rendering a too small image being sent to the printer. It also fixed the 'white pdf' issue.
Second, we didn't pass rectPage
to DrawImage, and replaced it with: new Rectangle(0, 0, bmp.Width, bmp.Height)
.
I can understand the smaller size being sent to the printer, but why it didn't pick up anything to print to pdf is still unclear.
While the ultimate goal is still printing, it's much easier to debug and test with a properly working 'print to pdf'. The above code works in 2 separate projects, so I'm going to assume this indeed fixes the issue.