Spread 表格控件:打印带背景图的 PDF 文件

发布时间:2013/08/09 00:08 发布者:iceman

返回博客中心

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: 点击下载


关于葡萄城

赋能开发者!葡萄城是专业的集开发工具、商业智能解决方案、低代码开发平台于一身的软件和服务提供商,为超过 75% 的全球财富 500 强企业提供服务。葡萄城专注控件软件领域30年,希望通过模块化的开发控件、灵活的低代码应用开发平台等一系列开发工具、解决方案和服务,帮助开发者快速响应复杂多变的业务需求,最大程度地发挥开发者的才智和潜能,让开发者的 IT 人生更从容更美好。

了解详情,请访问葡萄城官网