2013-04-06 17 views
0

我试图自动填充pdf表单,但它不会让我画pdf。所以我创建了一个窗体的图像,并使用drawstring方法填充它。问题是,即使打印审阅在我尝试打印时正确显示文档,我也会得到比纸张大小更大的放大文档。我无法想象为什么。 这里是我的代码:c中的printpreview图像#

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文件时,我可以正确打印图像。没有任何明显的原因,相同的图像在我的项目中显得更大,更模糊。打印机为什么拉伸我的图像?为什么预览窗口显示正确的图像大小?

回答

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方法中设置图像大小。最后一件事:图像看起来不像原点那样清晰。它可用但模糊。