关于Spread WPF-Silverlight打印的问题
版主,您好!我在咱们公司的网站上没有找到关于Spread WPF-Silverlight控件打印功能的演示,我想请问一下,咱们的控件是否支持打印功能,如果支持能否发给我一个Silverlight的例子?我的邮箱是zysfaithboy@163.com。谢谢!盼回复!
祝好!
2013-11-28 回复 1楼zysfaithboy的帖子
zysfaithboy您好,
感谢您对 Spread 产品的支持。
Spread WPF-Silverlight控件目前不支持直接通过打印机打印功能。可以通过自定义方式实现:
private void btnPrint_Click(object sender, RoutedEventArgs e)
{
this.Cursor = System.Windows.Input.Cursors.Wait;
SpreadSheetPrinter ssp = new SpreadSheetPrinter(this.gcSpreadSheet1);
ssp.PrintDocument.EndPrint += (ss, ee) => { this.Cursor = System.Windows.Input.Cursors.Arrow;};
ssp.PrintActiveSheet("SpreadSheet Print");
}
public class SpreadSheetPrinter
{
GcSpreadSheet printSource;
PrintDocument pd;
string documentName;
Grid printVisual;
Grid container;
TextBlock header;
GcSpreadSheet printPreview;
TextBlock footer;
int vPageBreak;
int vNextPageBreak;
int hPageBreak;
int hNextPageBreak;
double pageWidth;
double pageHeight;
public SpreadSheetPrinter(GcSpreadSheet spread)
{
printSource = spread;
pd = new PrintDocument();
pd.PrintPage += new EventHandler<PrintPageEventArgs>(pd_PrintPage);
this.printVisual = new Grid();
this.container = new Grid();
this.container.RowDefinitions.Add(new RowDefinition());
this.container.RowDefinitions.Add(new RowDefinition());
this.container.RowDefinitions.Add(new RowDefinition());
this.printVisual.Children.Add(this.container);
this.header = new TextBlock();
this.header.FontSize = 20;
this.header.VerticalAlignment = System.Windows.VerticalAlignment.Top;
this.header.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
this.container.Children.Add(this.header);
this.footer = new TextBlock();
this.footer.VerticalAlignment = System.Windows.VerticalAlignment.Bottom;
this.footer.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
this.container.Children.Add(this.footer);
this.footer.SetValue(Grid.RowProperty, 2);
this.printPreview = new GcSpreadSheet();
this.container.Children.Add(this.printPreview);
this.printPreview.SetValue(Grid.RowProperty, 1);
this.printPreview.VerticalAlignment = System.Windows.VerticalAlignment.Top;
this.printPreview.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
}
public PrintDocument PrintDocument
{
get { return pd; }
}
public void PrintActiveSheet(string documentName)
{
MemoryStream ms = new MemoryStream();
this.printSource.SaveXml(ms);
ms.Seek(0, System.IO.SeekOrigin.Begin);
this.printPreview.OpenXml(ms);
ms.Close();
// hide scrollbar tabstrip border
this.printPreview.VerticalScrollBarVisibility = ScrollBarVisibility.Hidden;
this.printPreview.HorizontalScrollBarVisibility = ScrollBarVisibility.Hidden;
this.printPreview.TabStripVisibility = System.Windows.Visibility.Collapsed;
this.printPreview.BorderThickness = new Thickness(0);
// hide selection
var sheet = this.printPreview.ActiveSheet;
sheet.RowCount++;
sheet.ColumnCount++;
sheet.FrozenTrailingColumnCount = 1;
sheet.FrozenTrailingRowCount = 1;
sheet.SetRowVisible(sheet.RowCount - 1, SheetArea.Cells, false);
sheet.SetColumnVisible(sheet.ColumnCount - 1, SheetArea.Cells, false);
sheet.SetActiveCell(sheet.RowCount - 1, sheet.ColumnCount - 1, true);
vPageBreak = 0;
vNextPageBreak = -1;
hPageBreak = 0;
hNextPageBreak = -1;
pageWidth = 0;
pageHeight = 0;
this.documentName = documentName;
pd.Print(documentName);
}
void pd_PrintPage(object sender, PrintPageEventArgs e)
{
var sheet = this.printPreview.ActiveSheet;
var margin = sheet.PrintInfo.Margin;
// set control position
this.container.Margin = new Thickness(margin.Left, margin.Top, margin.Right, margin.Bottom);
this.container.RowDefinitions.Height = new GridLength(margin.Header);
this.container.RowDefinitions.Height = new GridLength(margin.Footer);
this.header.Text = this.documentName;
this.footer.Text = string.Format("page {0}", this.pd.PrintedPageCount + 1);
// scroll to current page
this.printPreview.ShowRow(0, vPageBreak, VerticalPosition.Top);
this.printPreview.ShowColumn(0, hPageBreak, HorizontalPosition.Left);
// calc current page size, and find next page break
if (pageHeight == 0) // current page height is not calc yet
{
for (int i = vPageBreak; i < sheet.RowCount; i++)
{
double rowHeight = sheet.GetActualRowHeight(i, SheetArea.Cells) * sheet.ZoomFactor;
if ((pageHeight + rowHeight) > (e.PrintableArea.Height - margin.Bottom - margin.Top - margin.Header - margin.Footer))
{
vNextPageBreak = i;
break;
}
pageHeight += rowHeight;
}
if (this.printPreview.Height != pageHeight)
{
this.printPreview.Height = pageHeight;
}
}
if (pageWidth == 0) // current page width is not calc yet
{
for (int i = hPageBreak; i < sheet.ColumnCount; i++)
{
double columnWidth = sheet.GetActualColumnWidth(i, SheetArea.Cells) * sheet.ZoomFactor;
if ((pageWidth + columnWidth) > (e.PrintableArea.Width - margin.Left - margin.Right))
{
hNextPageBreak = i;
break;
}
pageWidth += columnWidth;
}
if (this.printPreview.Width != pageWidth)
{
this.printPreview.Width = pageWidth;
}
}
e.PageVisual = this.printVisual;
// prepare for next page
if (sheet.PrintInfo.PageOrder == PrintPageOrder.DownThenOver)
{
if (vNextPageBreak != -1) // has next page in vertical
{
vPageBreak = vNextPageBreak;
vNextPageBreak = -1;
pageHeight = 0;
e.HasMorePages = true;
}
else if (hNextPageBreak != -1)
{
hPageBreak = hNextPageBreak;
hNextPageBreak = -1;
pageWidth = 0;
pageHeight = 0;
vPageBreak = 0;
vNextPageBreak = -1;
e.HasMorePages = true;
}
}
else
{
if (hNextPageBreak != -1)
{
hPageBreak = hNextPageBreak;
hNextPageBreak = -1;
pageWidth = 0;
e.HasMorePages = true;
}
else if (vNextPageBreak != -1)
{
vPageBreak = vNextPageBreak;
vNextPageBreak = -1;
pageWidth = 0;
pageHeight = 0;
hPageBreak = 0;
hNextPageBreak = -1;
e.HasMorePages = true;
}
}
}
}
同时支持导出到 PDF 文件。
有其他问题,欢迎继续提出。
谢谢 非常感谢版主回复,不知道咱们公司有没有计划在咱们的控件中增加自带的打印功能,如果有计划,大概能够在什么时间推出该版本? 回复 3楼zysfaithboy的帖子
zysfaithboy 你好,
目前“打印功能”没有明确的添加计划。
我需要和开发组再次确认后给你回复。 回复 3楼zysfaithboy的帖子
您好,
目前 Spread 8.0 在XAML和WinRT平台产品中提供了打印和打印预览功能,公式支持结构化引用,新的控件外观,更重要的是在Excel导入速度方面也有明显的提升。
下载链接:
http://www.gcpowertools.com.cn/downloads/trial/SpreadStudio/8/SpreadforWPF-Silverlightv8.zip
谢谢
页:
[1]