0

I tried to autofill a pdf form but it won't let me draw over the pdf. So I created an image of the form and filled it by using the drawstring method. The problem is that even though the print review shows the document correctly when I try to print it I get an enlarged document which is bigger than the paper size. I can't imagine why. Here is my code:

private void buttonPrint_Click(object sender, EventArgs e)
    {
        Image img = panelForm.BackgroundImage;
        printDocumentForm.DefaultPageSettings.PaperSize = new PaperSize("A4", img.Width, img.Height);
        printPreviewForm.Document = printDocumentForm;
        printPreviewForm.ShowDialog();
    }

    private void printDocumentForm_PrintPage(object sender, PrintPageEventArgs e)
    {

        Image img = panelForm.BackgroundImage;
        Image copy = (Image) img.Clone();
        // I've added some drawstring methods here to fill the blanks
        // but I removed them in this example to save some space
        e.Graphics.DrawImage(copy, new Point(0, 0));
    }

The preview window shows the correct document with the blanks filled. But when I push the print button on the preview window I get the enlarged document... edit: I should probably add that when I open the .bmp file with Paint, I can print the image correctly. The same image appears bigger and blurred in my project without any obvious reason. Why does the printer stretch my image? And why does the preview window show the correct size of the image?

pzogr
  • 424
  • 1
  • 12
  • 30

1 Answers1

0

This is what I came up with:

        printDoc.DefaultPageSettings.PaperSize=printDoc.PrinterSettings.PaperSizes[4];
        // this sets the printPreview document size to A4. The previously presented code doesn't
        Image img = panelDM.BackgroundImage; // this is what I want to print
        Rectangle bounds = e.PageBounds;            
        int mx = 30;
        int my = 30; // add borders
        int x = img.Width;
        int y = img.Height;
        int xp = bounds.Width*9/10; // for some reason (I can't explain why)
        int yp = bounds.Height*9/10; // 100% appears larger than the page...
        int xr, yr;
        if (x > y)
        {
            xr = xp-mx;
            yr = xr*y/x;
        }
        else
        {
            yr = yp-my;
            xr = yr*x/y;
        }
        Size size = new Size(xr,yr);
        Image copy = (Image)img.Clone();            

        e.Graphics.DrawImage(copy, new Rectangle(new Point (mx,my), size));

So I figured that you need to set the printpreview page size by altering the defaultpagesettings.pagesize property. I don't know why the custom papersize did not work. Then you need to set the image size in the DrawImage method. One last thing: the image does not appear clear like the origin. It's usable but blurred.

pzogr
  • 424
  • 1
  • 12
  • 30