← 返回所有博客文章

Spread 表格控件支持打印到 PDF 文件,通过 PrintInfo 类下的 PrintToPdf 方法可以实现打印 PDF 功能。

最近,有用户询问如何打印带有背景图的PDF文件。虽然,Spread 不内置该功能,但是我们可以同过自定义来实现。

实现方法很简单,设置 Spread 的背景色为透明色,此时Spread的背景图即可见。

详细请参考代码:

  1: private void fpSpread1_PrintBackground(object sender, FarPoint.Win.Spread.PrintBackgroundEventArgs e)
  2:  {
  3:     System.Drawing.Drawing2D.GraphicsState saveState = e.Graphics.Save();
  4:     Rectangle rect = e.SheetRectangle;
  5:     rect.Width = (int)AdjustWorkaroundForPDFPrint((float)rect.Width);
  6:     rect.Height = (int)AdjustWorkaroundForPDFPrint((float)rect.Height);
  7:     e.Graphics.SetClip(rect);
  8:     e.Graphics.SetClip(rect);
  9:     e.Graphics.DrawImage(fpSpread1.BackgroundImage, rect);
 10:     e.Graphics.Restore(saveState);
 11:  }
 12:     private float AdjustWorkaroundForPDFPrint(float value)
 13:  {
 14:     float _ptperInch = 72;
 15:     //Points Per inch
 16:     float pixelPointFactor = (float)(_ptperInch / 96);
 17:     //pixel point factor base on graphic dpi
 18:     float displayPointFactor = (float)(_ptperInch / 100);
 19:     //point factor base on display graphic unit
 20:     return (float)(value * displayPointFactor / pixelPointFactor);
 21:  }

 

SpreadWithBackGroundImage-300x281

ExportedPDF-300x166

Demo 下载:VS2010 + C# + Spread Studio 7 + .NET 4.0: 点击下载

Demo 下载:VS2010 + VB.NET + Spread Studio 7 + .NET 4.0: 点击下载