1

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.

Silvio Langereis
  • 487
  • 1
  • 11
  • 20

2 Answers2

1

Thank you for pointing this out. Just as you are, I am unclear why bmp.Save works fine, but Graphics.DrawImage(bmp,... is only showing the background color. I suspect it has something to do with the other parameters passed into Graphics.DrawImage

Since the Bitmap object is correct, then this particular issue is really a .Net question and not a PDFNet question, which I am currently unable to answer.

The other part of the sample runs fine, the one using PDFDraw.DrawInRect. Does this not work for you?

Ryan
  • 2,473
  • 1
  • 11
  • 14
  • We now print the page successfully, strangely enough, it didn't with 'print to pdf'. It's a bit of a hassle having to actually print for testing purposes though. We now have what we need, but it's still a mystery as to why it only shows a white page in 'print to pdf'. And indeed, it's probably a .NET issue. – Silvio Langereis Jun 14 '17 at 07:04
  • Ryan, I added a code snippet that worked for us, as an answer. Hopefully you can get something out of it, in case you want to change your sample code. – Silvio Langereis Jun 14 '17 at 08:41
  • If you have a new, or separate issue, please post a new SO question. – Ryan Jun 14 '17 at 16:35
1

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.

Silvio Langereis
  • 487
  • 1
  • 11
  • 20