0

我试图自动填写 pdf 表单,但它不会让我在 pdf 上绘图。所以我创建了一个表格的图像并使用拉绳方法填充它。问题是,即使打印审查在我尝试打印时正确显示文档,我也会得到一个大于纸张尺寸的放大文档。我无法想象为什么。这是我的代码:

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));
    }

预览窗口显示了正确的文档,其中填满了空白。但是当我按下预览窗口上的打印按钮时,我得到了放大的文档......编辑:我应该补充一点,当我用 Paint 打开 .bmp 文件时,我可以正确打印图像。同样的图像在我的项目中显得更大更模糊,没有任何明显的原因。为什么打印机会拉伸我的图像?为什么预览窗口会显示正确的图像尺寸?

4

1 回答 1

0

这就是我想出的:

        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));

所以我认为您需要通过更改 defaultpagesettings.pagesize 属性来设置打印预览页面大小。我不知道为什么自定义纸张尺寸不起作用。然后你需要在 DrawImage 方法中设置图像大小。最后一件事:图像不像原点那样清晰。它可用但模糊。

于 2013-04-25T16:31:23.747 回答